Trident学习笔记(一)
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学习笔记(一)的更多相关文章
- Trident学习笔记(二)
aggregator ------------------ 聚合动作:聚合操作可以是基于batch.stream.partiton [聚合方式-分区聚合] partitionAggregate 分区聚 ...
- CSS3与页面布局学习笔记(八)——浏览器兼容性问题与前端性能优化方案
一.浏览器兼容 1.1.概要 世界上没有任何一个浏览器是一样的,同样的代码在不一样的浏览器上运行就存在兼容性问题.不同浏览器其内核亦不尽相同,相同内核的版本不同,相同版本的内核浏览器品牌不一样,各种运 ...
- 【学习笔记】移动Web手册(PPK力作)
又是好久没写博客了,最近把近半年的总结,全部总结到博客园吧.先写最近的一个移动端的学习笔记.毕竟移动端开发了一段时间,就写一写读<移动web手册>中,对我感触比较深的几个点—— 一.浏览器 ...
- WebSocket学习笔记IE,IOS,Android等设备的兼容性问
WebSocket学习笔记IE,IOS,Android等设备的兼容性问 一.背景 公司最近准备将一套产品放到Andriod和IOS上面去,为了统一应用的开发方式,决定用各平台APP嵌套一个HTML5浏 ...
- HTML基础学习笔记(1)
HTML学习笔记(1) 1.常用快捷键 win+d---返回桌面 win+e---我的电脑 win+r---打开运行 Alt+tab---切换软件 ctrl+tab---切换软件文档 F2---重命名 ...
- JMeter接口学习笔记2017
协议学习地址:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html 本篇学习笔记来自于慕课网上学习JMeter的学习笔记 学习 ...
- HTTP学习笔记02-HTTP报文格式之概述
HTTP学习笔记02-HTTP报文格式之概述 HTTP学习笔记02-HTTP报文格式之概述 HTTP报文格式 报文的语法 起始行 首部 实体部分 学习一个协议感觉最有意思的就是看包结构…在我看来这是唯 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
随机推荐
- C#实现屏幕指定区域截屏
//string Opath = @"C:/Picture"; //if (Opath.Substring(Opath.Length - 1, 1) != @ ...
- Linux系统错误码对照表
C Name Value Description EPERM 1 Operation not permitted ENOENT 2 No such file or directory ESRCH 3 ...
- 来自NVIDIA开源的pix2pixHD,将Image-to-Image Translation带到了另一个境界
Kuo Ming Lin 分享了 Learning By Hacking 的动图 最近討論最火熱的project之一,來自NVIDIA開源的pix2pixHD,將Image-to-Image Tran ...
- 如何查看某个用户指定时间段的ABAP开发记录
输入用户名和想查询的时间段: 执行得到结果.双击可查看具体代码: 工具源代码: REPORT tool_dev_history. PARAMETERS: name TYPE usr02-bname O ...
- Codeforces 760B Frodo and pillows
题目链接:http://codeforces.com/problemset/problem/760/B 题意:n个床位,m个枕头,第k个位置最多有多少个枕头,其中相邻之间的差<=1; 第k个位置 ...
- 【[USACO17DEC]Standing Out from the Herd】
题目 不会广义\(SAM\)啊 但信仰插入特殊字符就可以搞定一切了 我们先把所有的串搞在一起建出一个\(SAM\),记得在中间插入特殊字符 对于\(parent\)树上的一个节点,只有当其\(endp ...
- tensorflow与android编译
我的过程: 1.下载tensorflow 2.下载ndk.sdk然后放到了tensorflow的目录下 3,修改workspace 4.运行命令:bazel build -c opt //tensor ...
- 剑指offer39 平衡二叉树
剑指上用了指针传递,这里用的引用传递 class Solution { public: bool IsBalanced_Solution(TreeNode* pRoot) { ; return IsB ...
- EF 连接数据库 Mysql (database first ) 一个表对应一个模型
准备工作 1.下载vs2015 2.下载mysql2017 3.安装 1.创建数据库 2. 将数据库映射成模型 3创建aspx 文件. 写下窗体内容的代码 hello_worldEntities en ...
- matlab所需插件
1