Strom的trident小例子
上代码:
public class TridentFunc {
/**
* 类似于普通的bolt
*/
public static class MyFunction extends BaseFunction{
@Override
public void execute(TridentTuple tuple, TridentCollector collector) {
Integer value = tuple.getIntegerByField("sentence");
System.out.println(value);
}
}
public static void main(String[] args) {
@SuppressWarnings("unchecked")
FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 1, new Values(1),new Values(2));
spout.setCycle(true);//让spout循环发送数据
TridentTopology tridentTopology = new TridentTopology();
tridentTopology.newStream("spoutid",spout)
.each(new Fields("sentence"), new MyFunction(), new Fields(""));
LocalCluster localCluster = new LocalCluster();
String simpleName = TridentFunc.class.getSimpleName();
localCluster.submitTopology(simpleName, new Config(), tridentTopology.build());
//运行结果就是 一直循环打印 1 2 1 2
}
}
多数据源
public class TridentMeger {
/**
* 类似于普通的bolt
*/
public static class MyFunction extends BaseFunction{
@Override
public void execute(TridentTuple tuple, TridentCollector collector) {
Integer value = tuple.getIntegerByField("sentence");
System.out.println(value);
}
}
public static void main(String[] args) {
FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 1, new Values(1),new Values(2));
//spout.setCycle(true);//让spout循环发送数据
TridentTopology tridentTopology = new TridentTopology();
//指定多个数据源,流连接
Stream newStream1 = tridentTopology.newStream("spoutid1",spout);
Stream newStream2 = tridentTopology.newStream("spoutid2",spout);
//tridentTopology.newStream("spoutid",spout) 之前是这种 但是只能有 一个数据源
tridentTopology.merge(newStream1,newStream2)//使用这种就可以有多个数据源.
.each(new Fields("sentence"), new MyFunction(), new Fields(""));
LocalCluster localCluster = new LocalCluster();
String simpleName = TridentMeger.class.getSimpleName();
localCluster.submitTopology(simpleName, new Config(), tridentTopology.build());
}
}
增加过滤器
public class TridentFilter {
/**
* 类似于普通的bolt
*/
public static class MyFunction extends BaseFunction{
@Override
public void execute(TridentTuple tuple, TridentCollector collector) {
Integer value = tuple.getIntegerByField("sentence");
System.out.println(value);
}
}
public static class MyFilter extends BaseFilter{//专门封装了一个Filter功能.
//对数据进行过滤 如果过滤出的数据不要了就false 保留就ture
@Override
public boolean isKeep(TridentTuple tuple) {
Integer value = tuple.getIntegerByField("sentence");
return value%2==0?true:false; //只要偶数不要奇数
}
}
public static void main(String[] args) {
FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 1, new Values(1),new Values(2));
spout.setCycle(true);//让spout循环发送数据
TridentTopology tridentTopology = new TridentTopology();
tridentTopology.newStream("spoutid",spout) //这个地方只能指定一个数据源,如果想指定多个数据源Spout 看TridentMeger.java
.each(new Fields("sentence"), new MyFilter())
.each(new Fields("sentence"), new MyFunction(), new Fields(""));
LocalCluster localCluster = new LocalCluster();
String simpleName = TridentFilter.class.getSimpleName();
localCluster.submitTopology(simpleName, new Config(), tridentTopology.build());
}
}
Strom的trident小例子的更多相关文章
- springmvc入门的第一个小例子
今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...
- java即时通信小例子
学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...
- Runtime的几个小例子(含Demo)
一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.) 1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数); [runti ...
- bootstrap 模态 modal 小例子
bootstrap 模态 modal 小例子 <html> <head> <meta charset="utf-8" /> <title ...
- INI配置文件分析小例子
随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...
- JavaScript小例子:复选框全选
JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...
- 【zTree】 zTree使用的 小例子
使用zTree树不是第一次了 但是 还是翻阅着之前做的 对照着 使用起来比较方便 这里就把小例子列出来 总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤: 1.首先 在 ...
- js小例子(标签页)
运用js写的一个小例子,实现点击不同的标签出现不同的内容: <!DOCTYPE html> <html> <head> <meta chaset=" ...
- sbrk与brk的使用小例子
sbrk() 和 brk() - Unix的系统函数 sbrk()和brk() 系统的底层会维护一个位置,通过位置的移动完成内存的分配和回收.映射内存时 以一个内存页作为基本单位. void* ...
随机推荐
- bootstrap-glyph-customization
http://www.runoob.com/try/demo_source/bootstrap-glyph-customization.htm http://fontawesome.io/icons/
- ActiveMQ -5.9和jms-1.1源码下载
ActiveMQ-5.9和jms-1.1源码下载:见附件
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记六之铭文升级版
铭文一级: 整合Flume和Kafka的综合使用 avro-memory-kafka.conf avro-memory-kafka.sources = avro-sourceavro-memory-k ...
- 一个发送邮件的java类,包含多种发送方法
import java.util.Calendar;import java.util.Date; import java.util.Properties; import javax.mail.Addr ...
- 一个封存Id与状态对应键值的神器,BigInteger的setBit和testBit用法实例
1,首先描述一下应用场景 比如,我们要对菜单做权限,控制不同角色菜单显示与不显示,角色为经理时,我们需要菜单id为 4,7,13,24的菜单显示,别的菜单不显示. 就是说,这时候我们要把4,7,13, ...
- spring-aop代理的生效原理
主要说下spring里aop的生效的原理吧,并不是讲底层的cglib和gdk动态代理. 还是老一套的分析流程,先找到了aop的标签的handler,然后看下在解析这个标签的时候,都干了些什么,其实主要 ...
- 微信小程序-button组件
主要属性: 注:button-hover 默认为{background-color: rgba(0, 0, 0, 0.1); opacity: 0.7;} 效果图: ml: <!--默认的but ...
- docker opencpu R
有一个项目中用到了docker opencpu R,这里把所学的整理下. docker,一个运行容器,搭建一次,以后可以很方便的移植,win7 64下也可以装. opencpu,云端计算,运行R函数和 ...
- PowerDesigner常用功能介绍
PowerDesigner常用功能:1:把SQL脚步导入PowerDesigner打开powerdesigner,选择File --> Reverse Engineer --> Datab ...
- poj1321 DFS
棋盘问题 Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java class n ...