Storm的StreamID使用样例(版本1.0.2)
随手尝试了一下StreamID的的用法。留个笔记。
==数据样例==
{
"Address": "小桥镇小桥中学对面",
"CityCode": "511300",
"CountyCode": "511322",
"EnterpriseCode": "YUNDA",
"MailNo": "667748320345",
"Mobile": "183****5451",
"Name": "王***",
"ProvCode": "510000",
"Weight": "39"
}
==拓扑结构==
==程序源码==
<Spout1>
package test; import com.alibaba.fastjson.JSONObject;
import common.constants.Constants;
import common.simulate.DataRandom;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values; import java.util.Map; public class Spout1 extends BaseRichSpout {
private SpoutOutputCollector _collector = null;
private DataRandom _dataRandom = null;
private int _timeInterval = 1000; @Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declareStream("Stream1", new Fields("json"));
declarer.declareStream("Stream2", new Fields("json"));
} @Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
_collector = collector;
_dataRandom = DataRandom.getInstance();
if (conf.containsKey(Constants.SpoutInterval)) {
_timeInterval = Integer.valueOf((String) conf.get(Constants.SpoutInterval));
}
} @Override
public void nextTuple() {
try {
Thread.sleep(_timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
} JSONObject jsonObject = _dataRandom.getRandomExpressData();
System.out.print("[---Spout1---]jsonObject=" + jsonObject + "\n");
_collector.emit("Stream1", new Values(jsonObject.toJSONString()));
_collector.emit("Stream2", new Values(jsonObject.toJSONString()));
}
}
<CountBolt1>
package test; import com.alibaba.fastjson.JSONObject;
import common.constants.Constants;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values; import java.util.HashMap;
import java.util.Map; public class CountBolt1 extends BaseRichBolt {
private OutputCollector _collector = null;
private int taskId = 0;
private Map<String, Integer> _map = new HashMap<>(); @Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declareStream("Stream3", new Fields("company", "count"));
} @Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
_collector = collector;
taskId = context.getThisTaskId();
} @Override
public void execute(Tuple input) {
String str = input.getStringByField("json");
JSONObject jsonObject = JSONObject.parseObject(str);
String company = jsonObject.getString(Constants.EnterpriseCode); int count = 0;
if (_map.containsKey(company)) {
count = _map.get(company);
}
count++;
_map.put(company, count); _collector.emit("Stream3", new Values(company, count));
System.out.print("[---CountBolt1---]" +
"taskId=" + taskId + ", company=" + company + ", count=" + count + "\n");
}
}
<CountBolt2>
package test; import com.alibaba.fastjson.JSONObject;
import common.constants.Constants;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values; import java.util.HashMap;
import java.util.Map;
import java.util.UUID; public class CountBolt2 extends BaseRichBolt {
private OutputCollector _collector = null;
private int _taskId = 0;
private Map<String, Integer> _map = new HashMap<>(); @Override
public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
_collector = outputCollector;
_taskId = topologyContext.getThisTaskId();
} @Override
public void execute(Tuple tuple) {
String str = tuple.getStringByField("json");
JSONObject jsonObject = JSONObject.parseObject(str);
String prov = jsonObject.getString(Constants.ProvCode); int count = 0;
if (_map.containsKey(prov)) {
count = _map.get(prov);
}
count++;
_map.put(prov, count); _collector.emit("Stream4", new Values(prov, count, UUID.randomUUID()));
System.out.print("[---CountBolt2---]" +
"taskId=" + _taskId + ", prov=" + prov + ", count=" + count + "\n");
} @Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
outputFieldsDeclarer.declareStream("Stream4", new Fields("prov", "count", "random"));
}
}
<CountBolt3>
package test; import com.alibaba.fastjson.JSONObject;
import common.constants.Constants;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values; import java.util.HashMap;
import java.util.Map;
import java.util.UUID; public class CountBolt3 extends BaseRichBolt {
private OutputCollector _collector = null;
private int _taskId = 0;
private Map<String, Integer> _map = new HashMap<>(); @Override
public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
_collector = outputCollector;
_taskId = topologyContext.getThisTaskId();
} @Override
public void execute(Tuple tuple) {
String str = tuple.getStringByField("json"); JSONObject jsonObject = JSONObject.parseObject(str);
String city = jsonObject.getString(Constants.CityCode); int count = 0;
if (_map.containsKey(city)) {
count = _map.get(city);
}
count++;
_map.put(city, count); _collector.emit("Stream4", new Values(city, count));
System.out.print("[---CountBolt3---]" +
"taskId=" + _taskId + ", city=" + city + ", count=" + count + "\n");
} @Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
outputFieldsDeclarer.declareStream("Stream4", new Fields("city", "count"));
}
}
<TopBolt>
package test; import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Tuple; import java.util.List;
import java.util.Map; public class TopBolt extends BaseRichBolt { @Override
public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {
} @Override
public void execute(Tuple tuple) {
System.out.print("[---TopBolt---]StreamID=" + tuple.getSourceStreamId() + "\n");
List<Object> values = tuple.getValues();
for(Object value : values) {
System.out.print("[---TopBolt---]value=" + value + "\n");
}
} @Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
}
}
<TestTopology>
package test; import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.StormSubmitter;
import org.apache.storm.generated.AlreadyAliveException;
import org.apache.storm.generated.AuthorizationException;
import org.apache.storm.generated.InvalidTopologyException;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.tuple.Fields; public class TestTopology {
public static void main(String[] args)
throws InvalidTopologyException, AuthorizationException, AlreadyAliveException {
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("Spout1", new Spout1());
builder.setBolt("Count1", new CountBolt1()).shuffleGrouping("Spout1", "Stream1");
builder.setBolt("Count2", new CountBolt2()).shuffleGrouping("Spout1", "Stream2");
builder.setBolt("Count3", new CountBolt3()).shuffleGrouping("Spout1", "Stream2");
builder.setBolt("Top", new TopBolt())
.fieldsGrouping("Count1", "Stream3", new Fields("company"))
.fieldsGrouping("Count2", "Stream4", new Fields("prov"))
.fieldsGrouping("Count3", "Stream4", new Fields("city")); Config config = new Config();
config.setNumWorkers(1);
config.put(common.constants.Constants.SpoutInterval, args[1]); if (Boolean.valueOf(args[0])) {
StormSubmitter.submitTopology("TestTopology1", config, builder.createTopology());
} else {
LocalCluster localCluster = new LocalCluster();
localCluster.submitTopology("TestTopology1", config, builder.createTopology());
}
}
}
==结果日志==
[---Spout1---]jsonObject={"CityCode":"511300","CountyCode":"511322","Address":"小桥镇小桥中学对面","MailNo":"667748320345","ProvCode":"510000","Mobile":"183****5451","EnterpriseCode":"YUNDA","Weight":"39","Name":"王***"}
[---CountBolt1---]taskId=1, company=YUNDA, count=1
[---CountBolt3---]taskId=3, city=511300, count=1
[---CountBolt2---]taskId=2, prov=510000, count=1
[---TopBolt---]StreamID=Stream4
[---TopBolt---]value=510000
[---TopBolt---]value=1
[---TopBolt---]value=99bd1cdb-d5c1-4ac8-b1a1-a4cfffb5a616
[---TopBolt---]StreamID=Stream4
[---TopBolt---]value=511300
[---TopBolt---]value=1
[---TopBolt---]StreamID=Stream3
[---TopBolt---]value=YUNDA
[---TopBolt---]value=1
Storm的StreamID使用样例(版本1.0.2)的更多相关文章
- spark mllib lda 中文分词、主题聚合基本样例
github https://github.com/cclient/spark-lda-example spark mllib lda example 官方示例较为精简 在官方lda示例的基础上,给合 ...
- PAT 1024 科学计数法 (20)(精简版代码+思路+推荐测试样例)
1024 科学计数法 (20)(20 分) 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+, ...
- cmake使用演示样例与整理总结
本文代码托管于github cmake_demo cmake中一些提前定义变量 PROJECT_SOURCE_DIR project的根文件夹 PROJECT_BINARY_DIR 执行cmake命 ...
- C++的性能C#的产能?! - .Net Native 系列《三》:.NET Native部署测试方案及样例
之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...
- Android OpenCV样例调试+报错处理
1.OpenCV样例调试:<OpenCV Sample - image-manipulations> blog+报错:E/CAMERA_ACTIVITY(17665): Cam ...
- jbpm入门样例
1. jBPM的简介 jBPM是JBOSS下的一个开源java工作流项目,该项目提供eclipse插件,基于Hibernate实现数据持久化存储. 參考 http://www.jbos ...
- java cglib动态代理原理及样例
cglib动态代理: http://blog.csdn.net/xiaohai0504/article/details/6832990 一.原理 代理为控制要访问的目标对象提供了一种途径.当访问 ...
- WebGL自学教程——WebGL演示样例:開始
最终開始WebGL的演示样例了,...... 開始 使用WebGL的步骤,非常easy: 1. 获得WebGL的渲染环境(也叫渲染上下文). 2. 发挥你的想象力,利用<WebGL參考手冊> ...
- PHP初学者如何搭建环境,并在本地服务器(or云端服务器)运行自己的第一个PHP样例
页面底部有PHP代码样例供测试使用. 1.PHP开发,你需要什么? 1)开发代码的工具,可以用IDE名字叫做phpDesigner.当然也可以临时用记事本代替,记得文件扩展名为.php 2)服务器(本 ...
随机推荐
- 国内DP厂家的相关资料信息
这两天看了国内各个厂家的DSP开发板,总结一下,希望给同样需要的人一点经验和总结. 因为我是做通信系统,所以需要TI公司的C6000系列处理器. 目前C6000系列性能最强的是C66X系列,该系列的C ...
- es6 一些小知识
本人最近被es6感兴趣了,学习一些,以下就是自己总结的一些小知识 1.当你在百度输入"es6"关键字,点击进入es6入门,首先你需要知道怎样学习的顺序,先看第21章Module语法 ...
- python学习(二十) Python 中的比较:is 与 ==
Python 中的比较:is 与 == 在 Python 中会用到对象之间比较,可以用 ==,也可以用 is .但是它们的区别是什么呢? is 比较的是两个实例对象是不是完全相同,它们是不是同一个对象 ...
- css响应式设计
响应式设计是指在不同分辨率的设备中,网页布局可以自适应的调整.这种弹性化的布局使网站在不同设备中的布局都比较合理,可以为不同终端的用户提供更加舒适的界面和更好的用户体验,其根本理念是使原本 PC 上的 ...
- C++ 单链表操作总结
第一.单链表的定义和操作 #include <iostream> using namespace std; template <typename T> struct Node ...
- Asp.net 的cookie问题
写代码的发现的小问题 如果写入cookie的时候指定了domin域名 那么删除的时候必须也指定domin域名才行,即使你先读取了这个已经存在的cookie,但是还是要指定domin才能删除 /// & ...
- 0007-一套完整的CRUD_DEMO
好久没写了,一直在忙别的东西,但是想想,还是把之前的补充完整好了.给大家一个参考,也为自己留个备份. 首先写一个Html作为内容载体,主要结构如下 <div ui-view="navb ...
- Tkinter Spinbox
Python - Tkinter Spinbox: Spinbox小部件是一个标准的Tkinter的Entry小窗口部件的变体,它可以用来选择从一个固定的值. Spinbox小部件是一个标准的Tk ...
- C投票系统
- Python实践练习:口令保管箱
缘由 做中学才是最好的方法,通过这些项目来加强自己的Python掌握程度. 所有练习目录地址 题目描述: 一个字典中存在着账户和密码,通过命令行参数直接执行,查看是否有这个账户. 若有,则复制账户的密 ...