【问题标题】:WordCount with Guaranteeing-message-processing带有保证消息处理的 WordCount
【发布时间】:2014-05-31 14:58:12
【问题描述】:

我正在尝试运行带有保证消息处理的 WordCount 示例。

只有一个喷口

  1. WSpout - 发出带有 msgID 的随机句子。

还有两个螺栓

  1. SplitSentence - 将句子拆分成单词并通过锚定发出

  2. WordCount - 打印字数。

我想用下面的代码实现的是,当所有单词都计算为一个句子时。必须承认该句子对应的 Spout。

我在最后一个螺栓 WordCount 中确认了 _collector.ack(tuple)。我看到奇怪的是,尽管 ack() 在 WordCount.execute() 被调用,但相应的 WSpout.ack() 没有得到叫。默认超时后总是失败。

我真的不明白代码有什么问题。请帮我理解问题。 任何帮助表示赞赏。

以下是完整的代码。

public class TestTopology {

    public static class WSpout implements IRichSpout {
        SpoutOutputCollector _collector;
    Integer msgID = 0;
    @Override
    public void nextTuple() {
        Random _rand = new Random();
        String[] sentences = new String[] { "There two things benefit",
                " from Storms reliability capabilities",
                "Specifying a link in the",
                " tuple tree is " + "called anchoring",
                " Anchoring is done at ",
                "the same time you emit a " + "new tuple" };

        String message = sentences[_rand.nextInt(sentences.length)];
        _collector.emit(new Values(message), msgID);
        System.out.println(msgID + " " + message);

        msgID++;
    }
    @Override
    public void open(Map conf, TopologyContext context,
            SpoutOutputCollector collector) {
        System.out.println("open");
        _collector = collector;
    }
    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("LINE"));
    }
    @Override
    public void ack(Object msgID) {
        System.out.println("ack ------------------- " + msgID);

    }
    @Override
    public void fail(Object msgID) {
        System.out.println("fail ----------------- " + msgID);

    }
    @Override
    public void activate() {
        // TODO Auto-generated method stub
    }
    @Override
    public void close() {

    }
    @Override
    public void deactivate() {
        // TODO Auto-generated method stub
    }
    @Override
    public Map<String, Object> getComponentConfiguration() {
        // TODO Auto-generated method stub
        return null;
    }
}

public static class SplitSentence extends BaseRichBolt {
    OutputCollector _collector;
    public void prepare(Map conf, TopologyContext context,
            OutputCollector collector) {
        _collector = collector;
    }

    public void execute(Tuple tuple) {
        String sentence = tuple.getString(0);
        for (String word : sentence.split(" ")) {
            System.out.println(word);
            _collector.emit(tuple, new Values(word));
        }
        //_collector.ack(tuple);
    }

    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("word"));
    }
}

public static class WordCount extends BaseBasicBolt {
    Map<String, Integer> counts = new HashMap<String, Integer>();

    @Override
    public void execute(Tuple tuple, BasicOutputCollector collector) {
        System.out.println("WordCount MSGID : " + tuple.getMessageId());
        String word = tuple.getString(0);
        Integer count = counts.get(word);
        if (count == null)
            count = 0;
        count++;
        System.out.println(word + " ===> " + count);
        counts.put(word, count);
        collector.emit(new Values(word, count));
    }
    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("word", "count"));
    }

}

public static void main(String[] args) throws Exception {
    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout("spout", new WSpout(), 2);
    builder.setBolt("split", new SplitSentence(), 2).shuffleGrouping(
            "spout");
    builder.setBolt("count", new WordCount(), 2).fieldsGrouping("split",
            new Fields("word"));
    Config conf = new Config();
    conf.setDebug(true);

    if (args != null && args.length > 0) {
        conf.setNumWorkers(1);
        StormSubmitter.submitTopology(args[0], conf,
                builder.createTopology());
    } else {
        conf.setMaxTaskParallelism(3);

        LocalCluster cluster = new LocalCluster();
        cluster.submitTopology("word-count", conf, builder.createTopology());
        Thread.sleep(10000);
        cluster.shutdown();
    }
}
}

【问题讨论】:

  • 但是您没有在 SplitSentence 和 WordCount 中的任何地方确认。您没有错过示例中的某些内容吗?此外,WordCount 正在发出一个元组,但在拓扑中 WordCount 螺栓之后没有任何内容。
  • 嗨 Micias,代码有最后一个螺栓 WordCount,它基本上扩展了 BaseBasicBolt 类。这样,每个 touple 都会在 execute() 方法结束时自动得到确认。希望我对 BaseBasicBolt 的理解是正确的 ....
  • 确实如此。老实说,我没有意识到这一点,从未使用过 BaseBasicBolt。但是,SplitSentence 扩展了 BaseRichBolt,而不是 BaseBasicBolt ......请参阅我的答案。

标签: java apache-storm


【解决方案1】:

WordCount 扩展了 BaseBasicBolt,它确保元组在那个 BOLT 中自动得到确认,就像您在评论中所说的那样。但是,SplitSentence 扩展了 BaseRichBolt,它需要您手动确认元组。你没有确认,所以元组超时。

【讨论】:

  • 非常感谢...它成功了。正如我在问题陈述中所说,我最后只承认了 WordCount。我曾想过,如果您在螺栓的每一层都锚定,那么只需要在最后一层确认即可确认生成的完整元组树。添加 _ack spliterBolt 解决了这个问题。
  • ...是的,锚定并不意味着所有锚定的元组都立即得到确认。这意味着一旦其中一个失败,所有锚定的元组都会重复。
猜你喜欢
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 2018-07-05
相关资源
最近更新 更多