Storm/JStorm之Topology提交过程

咱以一个简单的案例来说明Topology提交流程:

public static void main(String[] args) throws AlreadyAliveException,
            InvalidTopologyException {
        TopologyBuilder builder = new TopologyBuilder();
        builder.setSpout("input", new RandomSentenceSpout(), 2);
        builder.setBolt("bolt_sentence", new SplitSentenceBolt(), 2)
                .shuffleGrouping("input");

        // 本地模式:最主要用来调试用
        LocalCluster cluster = new LocalCluster();
        System.out.println("start wordcount");
        cluster.submitTopology("word count", conf, builder.createTopology());
    }

1.先创建TopologyBuilder对象,用来构建builder
2.设置spout:传递一个spout-id,创建一个IRichSpout类型的对象,并将该对象作为setSpout方法的参数,数字2代表并行度
咱看一下setSpout源码:

 public SpoutDeclarer setSpout(String id, IRichSpout spout, Number parallelism_hint) {

        validateUnusedId(id);
        initCommon(id, spout, parallelism_hint);
        _spouts.put(id, spout);
        return new SpoutGetter(id);
    }

在上面的方法中,
首先:检测输入的id是否唯一,若已经存在将抛出异常
然后:构建ComponentCommon对象并进行相对应的初始化,最后放入到_commons(是TopologyBuilder的类成员变量)
接着:将所有IRichSpout类型的Spout对象,存放到Map

public BoltDeclarer setBolt(String id, IBasicBolt bolt, Number parallelism_hint) {
        return setBolt(id, new BasicBoltExecutor(bolt), parallelism_hint);
}

在该方法中先用BasicBoltExecutor将传入的IBasicBolt对象封装了一下,然后调用的是它的重载方法(为什么要对bolt封装一下呢?因为BasicBoltExecutor中实现了对消息的追踪)那在setBolt的重载方法里是如何处理的呢?
咱看一下这个重载方法的源码:

public BoltDeclarer setBolt(String id, IRichBolt bolt, Number parallelism_hint) {
        validateUnusedId(id);
        initCommon(id, bolt, parallelism_hint);
        _bolts.put(id, bolt);
        return new BoltGetter(id);
}

这个方法和上述的setSpout方法很类似:
首先:检测传入的组件id是否唯一
然后:通过调用initCommon方法生成common对象
接着:将所有IRichBolt类型的Bolt对象存放到Map String, IRichBolt>类型的变量_bolts中
最后:返回BoltDeclarer类型的对象(它其实继承了InputDeclarer接口),这里用来定义bolt的输入

在setSpout和setBolt的方法中都调用了initCommon方法,我们跟句前面介绍可以知道initCommon方法使用来初始化common对象的,然后给类成员变量_commons赋值,下面我们看一下它如何做的:

private void initCommon(String id, IComponent component, Number parallelism) {
        ComponentCommon common = new ComponentCommon();
        //设置消息流的来源及分组方式
        common.set_inputs(new HashMap<GlobalStreamId, Grouping>());
        if (parallelism != null) {
            //设置并行度
            common.set_parallelism_hint(parallelism.intValue());
        } else {
            common.set_parallelism_hint(1);
        }
        Map conf = component.getComponentConfiguration();
        if (conf != null)
            //设置组件的配置参数
            common.set_json_conf(JSONValue.toJSONString(conf));
        _commons.put(id, common);
}

如何上面的注释,在该方法中首先设置消息流的来源以及分组方式,如果并行度没有手动设置则默认为1
然后根据传入的相应的组件获得该组件的配置信息,并将配置信息转换成json类型存放到common对象中
最后将组件id和common对象存放到Map String, ComponentCommon>类型的变量_commons中,(_commons该变量主要用来存放了所有的Bolt和Spout对象)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脑机接口社区

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值