上代码:

 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. dj 模型层orm-1

    ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...

  2. 张奎师弟参与devexpress chartControl绘图--解决了devexpress的chartControl控件不能添加系列的问题

    using DevExpress.XtraCharts; using System; using System.Collections.Generic; using System.ComponentM ...

  3. c# 抽象类和抽象方法

    参考:http://www.runoob.com/csharp/csharp-polymorphism.html https://zhidao.baidu.com/question/587686949 ...

  4. AngularJS的select设置默认值

    AngularJS的select设置默认值 在使用Angular时候使用select标签时会遇到绑定数据指定默认显示值可这样实现 <!DOCTYPE html> <html ng-a ...

  5. java的静态内部类

    只是一个简单的记录.因为一直排斥java这个东西.java跟c++比是很不错的一个语言,至少内存管理这么麻烦的东西不用操心了.但是和不断崛起的脚本语言比起来,效率差的太多.无论如何做android还是 ...

  6. linux之vim配置及使用示例

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7056193.html linux之vim配置及使用示例 vi的三种模式: 一 ...

  7. 修改linux swap空间的swappiness,降低对硬盘的缓存

    linux 会使用硬盘的一部分做为SWAP分区,用来进行进程调度--进程是正在运行的程序--把当前不用的进程调成‘等待(standby)‘,甚至‘睡眠(sleep)’,一旦要用,再调成‘活动(acti ...

  8. spring-事务管理学习

    Ok,spring的源码学习到了事务这块就大概要告一段落了,后续如果有机会的话,会开启spring-boot的学习.不过目前还是打算把下一段的学习计划放在其他事情上.先对事务这块做一个简要的学习笔记, ...

  9. 主题模型之潜在语义分析(Latent Semantic Analysis)

    主题模型(Topic Models)是一套试图在大量文档中发现潜在主题结构的机器学习模型,主题模型通过分析文本中的词来发现文档中的主题.主题之间的联系方式和主题的发展.通过主题模型可以使我们组织和总结 ...

  10. python之函数2

    Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...