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. 整体架构图 ...
随机推荐
- vue-cli -- > 项目基本构建的方法
本文档目的在于让对vue了解比较少的同学,能够快速搭建属于自己的vue项目.(window) 一.构建项目的前提条件 1.确保本机安装了node.js ^6 --> javascript 的服务 ...
- LeetCode OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- 从HTTP响应头判断是否命中CDN
腾讯云: X-Cache-Lookup:Hit From MemCache 表示命中CDN节点的内存X-Cache-Lookup:Hit From Disktank 表示命中CDN节点的磁盘X-Cac ...
- April 28 2017 Week 17 Friday
The only thing more painful than learning from experience is not learning from experience. 比从经验中学习更为 ...
- Cydia Tweak--Cydia Substrate
http://www.jianshu.com/p/8982e9670fc6 Cydia Substrate.MobileHooker MSHookMessageEx MSHookFunction Mo ...
- Linux高性能server编程——定时器
版权声明:本文为博主原创文章.未经博主允许不得转载. https://blog.csdn.net/walkerkalr/article/details/36869913 定时器 服务器程序通常管 ...
- spring使用bean
ApplicationContext 应用上下文,加载Spring 框架配置文件 加载classpath: new ClassPathXmlApplicationContext(“applicatio ...
- centos6.x yum 安装 mysql5.6 mysql5.7
先卸载低版本MYSQL yum remove mysql* rpm -ivh http://repo.mysql.com/mysql-community-release-el6.rpm yum ins ...
- GDB调试手册[转]
Linux 包含了一个叫gdb 的GNU 调试程序.gdb 是一个用来调试C和C++程序的强力调试器.它使你能在程序运行时观察程序的内部结构和内存的使用情况.以下是 gdb 所提供的一些功能:它使你能 ...
- web的监听器,你需要知道这些...
一.简介 Listener是Servlet规范的另一个高级特性,它用于监听java web程序的事件,例如创建.修改.删除session,request,context等,并触发相应的处理事件,这个处 ...