简体   繁体   English

在 Storm Bolt 中创建计数器

[英]Create counter in Storm Bolt

I tried to create a counter and count how many times I received the tuple in the bolt.我试图创建一个计数器并计算我在螺栓中收到元组的次数。 But after doing this, :但是在这样做之后,:

public class CounterBolt extends BaseRichBolt {
    OutputCollector outputCollector;
    int count;

    @Override
    public void prepare(Map<String, Object> topoConf, TopologyContext context, OutputCollector collector) {
        outputCollector = collector;
    }

    @Override
    public void execute(Tuple tuple) {
        if (tuple.getSourceStreamId().equals("GotResult")) {
            count++;
        } else
            System.out.println(count); //check count
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {}

I realise that count will become 0 because of new instance of CounterBolt is created every time in Storm topology.我意识到count将变为 0,因为每次在 Storm 拓扑中都会创建CounterBolt的新实例。

An approach that I could think of is using an external storage to do this maybe using a database or message broker like Redis to store the counter.我能想到的一种方法是使用外部存储来执行此操作,可能使用数据库或消息代理(如 Redis)来存储计数器。 My Java knowledge is still not advanced level so is there any proper way to do this?我的 Java 知识还不是高级水平,所以有什么合适的方法可以做到这一点吗?

The right way to have metrics from your topology is to create Metrics consumer, which collects your programmatically defined stats from all Spouts and Bolts of your topology.从拓扑中获取度量的正确方法是创建度量消费者,它从拓扑的所有 Spout 和 Bolt 中收集以编程方式定义的统计信息。 Take look at the description in the docs .看看文档中的描述。 Yes, you need something to store your +1 data, like Redis, MySQL etc. If you are not an experienced programmer take look at the great projects that use Storm like StromCrawler .是的,您需要一些东西来存储您的 +1 数据,例如 Redis、MySQL 等。如果您不是经验丰富的程序员,请查看使用 Storm 的伟大项目,例如StromCrawler This is a cool example of how to create own metrics consumer for storing them in Mysql这是一个很酷的示例,说明如何创建自己的指标使用者以将它们存储在 Mysql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM