当前位置:网站首页>Reduce of Flink
Reduce of Flink
2020-11-09 12:17:00 【Little dragon man who can't fly】
Reduce operator : Rolling aggregation of data streams , And return the merged result of each rolling aggregation calculation
Sample environment
java.version: 1.8.x
flink.version: 1.11.1
Sample data source ( Project code cloud download )
Flink System examples And Build development environment and data
Reduce.java
import com.flink.examples.DataSource;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import java.util.List;
/**
* @Description Reduce operator : Rolling aggregation of data streams , And return the merged result of each rolling aggregation calculation
*/
public class Reduce {
/**
* Ergodic set , Partition printing results of each rolling aggregation
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(4);
List<Tuple3<String,String,Integer>> tuple3List = DataSource.getTuple3ToList();
// Be careful : Use Integer When partitioning , It will result in incorrect partition results , convert to String Type output key The output is correct
KeyedStream<Tuple3<String,String,Integer>, String> keyedStream = env.fromCollection(tuple3List).keyBy(new KeySelector<Tuple3<String,String,Integer>, String>() {
@Override
public String getKey(Tuple3<String, String, Integer> tuple3) throws Exception {
//f1 For the gender field , With the same f1 value ( Gender ) partition
return String.valueOf(tuple3.f1);
}
});
SingleOutputStreamOperator<Tuple3<String, String, Integer>> result = keyedStream.reduce(new ReduceFunction<Tuple3<String, String, Integer>>() {
@Override
public Tuple3<String, String, Integer> reduce(Tuple3<String, String, Integer> t0, Tuple3<String, String, Integer> t1) throws Exception {
int totalAge = t0.f2 + t1.f2;
return new Tuple3<>("", t0.f1, totalAge);
}
});
result.print();
env.execute("flink Reduce job");
}
}
Print the results
## explain : Why each parameter of the first data object in each partition has a value , Because the rolling aggregation returns the first data object overlaid forward from the second data object , Start calculating , So the first data object doesn't enter at all reduce Method ;
2> ( Zhang San ,man,20)
2> (,man,49)
2> (,man,79)
4> ( Li Si ,girl,24)
4> (,girl,56)
4> (,girl,74)
版权声明
本文为[Little dragon man who can't fly]所创,转载请带上原文链接,感谢
边栏推荐
- Detailed explanation of [golang] GC
- JVM learning (4) - garbage collector and memory allocation
- Understanding data structures starts with this article~
- 走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
- 用一种简单的方式实现终端文字粘贴板
- How to use function framework to develop large web application
- 配置交换机Trunk接口流量本地优先转发(集群/堆叠)
- 接口测试如何在post请求中传递文件
- 一个简单的能力,决定你是否会学习!
- Chrome browser engine blink & V8
猜你喜欢

Android NDK 开发实战 - 微信公众号二维码检测

Dynamo: a typical distributed system analysis

What really drags you down is sunk costs

New features of Fedora 33 workstation

Using rem, the font size changes when the screen zooms

Method of creating flat panel simulator by Android studio

Shoes? Forecasting stock market trends? Taobao second kill? Python means what you want

嗯,查询滑动窗口最大值的这4种方法不错...

如何保证消息不被重复消费?(如何保证消息消费的幂等性)

配置交换机Trunk接口流量本地优先转发(集群/堆叠)
随机推荐
Reread reconstruction
Configure switch trunk interface traffic local priority forwarding (cluster / stack)
解决IDEA快捷键 Alt+Insert 失效的问题
For and for... In, for each and map and for of
手写Koa.js源码
Oh, my God! Printing log only knows log4j?
Android rights
inet_ Pton () and INET_ Detailed explanation of ntop() function
How to use function framework to develop large web application
Navigation component of Android architecture (2)
After SQL group query, get the first n records of each group
手写Koa.js源码
Where should wild card SSL certificate register and apply
Dynamo: a typical distributed system analysis
Well, these four ways to query the maximum value of sliding window are good
安全(杂记)
JVM学习(五) -执行子系统
零基础IM开发入门(四):什么是IM系统的消息时序一致性?
android studio创建平板模拟器方法
Stack & queue (go) of data structure and algorithm series