Trident学习笔记(二)
aggregator
------------------
聚合动作;聚合操作可以是基于batch、stream、partiton
[聚合方式-分区聚合]
partitionAggregate
分区聚合;基于分区进行聚合运算;作用于分区而不是batch。
mystream.partitionAggregate(new Fields("x"), new Count(), new Fields("count"));
[聚合方式-batch聚合]
aggregate
批次聚合;先将同一batch的所有分区的tuple进行global再分区,将其汇集到一个分区中,再进行聚合运算。
.aggregate(new Fields("a"), new Count(), new Fields("count")); // 批次聚合
聚合函数
[ReducerAggregator]
init();
reduce();
Aggregator
CombinerAggregator
import org.apache.storm.trident.operation.ReducerAggregator;
import org.apache.storm.trident.tuple.TridentTuple; /**
* 自定义sum聚合函数
*/
public class SumReducerAggregator implements ReducerAggregator<Integer> { private static final long serialVersionUID = 1L; @Override
public Integer init() {
return 0;
} @Override
public Integer reduce(Integer curr, TridentTuple tuple) {
return curr + tuple.getInteger(0) + tuple.getInteger(1);
} }
分区聚合
import net.baiqu.shop.report.trident.demo01.PrintFunction;
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.FixedBatchSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values; public class TridentTopologyApp4 { public static void main(String[] args) {
// 创建topology
TridentTopology topology = new TridentTopology(); // 创建spout
FixedBatchSpout testSpout = new FixedBatchSpout(new Fields("a", "b"), 4,
new Values(1, 2),
new Values(2, 3),
new Values(3, 4),
new Values(4, 5)); // 创建流
Stream stream = topology.newStream("testSpout", testSpout);
stream.partitionAggregate(new Fields("a", "b"), new SumReducerAggregator(), new Fields("sum"))
.shuffle().each(new Fields("sum"), new PrintFunction(), new Fields("result")); // 本地提交
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("TridentDemo4", new Config(), topology.build());
} }
[ReducerAggregator]
init();
reduce();
public interface ReducerAggregator<T> extends Serializable {
T init();
T reduce(T curr, TridentTuple tuple);
}
[Aggregator]
描述同ReducerAggregator.
public interface Aggregator<T> extends Operation {
// 开始聚合之间调用,主要用于保存状态。共下面的两个方法使用
T init(Object batchId, TridentCollector collector);
// 迭代batch的每个tuple, 处理每个tuple后更新state的状态。
void aggreate(T val, TridentTuple tuple, TridentCollector collector);
// 所有tuple处理完成后调用,返回单个tuple给每个batch。
void complete(T val, TridentCollector collector);
}
CombinerAggregator
import org.apache.storm.trident.operation.BaseAggregator;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.tuple.TridentTuple;
import org.apache.storm.tuple.Values; import java.io.Serializable; /**
* 批次求和函数
*/
public class SumAggregator extends BaseAggregator<SumAggregator.State> { private static final long serialVersionUID = 1L; static class State implements Serializable {
private static final long serialVersionUID = 1L;
int sum = 0;
} @Override
public SumAggregator.State init(Object batchId, TridentCollector collector) {
return new State();
} @Override
public void aggregate(SumAggregator.State state, TridentTuple tuple, TridentCollector collector) {
state.sum = state.sum + tuple.getInteger(0) + tuple.getInteger(1);
} @Override
public void complete(SumAggregator.State state, TridentCollector collector) {
collector.emit(new Values(state.sum));
} }
批次聚合
package net.baiqu.shop.report.trident.demo04; import net.baiqu.shop.report.trident.demo01.PrintFunction;
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.FixedBatchSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values; public class TridentTopologyApp4 { public static void main(String[] args) {
// 创建topology
TridentTopology topology = new TridentTopology(); // 创建spout
FixedBatchSpout testSpout = new FixedBatchSpout(new Fields("a", "b"), 4,
new Values(1, 2),
new Values(2, 3),
new Values(3, 4),
new Values(4, 5)); // 创建流
Stream stream = topology.newStream("testSpout", testSpout);
stream.aggregate(new Fields("a", "b"), new SumAggregator(), new Fields("sum"))
.shuffle().each(new Fields("sum"), new PrintFunction(), new Fields("result")); // 本地提交
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("TridentDemo4", new Config(), topology.build());
} }
运行结果
PrintFunction:
PrintFunction:
PrintFunction:
......
平均值批次聚合函数
import org.apache.storm.trident.operation.BaseAggregator;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.tuple.TridentTuple;
import org.apache.storm.tuple.Values; import java.io.Serializable; /**
* 批次求平均值函数
*/
public class AvgAggregator extends BaseAggregator<AvgAggregator.State> { private static final long serialVersionUID = 1L; static class State implements Serializable {
private static final long serialVersionUID = 1L;
// 元组值的总和
float sum = 0;
// 元组个数
int count = 0;
} /**
* 初始化状态
*/
@Override
public AvgAggregator.State init(Object batchId, TridentCollector collector) {
return new State();
} /**
* 在state变量中维护状态
*/
@Override
public void aggregate(AvgAggregator.State state, TridentTuple tuple, TridentCollector collector) {
state.count = state.count + 2;
state.sum = state.sum + tuple.getInteger(0) + tuple.getInteger(1);
} /**
* 处理完成所有元组之后,返回一个具有单个值的tuple
*/
@Override
public void complete(AvgAggregator.State state, TridentCollector collector) {
collector.emit(new Values(state.sum / state.count));
} }
批次聚合求平均值
import net.baiqu.shop.report.trident.demo01.PrintFunction;
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.FixedBatchSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values; public class TridentTopologyApp4 { public static void main(String[] args) {
// 创建topology
TridentTopology topology = new TridentTopology(); // 创建spout
FixedBatchSpout testSpout = new FixedBatchSpout(new Fields("a", "b"), 4,
new Values(1, 2),
new Values(2, 3),
new Values(3, 4),
new Values(4, 5)); // 创建流
Stream stream = topology.newStream("testSpout", testSpout);
stream.aggregate(new Fields("a", "b"), new AvgAggregator(), new Fields("avg"))
.shuffle().each(new Fields("avg"), new PrintFunction(), new Fields("result")); // 本地提交
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("TridentDemo4", new Config(), topology.build());
} }
[CombinerAggregator]
在每个partition运行分区聚合,然后再进行global再分区将同一对batch的所有tuple分到一个partition中,最后再这一个partition中进行聚合运算,并产生结果进行输出。
该种方式的网络流量占用少于前两种方式。
public interface CombinerAggregator<T> extents Serializable {
// 在每个tuple上运行,并接受字段值
T init(TridentTuple tuple);
// 合成tuple的值,并输出一个值的tuple
T combine(T val1, T vak2);
// 如果分区不含有tuple,调用该方法.
T zero();
}
合成聚合函数
import clojure.lang.Numbers;
import org.apache.storm.trident.operation.CombinerAggregator;
import org.apache.storm.trident.tuple.TridentTuple; /**
* 合成聚合函数
*/
public class SumCombinerAggregator implements CombinerAggregator<Number> { private static final long serialVersionUID = 1L; @Override
public Number init(TridentTuple tuple) {
return (Number) tuple.getValue(0);
} @Override
public Number combine(Number val1, Number val2) {
return Numbers.add(val1, val2);
} @Override
public Number zero() {
return 0;
} }
topology
import net.baiqu.shop.report.trident.demo01.PrintFunction;
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.FixedBatchSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values; public class TridentTopologyApp4 { public static void main(String[] args) {
// 创建topology
TridentTopology topology = new TridentTopology(); // 创建spout
FixedBatchSpout testSpout = new FixedBatchSpout(new Fields("a", "b"), 4,
new Values(1, 2),
new Values(2, 3),
new Values(3, 4),
new Values(4, 5)); // 创建流
Stream stream = topology.newStream("testSpout", testSpout);
stream.aggregate(new Fields("a", "b"), new SumCombinerAggregator(), new Fields("sum"))
.shuffle().each(new Fields("sum"), new PrintFunction(), new Fields("result")); // 本地提交
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("TridentDemo4", new Config(), topology.build());
} }
输出结果
PrintFunction:
PrintFunction:
PrintFunction:
......







Trident学习笔记(二)的更多相关文章
- WPF的Binding学习笔记(二)
原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...
- [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计
源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...
- JMX学习笔记(二)-Notification
Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...
- java之jvm学习笔记二(类装载器的体系结构)
java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...
- Java IO学习笔记二
Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...
- 《SQL必知必会》学习笔记二)
<SQL必知必会>学习笔记(二) 咱们接着上一篇的内容继续.这一篇主要回顾子查询,联合查询,复制表这三类内容. 上一部分基本上都是简单的Select查询,即从单个数据库表中检索数据的单条语 ...
- NumPy学习笔记 二
NumPy学习笔记 二 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(二) indigo tools
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- Redis学习笔记二 (BitMap算法分析与BitCount语法)
Redis学习笔记二 一.BitMap是什么 就是通过一个bit位来表示某个元素对应的值或者状态,其中的key就是对应元素本身.我们知道8个bit可以组成一个Byte,所以bitmap本身会极大的节省 ...
随机推荐
- Linux命令之添加权限Chmod的使用
chmod是change mode的缩写,是修改文件权限的一个命令: 一个文件分别有三组权限:用户拥有者,用户组 第一个横杆-表示文件,如果是d表示目录.还有可能是l,表示链接. 第一组(rw-)表示 ...
- Spring配置文件详细分析
XML Schema命名空间作用: 1.避免命名冲突,像Java中的package一样 2.将不同作用的标签分门别类(像Spring中的tx命名空间针对事务类的标签,context命名空间针对组件的标 ...
- 对react vd 性能的理解
相信大家都知道react vd的性能是很好的,速度挺快的,真实dom操作很慢的,但是结果完全相反: 后来我就做了个测试,从两个方面去测试,在页面初始渲染1w条数据,react渲染耗时超过了1秒 在12 ...
- 四大CPU体系结构ARM、X86/Atom、MIPS、PowerPC
http://blog.csdn.net/wangjianno2/article/details/52140936 RISC(reduced instruction set computer,精简指令 ...
- Mybatis异常:java.lang.NumberFormatException: For input string: "S"
MyBatis异常日志如下: Caused by: java.lang.NumberFormatException: For input string: "S" at sun.mi ...
- CTS、CLS、CLR分别作何解释?
CTS.CLS.CLR分别作何解释? 答:CTS:通用类型系统.CLS:通用语言规范.CLR:公共语言运行库.
- matlab vs联调
vs 和matlab联调,最近真的把我搞挂了要. 首先,怎么进入联调呢.在vs里先设置一下. vs:tools->attach to process,选择matlab,注意此时matlab一定是 ...
- vue 城市搜索组件
1.实现大致是如下效果 2.搜索组件的页面结构 <template> <div> <div class="search"> ...
- 关于js的严格模式
最近在看你不知道js,补充自己的js基础,加深理解.在读的过程中写点笔记. 严格模式下与非严格模式的区别 . 严格模式是es5新增的,es6是默认为严格模式的!js默认状态下是非严格模式的! 一般 ...
- LeetCode-177:第N高的薪水
第N高的薪水与第二高的薪水,解题思路是一样的,只要对LeetCode-176的SQL做一下变形,便可以满足这题,详见:https://www.cnblogs.com/zouqf/p/10282392. ...