上代码:

 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. java socket之上传文件

    一.功能介绍 该功能主要实现,将客户端的:F:/work/socketSample/filetemp/client/test_client.txt上传到服务端F:/work/socketSample/ ...

  2. 学以致用十-----centos7.2+python3.6+vim8.1+YouCompleteMe

    趟过了之前的坑后,再来安装YouCompleteMe 一.安装依赖包 yum install libXt-devel gtk2-devel yum -y install python-devel ru ...

  3. 【慕课网实战】Spark Streaming实时流处理项目实战笔记一之铭文升级版

    第一章:课程介绍 铭文一级: VMware Fusion Mac上搭建:为了给大家演示如何使用我们的OOTB环境 Hadoop环境:虚拟机,我是远程登录 Mac 那么就不需要使用我们的OOTB环境 V ...

  4. Oracle 数据库 dbConsole 配置笔记

    Oracle安装后,DBConsole有时安装会失败,这时可以通过以下命令恢复: set oracle_sid=orclemca -repos recreateemca -config dbcontr ...

  5. JAVA 面试题及思考

    ==================================== =======学而时习之======================== ===================== 1. p ...

  6. MongoDB、redis、memcached

    mongodb和memcached不是一个范畴内的东西. mongodb     是文档型的非关系型数据库,其优势在于查询功能比较强大,能存储海量数据. memcached,redis mongodb ...

  7. thrift使用总结

    转自 http://blog.csdn.net/qq_27784479/article/details/73250958 Apache Thrift软件框架用于可扩展的跨语言服务开发,简单来说就是RP ...

  8. Hdu3829 Cat VS Dog(最大独立点集)

    Cat VS Dog Problem Description The zoo have N cats and M dogs, today there are P children visiting t ...

  9. [mysql] mysql 查询语句收集

    // 1. 筛选出当天的中奖名单            $where = " date_format(f_ctime,'%Y-%m-%d') = current_date()"; ...

  10. 广搜 poj3278 poj1426 poj3126

    Catch That Cow Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Ja ...