上代码:

 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小例子的更多相关文章

  1. springmvc入门的第一个小例子

    今天我们探讨一下springmvc,由于是初学,所以简单的了解一下 springmvc的流程,后续会持续更新... 由一个小例子来简单的了解一下 springmvc springmvc是spring框 ...

  2. java即时通信小例子

    学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...

  3. Runtime的几个小例子(含Demo)

    一.什么是runtime(也就是所谓的“运行时”,因为是在运行时实现的.)           1.runtime是一套底层的c语言API(包括很多强大实用的c语言类型,c语言函数);  [runti ...

  4. bootstrap 模态 modal 小例子

    bootstrap 模态 modal  小例子 <html> <head> <meta charset="utf-8" /> <title ...

  5. INI配置文件分析小例子

    随手写个解析INI配置字符串的小例子 带测试 #include <iostream> #include <map> #include <string> #inclu ...

  6. JavaScript小例子:复选框全选

    JavaScript小例子:复选框全选 这只是一个小例子,很简单,但是这个功能还是很常用的: 实现后效果如图: JavaScript代码: <script type="text/jav ...

  7. 【zTree】 zTree使用的 小例子

    使用zTree树不是第一次了  但是 还是翻阅着之前做的 对照着 使用起来比较方便  这里就把小例子列出来   总结一下使用步骤 这样方便下次使用起来方便一点 使用zTree树的步骤: 1.首先  在 ...

  8. js小例子(标签页)

    运用js写的一个小例子,实现点击不同的标签出现不同的内容: <!DOCTYPE html> <html> <head> <meta chaset=" ...

  9. sbrk与brk的使用小例子

    sbrk() 和 brk() - Unix的系统函数   sbrk()和brk() 系统的底层会维护一个位置,通过位置的移动完成内存的分配和回收.映射内存时 以一个内存页作为基本单位.   void* ...

随机推荐

  1. jquery checkbox反复调用attr('checked', true/false)只有第一次生效 Jquery 中 $('obj').attr('checked',true)失效的几种解决方案

    1.$('obj').prop('checked',true) 2. $(':checkbox').each(function(){ this.checked=true; }) 为什么:attr为失效 ...

  2. 关于preg_match() / preg_replace()函数的一点小说明

    int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $ ...

  3. highcharts x轴中文刻度太长换行

    xAxis: { type: 'category', title:null, gridLineWidth: 1, lineColor: "#50ae93", labels: { s ...

  4. .NET 开源GIS项目

    SharpMapSharpMap是一个基于.NET 2.0使用C#开发的Map渲染类库,可以渲染ESRI Shape.PostGIS.MS SQL等格式的GIS数据,通过扩展地图数据Provider, ...

  5. python psutil简单示例

    python psutil简单示例 利用psutil编写简单的检测小脚本 0.安装psutil模块                                                    ...

  6. NoSQL数据库的分布式算法

    本文译自 Distributed Algorithms in NoSQL Databases 系统的可扩展性是推动NoSQL运动发展的的主要理由,包含了分布式系统协调,故障转移,资源管理和许多其他特性 ...

  7. LED点阵书写显示

    LED点阵书写显示屏   摘要:随着时代的发展,数字电子技术已经普及到我们生活,工作,科研,各个领域,而LED显示以其组构方式灵活.显示稳定.功耗低.寿命长.技术成熟.成本低廉等特点在车站.证券所.运 ...

  8. myeclipse6.6+maven跑springside4.1的demo

    1.安装myeclipse6.6 2.myeclipse6.6安装maven 2.1 删除原有maven. 关闭Eclipse程序, 进入MyEclipse插件目录/eclipse/features ...

  9. poj 2155 区间更新 单点查询

    Matrix Time Limit: 3000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class ...

  10. B样条基函数的定义及系数的意义

    原文链接:http://blog.csdn.net/tuqu/article/details/5177405 贝塞尔基函数用作权重.B-样条基函数也一样:但更复杂.但是它有两条贝塞尔基函数所没有的特性 ...