Menu
Amazon Kinesis Streams
Developer Guide

Step 6: (Optional) Extending the Consumer

The application shown here may already be sufficient for your purposes. This optional section shows how you might want to extend the consumer code for a slightly more elaborate scenario.

If you want to know about the biggest sell orders each minute, this is a matter of modifying the StockStats class in three places to accommodate this new priority.

To extend the consumer

  1. Add new instance variables:

     // 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. Add the following code to addStockTrade:

     if (type == TradeType.SELL) {
         if (largestSellOrderStock == null || trade.getQuantity() > largestSellOrderQuantity) {
             largestSellOrderStock = trade.getTickerSymbol();
             largestSellOrderQuantity = trade.getQuantity();
         }
     }
  3. Modify the toString method to print the additional information:

     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);
     }

If you run the consumer now (remember to run the producer also), you should see output similar to this:

 ****** 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.
  ****************************************************************