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* ...
随机推荐
- 音频管理器(AudioManager)
MainActivity.java package com.wwj.serviceandboardcast; import android.app.Activity; import android ...
- L1范式和L2范式
正则化(Regularization) 机器学习中几乎都可以看到损失函数后面会添加一个额外项,常用的额外项一般有两种,一般英文称作ℓ1ℓ1-norm和ℓ2ℓ2-norm,中文称作L1正则化和L2正则化 ...
- 公司内部Samba 服务器架设
1.需求 在公司内部打造一个文件管理系统,其作用域仅仅在公司内部,支持在线对文件的修改和保存操作等,同时也要注意权限问题. 2.策划 目前设立四个群组:运维.开发 .测试和普通,当然所对应的对文件的访 ...
- Mysql之数据库操作
数据库操作: 链接数据库: mysql -uroot -p masql -uroot -pmysql 退出数据库: exit/quit/ctrl + d sql语句最后需要分号结尾: 查看时间: ...
- Ubuntu下安装并配置TexStudio
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7069715.html Ubuntu下安装并配置TexStudio Ubunt ...
- Java案例:超市库存管理系统
案例介绍: 模拟真实的库存管理逻辑,完成超市管理系统的日常功能实现,见下图 案例需求分析: 根据案例介绍,我们进行分析,首先需要一个功能菜单,然后输入功能序号后,调用序号对应的功能方法,实现想要的操作 ...
- POJ3666 线性dp_离散化_贪心
POJ3666 线性dp_离散化_贪心 就DP而言这个题不算难,但是难就难在贪心,还有离散化的思想上 题目大意:n个土堆,问你最少移动多少单位的图,可以使得这n个土堆变成单调的 dp[i][j]表示前 ...
- NLTK之WordNet 接口【转】
转自:http://www.cnblogs.com/kaituorensheng/p/3149095.html WordNet是面向语义的英语词典,类似于传统字典.它是NLTK语料库的一部分,可以 ...
- Android-Java-面向对象与面向过程举例
例子一: 面向过程 在生活中的体现: 李四去饭店吃饭,进入风华高档餐饮店后,首先不理服务员,然后冲进厨房,推开厨师,自己开煤气,自己切菜,自己炒菜,自己调料,炒好后自己端出来,然后吃,吃完后 买单 面 ...
- jQuery---ajax---error函数及其参数详解
使用jquery的ajax方法向服务器发送请求的时候,常常需要使用到error函数进行错误信息的处理,本文详细说明了ajax中error函数和函数中各个参数的用法. 一般error函数返回的参数有三个 ...