package com.neusoft.storm;
import java.util.List;
import java.util.Map;
import backtype.storm.spout.SpoutOutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichSpout;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values;
public class SimpleSpout extends BaseRichSpout{//simplesout数据来源
//声明为全局变量
Map map;
TopologyContext context;
SpoutOutputCollector collector;
int sum=0; //定义数字
@Override
public void nextTuple() {
sum++;
List tuple = new Values(sum);//创建tuple
collector.emit(tuple);//
System.out.println("spout"+sum);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//入口类
@Override
public void open(Map map, TopologyContext context, SpoutOutputCollector collector) {
//初始化
this.map =map;
this.context =context;
this.collector =collector;
}
//出口类
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("num"));
//通过num来获取数字的值,相当于声明了一个变量
}
}
package com.neusoft.storm;
import java.util.Map;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Tuple;
public class SimpleBolt extends BaseRichBolt{
//定义全局变量
Map map;
TopologyContext context;
OutputCollector collector;
int sum=0;//来接受累加的值
@Override
public void execute(Tuple input) {
Integer num = input.getIntegerByField("num");//来获取spout的num值
sum+=num;
System.err.println("bolt------sum:"+sum);
//红色打印
}
@Override
public void prepare(Map map, TopologyContext context, OutputCollector collector) {
//初始化
this.map = map;
this.context = context;
this.collector = collector;
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
//这个方法不需要重写,因为上面已经可以完成相加
}
}
package com.neusoft.storm;
import backtype.storm.*;
import backtype.storm.topology.TopologyBuilder;
public class SimpleDemo {
public static void main(String[] args) {
TopologyBuilder builder = new TopologyBuilder();//因为Storm都是拓扑结构
builder.setSpout("simplespout", new SimpleSpout());//需要传一个id,需要把Spout类传入
builder.setBolt("simplebolt", new SimpleBolt()).shuffleGrouping("simplespout");
//将bolt传入
//分组策略---通过id获取spout
LocalCluster cluster = new LocalCluster();//本地提交
cluster.submitTopology("simple", new Config(), builder.createTopology());
//提交(id,配置,拓扑对象)
}
}