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);
}
}
}
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);
}
}
}