Storm并行度编程

package com.uplooking.bigdata.storm.cluster;

import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.StormSubmitter;
import org.apache.storm.generated.StormTopology;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;

/**
 * 主要用于查看storm的并行度设置
 */
public class RemoteParallismSumTopology {
    static class SumNumSpout extends BaseRichSpout {
        private Map conf;
        private TopologyContext context;
        private SpoutOutputCollector collector;
        public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
            this.conf = conf;
            this.context = context;
            this.collector = collector;
        }
        int num = 0;
        public void nextTuple() {
            System.out.println("spout产生的数据:" + num);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            collector.emit(new Values(num++));
        }

        public void declareOutputFields(OutputFieldsDeclarer declarer) {
            declarer.declare(new Fields("num"));
        }
    }
    static class SumNumBolt extends BaseRichBolt {
        private Map conf;
        private TopologyContext context;
        private OutputCollector collector;

        public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
            this.conf = conf;
            this.context = context;
            this.collector = collector;
        }
        int sum = 0;
        public void execute(Tuple tuple) {
            int num = tuple.getIntegerByField("num");
            sum += num;
            System.out.println("bolt累加之后的结果值:" + sum);

        }

        public void declareOutputFields(OutputFieldsDeclarer declarer) {

        }
    }

    public static void main(String[] args) throws Exception {
        TopologyBuilder topologyBuilder = new TopologyBuilder();
        String spoutID = "sumNumSpout_id";
        topologyBuilder.setSpout(spoutID, new SumNumSpout()).setNumTasks(2);
        String boltID = "sumNumBolt_id";
        topologyBuilder.setBolt(boltID, new SumNumBolt()).shuffleGrouping(spoutID).setNumTasks(2);
        StormTopology stormTopology = topologyBuilder.createTopology();

        String topologyName = RemoteParallismSumTopology.class.getSimpleName();
        Config config = new Config();
//        config.setNumWorkers(2);
//        config.setNumAckers(1);//设置系统executor个数
        if(args == null || args.length < 1) {//如果没有参数就在本地运行
            LocalCluster cluster = new LocalCluster();
            cluster.submitTopology(topologyName, config, stormTopology);
        } else {//集群运行
            StormSubmitter.submitTopology(topologyName, config, stormTopology);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值