当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Front end code style practice prettier + eslint + git hook + lint staged
- JVM学习(六)-内存模型和线程
- Fedora 33 Workstation 的新功能
- Android 复选框 以及回显
- Interface tests how to pass files in post requests
- Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
- 10款必装软件,让Windows使用效率飞起!
- Oh, my God! Printing log only knows log4j?
- 使用TreeView树型菜单栏(递归调用数据库自动创建菜单)
- 实现商品CRUD操作
猜你喜欢

Mapstructure detoxifies object mapping

Configure switch trunk interface traffic local priority forwarding (cluster / stack)

【golang】GC详解

理解 OC 中 RunLoop

JVM学习(四)-垃圾回收器和内存分配

Front end code style practice prettier + eslint + git hook + lint staged

How to ensure that messages are not consumed repeatedly? (how to ensure the idempotent of message consumption)

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

Android Development - service application, timer implementation (thread + service)

AI应届生年薪涨到40万了,你现在转行还来得及!
随机推荐
20201107第16课,使用Apache服务部署静态网站;使用Vsftpd服务传输文件
未来中国电信将把云计算服务打造成为中国电信的主业
Impact of libssl on CentOS login
Android NDK 开发实战 - 微信公众号二维码检测
Program life: from Internet addicts to Microsoft, bat and byte offer harvesters
理解 OC 中 RunLoop
In the future, China Telecom will make cloud computing service the main business of China Telecom
JVM学习(六)-内存模型和线程
Pay attention to the request forwarding problem of. Net core
Gather in Beijing! Openi / O 2020 Qizhi Developer Conference enters countdown
Handwriting Koa.js Source code
微信圈子
Reading design patterns adapter patterns
线上服务的FGC问题排查,看这篇就够了!
JVM learning (5) - execution subsystem
Stack & queue (go) of data structure and algorithm series
JVM learning (4) - garbage collector and memory allocation
Configure switch trunk interface traffic local priority forwarding (cluster / stack)
一个简单的能力,决定你是否会学习!
[design pattern] Chapter 4: Builder mode is not so difficult