本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
(可选)扩展消费端
教程:使用 KPL 和 KCL 1.x 处理实时股票数据 中的应用程序可能已足以达到您的目的。此可选部分演示如何针对更为复杂的场景扩展消费端代码。
如果要了解每分钟的最大销售订单数,可以修改三个位置的 StockStats
类以适应此新的优先级。
扩展消费端
-
添加新实例变量:
// Ticker symbol of the stock that had the largest quantity of shares sold private String largestSellOrderStock; // Quantity of shares for the largest sell order trade private long largestSellOrderQuantity;
-
将以下代码添加到
addStockTrade
:if (type == TradeType.SELL) { if (largestSellOrderStock == null || trade.getQuantity() > largestSellOrderQuantity) { largestSellOrderStock = trade.getTickerSymbol(); largestSellOrderQuantity = trade.getQuantity(); } }
-
修改
toString
方法以打印其他信息:public String toString() { return String.format( "Most popular stock being bought: %s, %d buys.%n" + "Most popular stock being sold: %s, %d sells.%n" + "Largest sell order: %d shares of %s.", getMostPopularStock(TradeType.BUY), getMostPopularStockCount(TradeType.BUY), getMostPopularStock(TradeType.SELL), getMostPopularStockCount(TradeType.SELL), largestSellOrderQuantity, largestSellOrderStock); }
如果您现在运行消费端(请记住同时运行创建器),则应看到类似于以下内容的输出:
****** Shard shardId-000000000001 stats for last 1 minute ****** Most popular stock being bought: WMT, 27 buys. Most popular stock being sold: PTR, 14 sells. Largest sell order: 996 shares of BUD. ****************************************************************