1. Trident入门

Trident

-------------------

 三叉戟

 storm高级抽象,支持有状态流处理;

 好处是确保消费被处理一次;

 以小批次方式处理输入流,得到精准一次性处理 ;

 不再使用bolt,使用functions、aggreates、filters以及states。

 Trident Tuple: trident top的数据模型,trident处理数据的单元;

        每个tuple有预定义的字段列表构成,字段类型可以是byte;

        character,integer,long,float,double,Boolean or byte array。

 Trident functions: 包含修改tuple的业务逻辑,输入的是tuple的字段,输出多个tuple。

import org.apache.storm.trident.operation.BaseFunction;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.tuple.TridentTuple;
import org.apache.storm.tuple.Values; /**
* 求和函数
*/
public class SumFunction extends BaseFunction { @Override
public void execute(TridentTuple input, TridentCollector collector) {
Integer num1 = input.getInteger(0);
Integer num2 = input.getInteger(1);
int sum = num1 + num2;
collector.emit(new Values(sum));
} }

如果tuple有a, b, c, d四个field,只有a和b作为输入传给function,functions会生成新的sum字段,

sum字段和输入的元祖进行合并,生成一个完成tuple,因此,新的tuple的总和字段个数是a, b, c, d, sum。

Trident Filter

--------------------

  1. 描述

  获取字段集合作为输入,输出boolean,如果反悔true,tuple在流中保留,否则删除,

  a, b, c, d, sum是元祖的字段,sum作为输入传递给filter,判断sum是否为偶数,

  如果是偶数,tuple(a, b, c, d, sum)保留,否则tuple删除。

  2. 代码

import org.apache.storm.trident.operation.BaseFilter;
import org.apache.storm.trident.tuple.TridentTuple; /**
* 校验是否是偶数的过滤器
*/
public class CheckEvenFilter extends BaseFilter { @Override
public boolean isKeep(TridentTuple input) {
Integer sum = input.getInteger(0);
if (sum % 2 == 0) {
return true;
}
return false;
} }

Trident projections

--------------------

  1. 描述

   投影操作中,trident值保留在投影中制定的字段,

   x, y, z --> projection(x) --> x

  2. 调用投影的方式

   mystream.project(new fields("x"));

写一个topology

import org.apache.storm.trident.operation.BaseFunction;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.tuple.TridentTuple; public class PrintFunction extends BaseFunction { @Override
public void execute(TridentTuple input, TridentCollector collector) {
Integer sum = input.getInteger(0);
System.out.println(this.getCLass.getSimpleName + ": " + sum);
} }
import com.google.common.collect.ImmutableList;
import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.trident.Stream;
import org.apache.storm.trident.TridentTopology;
import org.apache.storm.trident.testing.FeederBatchSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values; public class TridentTopologyApp { public static void main(String[] args) {
// 创建topology
TridentTopology topology = new TridentTopology(); // 创建spout
FeederBatchSpout testSpout = new FeederBatchSpout(ImmutableList.of("a", "b", "c", "d")); // 创建流
Stream stream = topology.newStream("spout", testSpout);
stream.shuffle().each(new Fields("a", "b"), new SumFunction(), new Fields("sum")).parallelismHint(1)
.shuffle().each(new Fields("sum"), new CheckEvenFilter()).parallelismHint(1)
.shuffle().each(new Fields("sum"), new PrintFunction(), new Fields("xxx")).parallelismHint(1); // 本地提交
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("TridentDemo", new Config(), topology.build()); // 测试数据
testSpout.feed(ImmutableList.of(new Values(1, 2, 3, 4)));
testSpout.feed(ImmutableList.of(new Values(2, 3, 4, 5)));
testSpout.feed(ImmutableList.of(new Values(3, 4, 5, 6)));
testSpout.feed(ImmutableList.of(new Values(4, 5, 6, 7)));
} }

输出结果

SumFunction:,
CheckEvenFilter:
PrintFunction:
SumFunction:,
CheckEvenFilter:
PrintFunction:
SumFunction:,
CheckEvenFilter:
PrintFunction:
SumFunction:,
CheckEvenFilter:
PrintFunction:

加入一个求平均数的函数

import org.apache.storm.trident.operation.BaseFunction;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.tuple.TridentTuple; /**
* 求平均值方法
*/
public class AverageFunction extends BaseFunction { @Override
public void execute(TridentTuple input, TridentCollector collector) {
int a = input.getIntegerByField("a");
int b = input.getIntegerByField("b");
int c = input.getIntegerByField("c");
int d = input.getIntegerByField("d");
int sum = input.getIntegerByField("sum");
float avg = (float) ((a+b+c+d+sum) / 5.0);
System.out.println(this.getClass().getSimpleName() + ": avg = " + avg);
} }
import com.google.common.collect.ImmutableList;
import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.trident.Stream;
import org.apache.storm.trident.TridentTopology;
import org.apache.storm.trident.testing.FeederBatchSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values; public class TridentTopologyApp { public static void main(String[] args) {
// 创建topology
TridentTopology topology = new TridentTopology(); // 创建spout
FeederBatchSpout testSpout = new FeederBatchSpout(ImmutableList.of("a", "b", "c", "d")); // 创建流
Stream stream = topology.newStream("spout", testSpout);
stream.shuffle().each(new Fields("a", "b"), new SumFunction(), new Fields("sum")).parallelismHint(1)
.shuffle().each(new Fields("sum"), new CheckEvenFilter()).parallelismHint(1)
.shuffle().each(new Fields("sum"), new PrintFunction(), new Fields("res")).parallelismHint(1)
.shuffle().each(new Fields("a", "b", "c", "d", "sum"), new AverageFunction(), new Fields("avg")).parallelismHint(1); // 本地提交
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("TridentDemo", new Config(), topology.build()); // 测试数据
testSpout.feed(ImmutableList.of(new Values(1, 2, 3, 4)));
testSpout.feed(ImmutableList.of(new Values(2, 3, 4, 5)));
testSpout.feed(ImmutableList.of(new Values(3, 4, 5, 6)));
testSpout.feed(ImmutableList.of(new Values(4, 5, 6, 7)));
} }

2. Trident聚合函数

分区聚合

import com.google.common.collect.ImmutableList;
import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.trident.Stream;
import org.apache.storm.trident.TridentTopology;
import org.apache.storm.trident.testing.FeederBatchSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values; public class TridentTopologyApp2 { public static void main(String[] args) {
// 创建topology
TridentTopology topology = new TridentTopology(); // 创建spout
FeederBatchSpout testSpout = new FeederBatchSpout(ImmutableList.of("a", "b")); // 创建流
Stream stream = topology.newStream("testSpout", testSpout);
stream.shuffle().each(new Fields("a", "b"), new MyFilter1()).parallelismHint(1)
.global().each(new Fields("a", "b"), new MyFilter2()).parallelismHint(1)
.partitionBy(new Fields("a"))
//.each(new Fields("a", "b"), new MyFunction1(), new Fields("none")).parallelismHint(1)
.partitionAggregate(new Fields("a"), new MyCount(), new Fields("count"))
.each(new Fields("count"), new MyPrintFunction1(), new Fields("xxx")).parallelismHint(1); // 本地提交
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("TridentDemo2", new Config(), topology.build()); // 测试数据
testSpout.feed(ImmutableList.of(new Values(1, 2)));
testSpout.feed(ImmutableList.of(new Values(2, 3)));
testSpout.feed(ImmutableList.of(new Values(2, 4)));
testSpout.feed(ImmutableList.of(new Values(3, 5)));
} }

批次聚合

3. 自定义聚合函数-Sum-SumAsAggregator

Trident学习笔记(一)的更多相关文章

  1. Trident学习笔记(二)

    aggregator ------------------ 聚合动作:聚合操作可以是基于batch.stream.partiton [聚合方式-分区聚合] partitionAggregate 分区聚 ...

  2. CSS3与页面布局学习笔记(八)——浏览器兼容性问题与前端性能优化方案

    一.浏览器兼容 1.1.概要 世界上没有任何一个浏览器是一样的,同样的代码在不一样的浏览器上运行就存在兼容性问题.不同浏览器其内核亦不尽相同,相同内核的版本不同,相同版本的内核浏览器品牌不一样,各种运 ...

  3. 【学习笔记】移动Web手册(PPK力作)

    又是好久没写博客了,最近把近半年的总结,全部总结到博客园吧.先写最近的一个移动端的学习笔记.毕竟移动端开发了一段时间,就写一写读<移动web手册>中,对我感触比较深的几个点—— 一.浏览器 ...

  4. WebSocket学习笔记IE,IOS,Android等设备的兼容性问

    WebSocket学习笔记IE,IOS,Android等设备的兼容性问 一.背景 公司最近准备将一套产品放到Andriod和IOS上面去,为了统一应用的开发方式,决定用各平台APP嵌套一个HTML5浏 ...

  5. HTML基础学习笔记(1)

    HTML学习笔记(1) 1.常用快捷键 win+d---返回桌面 win+e---我的电脑 win+r---打开运行 Alt+tab---切换软件 ctrl+tab---切换软件文档 F2---重命名 ...

  6. JMeter接口学习笔记2017

    协议学习地址:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html 本篇学习笔记来自于慕课网上学习JMeter的学习笔记 学习 ...

  7. HTTP学习笔记02-HTTP报文格式之概述

    HTTP学习笔记02-HTTP报文格式之概述 HTTP学习笔记02-HTTP报文格式之概述 HTTP报文格式 报文的语法 起始行 首部 实体部分 学习一个协议感觉最有意思的就是看包结构…在我看来这是唯 ...

  8. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  9. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

随机推荐

  1. Mercurial (hg)

    附上两个站点: http://z42.readthedocs.org/zh/latest/devtools/hg.html http://bucunzai.net/hginit/ Mercurial( ...

  2. ModuleNotFoundError: No module named 'yaml'

    ModuleNotFoundError: No module named 'yaml' 需要安装 pyyaml 包

  3. leetcode: 树

    1. sum-root-to-leaf-numbers Given a binary tree containing digits from0-9only, each root-to-leaf pat ...

  4. UESTC 757 棋盘

    虽然是水题,但是还是很interesting的.(大概就是我最晚出这个题了... 博弈感觉就是靠yy能力啊.这题是对称性. 最后的必败态是白色格子对称的,一旦对称形成,对手怎么选,跟随就好,对手无法摆 ...

  5. POJ-1469 COURSES---二分图最大匹配--匈牙利算法

    题目链接: https://vjudge.net/problem/POJ-1469 题目大意: 给你p门课程和n个学生,一个学生可以选0门,1门,或者多门课程,现在要求一个由p个学生组成的集合,满足下 ...

  6. 转:adb操作命令详解及大全

    说到 ADB 大家应该都不陌生,即 Android Debug Bridge,Android调试桥,身为 Android 开发的我们,熟练使用 ADB 命令将会大大提升我们的开发效率, ADB 的命令 ...

  7. Spring Security 实现记住我

    开篇一张图,道理全靠悟. 示例如下: 1.    新建Maven项目  remember_me 2.   pom.xml <project xmlns="http://maven.ap ...

  8. 实现接口Controller定义控制器

    实现接口Controller定义控制器 控制器提供访问应用程序的行为,通常通过服务接口定义或注解定义两种方法实现. 控制器解析用户的请求并将其转换为一个模型.在Spring MVC中一个控制器可以包含 ...

  9. Git永久删除commit--[非教程]

    假设当前分支为master,当前的commit情况如下,现在需要删除commit_id_2和commit_id_4: commit_id_1 commit_id_2 commit_id_3 commi ...

  10. Search in Rotated Sorted Array——LeetCode

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...