(可選)擴展消費者 - Amazon Kinesis Data Streams

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

(可選)擴展消費者

此一選用章節示範如何就更為複雜的情境擴充消費者程式碼。

如果您想知道每分鐘銷售量最高的訂單,則可修改三處位置的 StockStats 類別以納入這項新的優先等級。

擴充消費者
  1. 加入新的執行個體變數:

    // 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;
  2. addStockTrade 添加以下程式碼:

    if (type == TradeType.SELL) { if (largestSellOrderStock == null || trade.getQuantity() > largestSellOrderQuantity) { largestSellOrderStock = trade.getTickerSymbol(); largestSellOrderQuantity = trade.getQuantity(); } }
  3. 修改 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. ****************************************************************

後續步驟

清除資源