Flink reduce 作用 实例
reduce作用:把2个类型相同的值合并成1个,对组内的所有值连续使用reduce,直到留下最后一个值!
package reduce;import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;/*** @Author you guess* @Date 2020/6/17 20:52* @Version 1.0* @Desc*/
public class DataStreamReduceTest {public static void main(String[] args) throws Exception {StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();DataStreamSource<Tuple3<String, String, Integer>> src1 = env.addSource(new SourceFunction<Tuple3<String, String, Integer>>() {@Overridepublic void run(SourceContext<Tuple3<String, String, Integer>> ctx) throws Exception {ctx.collect(Tuple3.of("Lisi", "Math", 1));ctx.collect(Tuple3.of("Lisi", "English", 2));ctx.collect(Tuple3.of("Lisi", "Chinese", 3));ctx.collect(Tuple3.of("Zhangsan", "Math", 4));ctx.collect(Tuple3.of("Zhangsan", "English", 5));ctx.collect(Tuple3.of("Zhangsan", "Chinese", 6));}@Overridepublic void cancel() {}}, "source1");// src1.print();
// 7> (Zhangsan,Chinese,6)
// 4> (Lisi,Chinese,3)
// 2> (Lisi,Math,1)
// 5> (Zhangsan,Math,4)
// 3> (Lisi,English,2)
// 6> (Zhangsan,English,5)/*** 代码段2*/
// src1.keyBy(0).reduce(new ReduceFunction<Tuple3<String, String, Integer>>() {
// @Override
// public Tuple3<String, String, Integer> reduce(Tuple3<String, String, Integer> value1, Tuple3<String, String, Integer> value2) throws Exception {
// return Tuple3.of(value1.f0, "总分:", value1.f2 + value2.f2);
// }
// }).print();
// 1> (Lisi,Math,1)
// 11> (Zhangsan,Math,4)
// 1> (Lisi,总分:,3)
// 11> (Zhangsan,总分:,9)
// 1> (Lisi,总分:,6)
// 11> (Zhangsan,总分:,15)/*** 代码段3,与代码段2 同义*/src1.keyBy(0).reduce((value1, value2) -> Tuple3.of(value1.f0, "总分:", value1.f2 + value2.f2)).print();
// 1> (Lisi,Math,1)
// 11> (Zhangsan,Math,4)
// 1> (Lisi,总分:,3)
// 11> (Zhangsan,总分:,9)
// 1> (Lisi,总分:,6)
// 11> (Zhangsan,总分:,15)env.execute("Flink DataStreamReduceTest by Java");}}
前面几个aggregation是几个较为特殊的操作,对分组数据进行处理更为通用的方法是使用reduce
算子。
上图展示了reduce
算子的原理:reduce
在按照同一个Key分组的数据流上生效,它接受两个输入,生成一个输出,即两两合一地进行汇总操作,生成一个同类型的新元素。
https://mp.weixin.qq.com/s/2vcKteQIyj31sVrSg1R_2Q
DataStreamSource没有aggregate(min minby max maxby sum等)、reduce操作;
KeyedStream、AllWindowedStream、DataSet有aggregate(min minby max maxby sum等)、reduce操作;
Flink ,Min MinBy Max MaxBy sum实例
flink 1.9.2,java1.8
源码:注意看注释:
/*** Base interface for Reduce functions. Reduce functions combine groups of elements to* a single value, by taking always two elements and combining them into one. Reduce functions* may be used on entire data sets, or on grouped data sets. In the latter case, each group is reduced* individually.** <p>For a reduce functions that work on an entire group at the same time (such as the* MapReduce/Hadoop-style reduce), see {@link GroupReduceFunction}. In the general case,* ReduceFunctions are considered faster, because they allow the system to use more efficient* execution strategies.** <p>The basic syntax for using a grouped ReduceFunction is as follows:* <pre>{@code* DataSet<X> input = ...;** DataSet<X> result = input.groupBy(<key-definition>).reduce(new MyReduceFunction());* }</pre>** <p>Like all functions, the ReduceFunction needs to be serializable, as defined in {@link java.io.Serializable}.** @param <T> Type of the elements that this function processes.*/
@Public
@FunctionalInterface
public interface ReduceFunction<T> extends Function, Serializable {/*** The core method of ReduceFunction, combining two values into one value of the same type.* The reduce function is consecutively applied to all values of a group until only a single value remains.** @param value1 The first value to combine.* @param value2 The second value to combine.* @return The combined value of both input values.** @throws Exception This method may throw exceptions. Throwing an exception will cause the operation* to fail and may trigger recovery.*/T reduce(T value1, T value2) throws Exception;
}
DataSet下:
Flink reduce 作用 实例相关推荐
- java中的string函数_java中string.trim()函数的作用实例及源码
trim()的作用:去掉字符串首尾的空格. public static void main(String arg[]){ String a=" hello world "; Str ...
- java中trim_java中string.trim()函数的作用实例及源码
trim()的作用:去掉字符串首尾的空格. public static void main(String arg[]){ String a=" hello world "; Str ...
- python 高级使用实例_Python中的高级函数map/reduce使用实例
怎么用Python写mapreduce,请举例说明,初学者,请1.lambda # 匿名函数# 基本用法 lambda x: x**2 # 第一个参数,然后是表达式# 也可以使用如下(lambda x ...
- python中变量名后的逗号_Python中逗号的三种作用实例分析
本文实例讲述了Python中逗号的三种作用.分享给大家供大家参考.具体分析如下: 最近研究python 遇到个逗号的问题 一直没弄明白 今天总算搞清楚了 1.逗号在参数传递中的使用: 这种情况不多说 ...
- java parcelable_Android中Parcelable的作用实例解析
这篇文章主要介绍了Android中Parcelable的作用,对于Android初学者有一定的参考学习价值,需要的朋友可以参考下 在android提供了一种类型:Parcel.被用作封装数据的容器,封 ...
- python逗号怎么用_Python中逗号的三种作用实例分析
本文实例讲述了Python中逗号的三种作用.分享给大家供大家参考.具体分析如下: 最近研究python 遇到个逗号的问题 一直没弄明白 今天总算搞清楚了 1.逗号在参数传递中的使用: 这种情况不多说 ...
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
为什么80%的码农都做不了架构师?>>> ECMAScript5标准发布于2009年12月3日,它带来了一些新的,改善现有的Array数组操作的方法.然而,这些新奇的数组方法并没有真 ...
- Flink官网实例:基于DataStream API 实现欺诈检测,完整实现
1.官方文档 https://ci.apache.org/projects/flink/flink-docs-release-1.12/zh/try-flink/datastream_api.html ...
- Flink中GroupWindow和OverWindow各自的作用+window体系+文档阅读方式
GroupWindow和OverWindow各自的作用 Flink Window 作用 完整实例 GroupWindow 对window中的数据按照字段进行分组 完整案例 OverWindow 在整个 ...
最新文章
- 结合项目实例 回顾传统设计模式(九)迭代器模式
- 模仿国外某小哥,做的一个字符串转动态linq表达式 及 部分扩展
- 62.类文件结构(平台无关性、类文件结构)
- ModuleNotFoundError: No module named 'distutils.core'
- 蓝桥杯 历届试题 九宫重排
- “做实体店,已经是绝路了吗?”
- SQL:postgresql点geom转换为经纬度、POINT
- K3s(Kubernetes)环境使用Let‘s Encrypt证书的部署及自动配置https域名-阿里云域名解析管理
- 关系抽取;串联抽取和联合抽取论文总结
- oracle windows系统下卸载oracle 11g和安装oracle 11g
- RapidMiner Studio 设计视图
- 【NLP】4 gensim word2vec库入门——官方手册embeddings和KeyedVectors
- html表格左右布局,css table布局大法,解决你大部分居中、多列等高、左右布局的问题...
- pg、pgadmin安装指导
- 计算机课平时成绩重要吗,离散数学课程平时成绩评定方法的探索与研究
- The request was rejected because the URL contained a potentially malicious String “//“ 报错
- 【CVPR 2021】Knowledge Review:知识蒸馏新解法
- 二、自己需求函数的模块化
- [豆瓣9.2]梯利的西方哲学史为入门级作品中的最佳推荐《西方哲学史-增补修订版》(唯一带索引的版本)...
- svn如何取消某个文件的版本管理_怎样去除SVN中的某个版本之前的所有版本