简体   繁体   English

Apache Storm-用另一个螺栓组成一个螺栓

[英]Apache Storm - composing a bolt with another bolt

I am using Apache Storm and I am wondering is it possible to compose a bolt with another bolt like this: 我正在使用Apache Storm,我想知道是否可以用另一个螺栓来组成螺栓,如下所示:

public class MyNewBolt extends BaseRichBolt {
    private MyOldBolt bolt; // MyOldBolt also extends BaseRichBolt

    public MyNewBolt(MyOldBolt bolt) {
        this.bolt = bolt;
    }

    @Override 
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
        bolt.prepare(map, topologyContext, outputCollector);
        //Prepare other stuff
    }

    @Override
    public void execute(Tuple tuple) {
        bolt.execute(tuple);
        bolt.someOtherMethod();
        //Do some other stuff
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        bolt.declareOutputFields(outputFieldsDeclarer);
    }
}

After this I would submit the bolt: 在此之后,我将提交螺栓:

public static void main(String[] args) throws Exception {
                         .
                         .
                         .
    TopologyBuilder builder = new TopologyBuilder();
    BaseRichBolt bolt = new MyNewBolt(new MyOldBolt());
    builder.setBolt("bolt-id", bolt).someGrouping(....);
                         .
                         .
                         .
}

This works when I run it in local mode but would it actually work in remote mode? 当我在本地模式下运行时,此方法有效,但实际上是否在远程模式下运行? How does Storm treat bolt serialization-deserialization in this case? 在这种情况下,Storm如何处理螺栓序列化/反序列化? How many tasks are now created? 现在创建了多少个任务?

I would suggest you take another approach (which coincidentally is the approach I use). 我建议您采用另一种方法(恰好是我使用的方法)。 Instead of treating the bolt as a reusable class, factor the bolt's business logic out into a pure-Java class. 与其将螺栓视为可重用的类,不如将螺栓的业务逻辑分解为纯Java类。 Then if you want to reuse that logic in another bolt, it's much easier to do so. 然后,如果您想在另一个地方重用该逻辑,则这样做会容易得多。

I'll also suggest that you factor all behavior related to a specific stream (knowledge of names, types and order of values in the tuple, etc.) out into another class. 我还将建议您将与特定流有关的所有行为(名称知识,元组中值的类型和顺序等)分解到另一个类中。 The implementation of your bolt classes should be fairly minimal. 您的螺栓类的实现应相当少。

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

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