测试Storm的多源头锚定
过程,
Spout 发送msgid 1-10
一级Bolt, msgid1的tuple做为基本组合tuple, 其他8个和一组合, 然后发送给二级Bolt, 同时单个msgid对应的tuple都ack一次,msgid1对象tuple, acker将会跟踪8个二级bolt处理情况.
二级Bolt,发送ack fail(模拟处理失败)
结果:在spout fail下出现msg1-9都失败的情况 .
拓扑代码
package storm.starter; import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.LocalDRPC;
import backtype.storm.StormSubmitter;
import backtype.storm.drpc.DRPCSpout;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.ShellBolt;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.IRichBolt;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.TopologyBuilder;
import backtype.storm.topology.base.BaseBasicBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;
import storm.starter.spout.RandomSentenceSpout; import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator; /**
* This topology demonstrates Storm's stream groupings and multilang
* capabilities.
*/
public class WordCountTopology {
public static String GetThreadName() {
Thread thread = Thread.currentThread();
return thread.getName();
} public static final Logger logger = Logger
.getLogger(WordCountTopology.class); // 切分单词 一级bolt
/*
* public static class SplitSentence extends ShellBolt implements IRichBolt
* { public SplitSentence() { super("python", "splitsentence.py");
* logger.error(GetThreadName() + "SplitSentence create"); }
*
* // 定义字段发送
*
* @Override public void declareOutputFields(OutputFieldsDeclarer declarer)
* { declarer.declare(new Fields("word")); logger.error(GetThreadName() +
* "declarer.declare(new Fields(word))"); }
*
* @Override public Map<String, Object> getComponentConfiguration() {
* logger.error("getComponentConfiguration"); return null; } }
*/
public static class SplitSentence implements IRichBolt {
private OutputCollector _collector; int num = 0;
@Override
public void prepare(Map stormConf, TopologyContext context,
OutputCollector collector) {
_collector = collector;
} private Tuple tuple1;
@Override
public void execute(Tuple tuple) {
String sentence = tuple.getString(0);
if(sentence.equals("a")) {
tuple1 = tuple;
}
else{
List<Tuple> anchors = new ArrayList<Tuple>();
anchors.add(tuple1);
anchors.add(tuple);
_collector.emit(anchors, new Values(sentence + "a"));
_collector.ack(tuple);
_collector.ack(tuple1);
} // for (String word : sentence.split(" ")){
// _collector.emit(tuple, new Values(word));
// }
// num++; System.out.println("Bolt Thread " + Thread.currentThread().getName() + "recve : " + sentence);
System.out.println( num + " bolt recev:" + tuple.getMessageId().getAnchorsToIds());
} @Override
public void cleanup() {
} @Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
} @Override
public Map<String, Object> getComponentConfiguration() {
// TODO Auto-generated method stub
return null;
}
} public static class CountCount1 implements IRichBolt {
Map<String, Integer> counts = new HashMap<String, Integer>();
private OutputCollector _collector;
int num = 0; @Override
public void execute(Tuple tuple) {
String word = tuple.getString(0);
//logger.error(this.toString() + "word = " + word);
Integer count = counts.get(word);
if (count == null)
count = 0; count++;
counts.put(word, count);
num++; _collector.fail(tuple);
//_collector.ack(tuple); //_collector.emit(tuple, new Values(word, count));
} @Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
// logger.error("declareOutputFields :");
declarer.declare(new Fields("result", "count"));
} @Override
public void prepare(Map stormConf, TopologyContext context,
OutputCollector collector) {
// TODO Auto-generated method stub
_collector = collector;
} @Override
public void cleanup() {
// TODO Auto-generated method stub } @Override
public Map<String, Object> getComponentConfiguration() {
// TODO Auto-generated method stub
return null;
}
} public static class WordCount extends BaseBasicBolt {
private OutputCollector _collector;
Map<String, Integer> counts = new HashMap<String, Integer>(); @Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
String word = tuple.getString(0);
//logger.error(this.toString() + "word = " + word);
Integer count = counts.get(word);
if (count == null)
count = 0;
count++;
counts.put(word, count); // <key, list<value, count> >
//logger.error(this.toString() + "count = " + count);
collector.emit(new Values(word, count));
} @Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
// logger.error("declareOutputFields :");
declarer.declare(new Fields("result", "count"));
}
} public static class WordCount1 extends BaseBasicBolt {
Map<String, Integer> counts = new HashMap<String, Integer>(); @Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
// logger.error("WordCount1");
// tuple.getFields()[0];
if (tuple.getFields().contains("result")) {
String count = (String) tuple.getValueByField("result");
// tuple.getValueByField(field)
long countl = -0;// = Long.valueOf(count);
// logger.error(this.toString() + " key = resultkey " + count);
} if (tuple.getFields().contains("count")) {
Integer count = (Integer) tuple.getValueByField("count");
// tuple.getValueByField(field)
long countl = -0;// = Long.valueOf(count);
//logger.error(this.toString() + " key = count " + count);
} // String word = tuple.getString(0);
// logger.error(this.toString() +"word = " + word);
// Integer count = counts.get(word);
// if (count == null)
// count = 0;
// count++;
// counts.put(word, count);
// logger.error(this.toString() + "count = " + count);
// collector.emit(new Values(word, count));
} @Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
// logger.error("declareOutputFields :");
declarer.declare(new Fields("word1", "count1"));
}
} public static void main(String[] args) throws Exception {
TopologyBuilder builder = new TopologyBuilder(); PropertyConfigurator
.configure("/home/hadoop/code1/Kafka/src/Log4j.properties"); // parallelism_hint 代表是executor数量, setNumTasks 代表Tasks数量
builder.setSpout("spout", new RandomSentenceSpout(), 5).setNumTasks(2); builder.setBolt("split", new SplitSentence(), 8).setNumTasks(1).shuffleGrouping("spout");
builder.setBolt("count", new CountCount1(), 12).fieldsGrouping("split",
new Fields("word"));
// builder.setBolt("WordCount1", new WordCount1(), 1).fieldsGrouping(
// "count", new Fields("result", "count")); Config conf = new Config();
conf.setDebug(true);
// 这个设置一个spout task上面最多有多少个没有处理(ack/fail)的tuple,防止tuple队列过大, 只对可靠任务起作用
conf.setMaxSpoutPending(2);
conf.setMessageTimeoutSecs(5); // 消息处理延时
conf.setNumAckers(2); // 消息处理acker System.out.println("args.length = " + args.length);
if (args != null && args.length > 0) {
conf.setNumWorkers(5); // 设置工作进程
StormSubmitter.submitTopology(args[0], conf,
builder.createTopology());
} else {
// 每个组件的最大executor数
conf.setMaxTaskParallelism(1);
conf.setDebug(true); LocalCluster cluster = new LocalCluster();
cluster.submitTopology("word-count", conf, builder.createTopology()); String str = "testdrpc";
// drpc.execute("testdrpc", str); Thread.sleep(1088000);
cluster.shutdown();
}
}
}
spout代码
package storm.starter.spout; 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;
import backtype.storm.utils.Utils; import java.util.Map;
import java.util.Random; import org.apache.log4j.Logger; import storm.starter.WordCountTopology; // IRichSpout
public class RandomSentenceSpout extends BaseRichSpout {
SpoutOutputCollector _collector;
Random _rand; public static final Logger logger = Logger
.getLogger(RandomSentenceSpout.class); @Override
public void open(Map conf, TopologyContext context,
SpoutOutputCollector collector) {
_collector = collector;
_rand = new Random(); WordCountTopology.logger.error(this.toString()
+ "RandomSentenceSpout is create");
} private int num = 0; private String gettmstr() {
StringBuilder tmp = new StringBuilder();
for (int i = 0; i <= num; i++)
tmp.append("a");
num++;
return tmp.toString();
} @Override
public void nextTuple() {
Utils.sleep(200);
// String[] sentences = new String[]{ "the cow jumped over the moon",
// "an apple a day keeps the doctor away",
// "four score and seven years ago", "snow white and the seven dwarfs",
// "i am at two with nature" };
String[] sentences = new String[] { "A" }; String sentence = gettmstr(); // sentences[_rand.nextInt(sentences.length)];
if (num < 10) {
_collector.emit(new Values(sentence), new Integer(num));
// logger.error(this.toString() + "send sentence = " + sentence);
// System.out.println(Thread.currentThread().getName() + " Spout ");
}
} @Override
public void ack(Object id) {
logger.error(this.toString() + "spout ack =" + (Integer)id);
} @Override
public void fail(Object id) {
logger.error("spout fail =" + (Integer)id);
} @Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
} }
运行结果
2014-10-03 21:17:31,149 ERROR (storm.starter.spout.RandomSentenceSpout:71) - spout fail =1
2014-10-03 21:17:31,351 ERROR (storm.starter.spout.RandomSentenceSpout:71) - spout fail =2
Bolt Thread Thread-22recve : aaa
0 bolt recev:{-3139141336114052337=7131499433188364504}
2014-10-03 21:17:31,552 ERROR (storm.starter.spout.RandomSentenceSpout:71) - spout fail =3
Bolt Thread Thread-22recve : aaaa
0 bolt recev:{-4497680640148241887=-615828348570847097}
2014-10-03 21:17:31,754 ERROR (storm.starter.spout.RandomSentenceSpout:71) - spout fail =4
Bolt Thread Thread-22recve : aaaaa
0 bolt recev:{-8878772617767839827=-7708082520013359311}
2014-10-03 21:17:31,957 ERROR (storm.starter.spout.RandomSentenceSpout:71) - spout fail =5
Bolt Thread Thread-22recve : aaaaaa
0 bolt recev:{-3995020874692495577=-5070846720162762196}
2014-10-03 21:17:32,160 ERROR (storm.starter.spout.RandomSentenceSpout:71) - spout fail =6
Bolt Thread Thread-22recve : aaaaaaa
0 bolt recev:{-5994700617723404155=-3738685841476816404}
2014-10-03 21:17:32,362 ERROR (storm.starter.spout.RandomSentenceSpout:71) - spout fail =7
Bolt Thread Thread-22recve : aaaaaaaa
0 bolt recev:{-2308734827213127682=-5719708045753233056}
2014-10-03 21:17:32,563 ERROR (storm.starter.spout.RandomSentenceSpout:71) - spout fail =8
Bolt Thread Thread-22recve : aaaaaaaaa
0 bolt recev:{-3718844156917119468=-6359724009048981605}
2014-10-03 21:17:32,766 ERROR (storm.starter.spout.RandomSentenceSpout:71) - spout fail =9
测试Storm的多源头锚定的更多相关文章
- 测试storm异常信息(时时更新)
Exception in thread "main" java.lang.RuntimeException: org.apache.thrift7.protocol.TProtoc ...
- 安装storm的一些很乱的笔记
下面是自己安装和测试storm的一些笔记,比较乱,后续有时间在整理一篇. storm jar all-my-code.jar com.storm.MyTopology arg1 arg2这个命令会运行 ...
- Storm on Yarn :原理分析+平台搭建
Storm on YARN: Storm on YARN被视为大规模Web应用与传统企业应用之间的桥梁.它将Storm事件处理平台与YARN(Yet Another Resource Negotiat ...
- storm源码之storm代码结构【译】【转】
[原]storm源码之storm代码结构[译] 说明:本文翻译自Storm在GitHub上的官方Wiki中提供的Storm代码结构描述一节Structure of the codebase,希望对正 ...
- 【原】storm源码之storm代码结构【译】
说明:本文翻译自Storm在GitHub上的官方Wiki中提供的Storm代码结构描述一节Structure of the codebase,希望对正在基于Storm进行源码级学习和研究的朋友有所帮助 ...
- storm源码之storm代码结构【译】
storm源码之storm代码结构[译] 说明:本文翻译自Storm在GitHub上的官方Wiki中提供的Storm代码结构描述一节Structure of the codebase,希望对正在基于S ...
- Storm的部署
配置方案如下 node1 Nimbus zookeeper node2 Supervisor zookeeper node3 Supervisor zookeeper node4 Supervisor ...
- Storm开发——环境配置部署
配置开发环境:http://storm.apache.org/releases/current/Setting-up-development-environment.html 开发环境定义: Stor ...
- flume+kafka+storm+mysql架构设计
前段时间学习了storm,最近刚开blog,就把这些资料放上来供大家参考. 这个框架用的组件基本都是最新稳定版本,flume-ng1.4+kafka0.8+storm0.9+mysql (项目是mav ...
随机推荐
- ife task0003学习笔记(三):JavaScript闭包
一.this易错分析 在学习闭包的时候,有一个概念this很重要,关于this的理解,下面3种情况:this指向谁? fn.call(obj1); obj2.fn() fn() 答案是obj1 obj ...
- Nginx启动关闭和重启、文档直接下载不阅览
nginx启动相关 启动:sbin/nginx -c conf/nginx.conf 关闭:sbin/nginx -s stop 重启(重新加载配置文件):sbin/nginx -s reload 检 ...
- NSTimer循环引用的问题
前言: 记得之前看过一个面试题问:ARC环境下的dealloc方法有什么用?问题解答是:代理指针置空,停止定时器timer,注销通知,释放掉实例变量.看着没什么问题,而且网上一收也是大概这样的答案.今 ...
- 移动端H5页面注意事项
1. 单个页面内容不能过多 设计常用尺寸:7501334 / 6401134,包含了手机顶部信号栏的高度. 移动端H5活动页面常常需要能够分享到各种社交App中,常用的有微信.QQ等. 使用移动设备查 ...
- Java版多人聊天室
server.java import java.io.*; import java.net.*; import java.text.SimpleDateFormat; import java.util ...
- C#中TransactionScope的使用方法和原理(摘)
出自51CTO博客:http://cnn237111.blog.51cto.com/2359144/1271600 在.net 1.1的时代,还没有TransactionScope类,因此很多关于事务 ...
- mysql主从复制报错 :Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
在配置mysql主从复制时,想通过 grant replication slave on bbs.* to 'bbs'@'192.168.1.3' identified by '123456'; 来限 ...
- jeecg308 <t:authFilter />标签失效的问题
<%--该标签放到body末尾会无效,估计是js冲突,放到body前好用--%><t:authFilter /> <body></body>
- linux基础-linux和unix的区别
有时候我们对天天使用的Linux指令,只知道怎么用,却分不清概念用法区别,我觉得很有必要整理整理大家熟视无睹的一些linux概念区别. 首先说说unix和linux的区别: linux和unix的最大 ...
- java实例初始化块
实例初始化程序块用于初始化实例数据成员. 它在每次创建类的对象时运行.实例变量的初始化可以是直接的,但是可以在初始化实例初始化块中的实例变量时执行额外的操作. 什么是实例初始化块的使用,我们可以直接分 ...