从Apache Storm Bolt打印

时间:2015-05-18 09:38:13

标签: java apache-storm

我正在研究一些Storm拓扑和螺栓的示例代码,但我遇到了一些奇怪的事情。我的目标是使用Storm建立Kafka,以便Storm可以处理Kafka总线上可用的消息。我定义了以下螺栓:

public class ReportBolt extends BaseRichBolt {

  private static final long serialVersionUID = 6102304822420418016L;

  private Map<String, Long> counts;
  private OutputCollector collector;

  @Override @SuppressWarnings("rawtypes")
  public void prepare(Map stormConf, TopologyContext context, OutputCollector outCollector) {
    collector = outCollector;
    counts = new HashMap<String, Long>();
  }

  @Override

  public void declareOutputFields(OutputFieldsDeclarer declarer) {
    // terminal bolt = does not emit anything
  }

  @Override
  public void execute(Tuple tuple) {   
    System.out.println("HELLO " + tuple);
  }

  @Override
  public void cleanup() {
    System.out.println("HELLO FINAL");
  }
}

实质上,它应该只输出每个Kafka消息;并且当调用清除功能时,应显示不同的消息。

我查看了工作日志,我找到了最后的消息(即&#34; HELLO FINAL&#34;),但是Kafka消息与&#34; HELLO&#34;无处可寻。据我所知,这应该是一个简单的打印机螺栓,但我无法看到我出错的地方。工人日志表明我已连接到Kafka总线(它取得偏移等)。

简而言之,为什么我的println没有出现在工作日志中?

编辑

public class AckedTopology {

  private static final String SPOUT_ID = "monitoring_test_spout";
  private static final String REPORT_BOLT_ID = "acking-report-bolt";
  private static final String TOPOLOGY_NAME = "monitoring-topology";

  public static void main(String[] args) throws Exception {
    int numSpoutExecutors = 1;

    KafkaSpout kspout = buildKafkaSpout();
    ReportBolt reportBolt = new ReportBolt();

    TopologyBuilder builder = new TopologyBuilder();

    builder.setSpout(SPOUT_ID, kspout, numSpoutExecutors);
    builder.setBolt(REPORT_BOLT_ID, reportBolt);

    Config cfg = new Config();
    StormSubmitter.submitTopology(TOPOLOGY_NAME, cfg, builder.createTopology());
  }

  private static KafkaSpout buildKafkaSpout() {
    String zkHostPort = "URL";
    String topic = "TOPIC";

    String zkRoot = "/brokers";
    String zkSpoutId = "monitoring_test_spout_id";
    ZkHosts zkHosts = new ZkHosts(zkHostPort);

    SpoutConfig spoutCfg = new SpoutConfig(zkHosts, topic, zkRoot, zkSpoutId);
    KafkaSpout kafkaSpout = new KafkaSpout(spoutCfg);
    return kafkaSpout;
  }
}

1 个答案:

答案 0 :(得分:2)

您的螺栓没有与喷口链接。你需要使用风暴的分组才能做到这一点..使用类似的东西

    builder.setBolt(REPORT_BOLT_ID, reportBolt).shuffleGrouping(SPOUT_ID);

setBolt通常会返回InputDeclarer个对象。在您的情况下,通过指定shuffleGrouping(SPOUT_ID),您告诉我您有兴趣使用ID为REPORT_BOLT_ID的组件发出的所有元组。

详细了解stream groupings并根据您的需要选择一个。