这里是一个简单的Storm的Topology代码,它在单机模式下执行,可以用来观察Storm拓扑中各个模块的执行顺序。
使用Storm版本为storm-0.9.x都可以运行,用IDE,比如idea,创建一个Maven项目,然后在pom.xml中添加以下依赖:
<dependency>
<groupId>org.apache.storm</groupId>
<artifactId>storm-core</artifactId>
<version>0.9.3</version>
<scope>compile</scope>
</dependency>
import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.StormSubmitter;
import backtype.storm.spout.SpoutOutputCollector;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.TopologyBuilder;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.topology.base.BaseRichSpout;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;
import backtype.storm.utils.Utils;
import java.util.Map;
import java.util.Random;
/**
* This is a basic example of a Storm topology.
*
* 探究对象的变量在被多个线程共享时,Storm是否已经对它进行同步了?
* 比如ExclamationBolt中的成员变量i,当设置其并行度为13时,其execute方法的打印结果
* 是同一个i值会被打印13次。
*
* 结论:execute()方法中调用的公共方法或共享变量都会被Storm同步。
*/
public class TestCountTopology {
public static class ExclamationBolt extends BaseRichBolt {
private static final long serialVersionUID = 6618890434446310020L;
OutputCollector _collector;
TopologyContext _context;
int i = 0; //这个变量被多个线程共享时,是否被Storm同步了?
@Override
public void prepare(Map conf, TopologyContext context,
OutputCollector collector) {
_collector = collector;
System.out.println("prepare in BOLT is called!");
_context = context;
}
@Override
public void execute(Tuple tuple) {
System.out.println("execute in BOLT is called! " + i);
System.err.println("Worker_port: " + _context.getThisWorkerPort()
+ " tasks: " + _context.getThisWorkerTasks()
+ " task_id: " + _context.getThisTaskId() + " BOLT receive: "
+ new Values(tuple.getString(0)) + " i: " + i);
inc();
_collector.emit(tuple, new Values(tuple.getString(0) + "!!!"));
_collector.ack(tuple);
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
}
private void inc() {
i++;
System.err.println("task_id: " + _context.getThisTaskId() + " inc() was called");
}
}
public static class WordSpout extends BaseRichSpout {
private static final long serialVersionUID = 6962615351294880911L;
SpoutOutputCollector _collector;
int i = 0;
@Override
public void open(Map conf, TopologyContext context,
SpoutOutputCollector collector) {
_collector = collector;
System.err.println("open in SPOUT is called!");
}
@Override
public void nextTuple() {
System.out.println("nextTuple in SPOUT is called! " + i);
Utils.sleep(100);
final String[] words = new String[] { "nathan", "mike", "jackson",
"golda", "bertels" };
final Random rand = new Random();
final String word = words[rand.nextInt(words.length)];
_collector.emit(new Values(word));
System.out.println("SPOUI emit: " + new Values(word) + " i: " + i);
i++;
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
System.out.println("declareOutputFields in SPOUT is called!");
}
public void ack(Object msgId) {
System.out.println("ack in SPOUT is called!");
}
public void fail(Object msgId) {
System.out.println("fail in SPOUT is called!");
}
public void close() {
System.out.println("close in SPOUT is called!");
}
}
public static void main(String[] args) throws Exception {
TopologyBuilder builder = new TopologyBuilder();
// builder.setSpout("word", new TestWordSpout(), 10);
builder.setSpout("word", new WordSpout(), 1);
builder.setBolt("exclaim1", new ExclamationBolt(), 13).shuffleGrouping("word");
//builder.setBolt("exclaim2", new ExclamationBolt(), 3).shuffleGrouping("exclaim1");
Config conf = new Config();
conf.setDebug(false);// TOPOLOGY_DEBUG
conf.setNumWorkers(2);
if (args != null && args.length > 0) {
conf.setNumWorkers(3);// TOPOLOGY_WORKERS
StormSubmitter.submitTopologyWithProgressBar(args[0], conf,
builder.createTopology());
} else {
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("test", conf, builder.createTopology());
Utils.sleep(10000);
cluster.killTopology("test");
cluster.shutdown();
/*
* comment out the cluster.killTopology() and cluster.shutdown()
* methods, to avoid the file deletion error.
*/
}
}
}
这个拓扑由两个节点组成,WordSpout的方法nextTuple会反复被执行,每次随机从words这个字符数组中选出一个单词发送到流中;而ExclamationBolt会从流中读取这个单词,然后在它后面添加三个叹号”!!!”,然后再发送到流中。
比如WordSpout产生单词”nathan”,那么ExclamationBolt就会产生”nathan!!!”。
在上面的代码中,ExclamationBolt这个Bolt的并行度设置为13,这意味着Storm会产生13个线程,每个线程都会反复执行该Bolt的execute方法,在该方法中的变量i的值会被访问,然后自增1。
Q: 由于这个变量会被多个线程共享访问和修改,那么需要我们进行同步吗?
A: 不需要, 运行Topology发现,*ExclamationBolt的execute方法会将接收到的每个单词添加”!!!”后打印13次,变量i也会以相同值被打印13次后才自增1,这说明Storm帮我们完成了同步。