示例#1
0
 @Override
 public void window(MessageCollector collector, TaskCoordinator coordinator) {
   KeyValueIterator<String, String> iter = this.store.all();
   String currentEpoch = this.store.get(CURRENT_EPOCH);
   out.println("Checking if epoch " + currentEpoch + " is complete.");
   int count = 0;
   while (iter.hasNext()) {
     String foundEpoch = iter.next().getValue();
     if (foundEpoch.equals(currentEpoch)) count += 1;
   }
   iter.close();
   if (count == expectedKeys + 1) {
     out.println("Epoch " + currentEpoch + " is complete.");
     int nextEpoch = Integer.parseInt(currentEpoch) + 1;
     for (int i = 0; i < numPartitions; i++)
       collector.send(
           new OutgoingMessageEnvelope(
               new SystemStream("kafka", "epoch"), i, Integer.toString(nextEpoch)));
   } else if (count > expectedKeys + 1) {
     throw new IllegalStateException(
         "Got " + count + " keys, which is more than the expected " + (expectedKeys + 1));
   } else {
     out.println("Only found " + count + " valid keys, try again later.");
   }
 }
  public void process(
      IncomingMessageEnvelope envelope, MessageCollector collector, TaskCoordinator coordinator)
      throws Exception {
    Map<String, Object> msg = (Map<String, Object>) envelope.getMessage();

    int logval;
    try {
      logval = Integer.parseInt((String) msg.get("log")); // 这里少了trim!!!
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
    if (logval >= 10) {
      collector.send(
          new OutgoingMessageEnvelope(new SystemStream("kafka", "g10"), envelope.getKey(), msg));
    }

    // check if it's a debug message
    if (msg.containsKey("mode") && msg.get("mode").equals("DEBUG")) {
      msg.put("path", "B");
      collector.send(
          new OutgoingMessageEnvelope(new SystemStream("kafka", "debug"), msg.get("user"), msg));
    }
  }
  @Override
  public void window(MessageCollector collector, TaskCoordinator coordinator) {
    String msg =
        String.format(
            "Tweets: %d Positive: %d Negative: %d MaxPositive: %d MinPositive: %d",
            tweets, positiveTweets, negativeTweets, maxPositive, maxNegative);

    collector.send(
        new OutgoingMessageEnvelope(new SystemStream("kafka", "tweet-sentiment-stats"), msg));

    // Reset counts after windowing.
    tweets = 0;
    positiveTweets = 0;
    negativeTweets = 0;
    maxPositive = 0;
    maxNegative = 0;
  }