20 篇文章 1 订阅

# 一.min和minby介绍

max、min、sum 会分别返回最大值、最小值和汇总值；而 minBy 和 maxBy 则会把最小或者最大的元素全部返回

Aggregations 为聚合函数的总称，常见的聚合函数包括但不限于 sum、max、min 等。Aggregations 也需要指定一个 key 进行聚合

keyedStream.sum(0);
keyedStream.sum("key");
keyedStream.min(0);
keyedStream.min("key");
keyedStream.max(0);
keyedStream.max("key");
keyedStream.minBy(0);
keyedStream.minBy("key");
keyedStream.maxBy(0);
keyedStream.maxBy("key");


## 二.min和minby介绍代码演示

1.min案例代码

import org.apache.flink.api.common.functions.ReduceFunction;

import java.util.ArrayList;
import java.util.List;

/**
* Author : Jackson
* Version : 2020/4/24 & 1.0
*/

public class ReduceDemo {
public static void main(String[] args) throws Exception {
//获取运行环境的上下文
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//获取数据源
List data = new ArrayList<Tuple3<Integer, Integer, Integer>>();

DataStreamSource<Tuple3<Integer, Integer, Integer>> items = env.fromCollection(data);
items.keyBy(0).min(2).printToErr();

//一定要触发执行，不然没结果输出
env.execute();


6> (0,1,0)
6> (0,1,0)
6> (0,1,0)
6> (0,1,0)
6> (1,2,5)
6> (1,2,5)
6> (1,2,5)
6> (1,2,5)

Process finished with exit code 0


2.minby案例代码

import org.apache.flink.api.common.functions.ReduceFunction;

import java.util.ArrayList;
import java.util.List;

/**
* Author : Jackson
* Version : 2020/4/24 & 1.0
*/

public class ReduceDemo {
public static void main(String[] args) throws Exception {
//获取运行环境的上下文
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//获取数据源
List data = new ArrayList<Tuple3<Integer, Integer, Integer>>();

DataStreamSource<Tuple3<Integer, Integer, Integer>> items = env.fromCollection(data);
items.keyBy(0).minby(2).printToErr();

//一定要触发执行，不然没结果输出
env.execute();


6> (0,1,0)
6> (0,1,0)
6> (0,1,0)
6> (0,1,0)
6> (1,2,5)
6> (1,2,5)
6> (1,2,5)
6> (1,2,5)

Process finished with exit code 0


3.分析对比

    data.add(new Tuple3<>(0, 1, 0));


min结果：

6> (0,1,0)
6> (0,1,0)
6> (0,1,0)
6> (0,1,0)
6> (1,2,5)
6> (1,2,5)
6> (1,2,5)
6> (1,2,5)


minby结果：

6> (0,1,0)
6> (0,1,0)
6> (0,1,0)
6> (0,1,0)
6> (1,2,5)
6> (1,2,5)
6> (1,2,5)
6> (1,2,5)


### 三.同理max和maxby

1.max案例

import org.apache.flink.api.common.functions.ReduceFunction;

import java.util.ArrayList;
import java.util.List;

/**
* Author : Jackson
* Version : 2020/4/24 & 1.0
*/

public class ReduceDemo {
public static void main(String[] args) throws Exception {
//获取运行环境的上下文
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//获取数据源
List data = new ArrayList<Tuple3<Integer, Integer, Integer>>();

DataStreamSource<Tuple3<Integer, Integer, Integer>> items = env.fromCollection(data);
items.keyBy(0).max(2).printToErr();

//一定要触发执行，不然没结果输出
env.execute();


6> (0,1,0)
6> (0,1,1)
6> (0,1,2)
6> (0,1,3)
6> (1,2,5)
6> (1,2,9)
6> (1,2,11)
6> (1,2,13)

Process finished with exit code 0



2.maxby案例

import org.apache.flink.api.common.functions.ReduceFunction;

import java.util.ArrayList;
import java.util.List;

/**
* Author : Jackson
* Version : 2020/4/24 & 1.0
*/

public class ReduceDemo {
public static void main(String[] args) throws Exception {
//获取运行环境的上下文
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//获取数据源
List data = new ArrayList<Tuple3<Integer, Integer, Integer>>();

DataStreamSource<Tuple3<Integer, Integer, Integer>> items = env.fromCollection(data);
items.keyBy(0).max(2).printToErr();

//一定要触发执行，不然没结果输出
env.execute();


6> (0,1,0)
6> (0,1,1)
6> (0,2,2)
6> (0,1,3)
6> (1,2,5)
6> (1,2,9)
6> (1,2,11)
6> (1,2,13)

Process finished with exit code 0


3.分析对比

        data.add(new Tuple3<>(0, 1, 0));



max结果：

6> (0,1,0)
6> (0,1,1)
6> (0,1,2)
6> (0,1,3)
6> (1,2,5)
6> (1,2,9)
6> (1,2,11)
6> (1,2,13)


maxby结果：

6> (0,1,0)
6> (0,1,1)
6> (0,2,2)
6> (0,1,3)
6> (1,2,5)
6> (1,2,9)
6> (1,2,11)
6> (1,2,13)


max、min、sum 会分别返回最大值、最小值和汇总值；而 minBy 和 maxBy 则会把最小或者最大的元素全部返回

min 和 minBy 都会返回整个元素，只是 min 会根据用户指定的字段取最小值，并且把这个值保存在对应的位置，而对于其他的字段，并不能保证其数值正确。max 和 maxBy 同理。

package flink42.day04;

import java.util.ArrayList;
import java.util.List;

/**
* Author : Jackson
* Version : 2020/4/24 & 1.0
*
*/

public class ReduceDemo {
public static void main(String[] args) throws Exception {
//获取运行环境的上下文
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

//获取数据源
List data = new ArrayList<Tuple3<Integer, Integer, Integer>>();

DataStreamSource<Tuple3<Integer, Integer, Integer>> items = env.fromCollection(data);
//reduce
SingleOutputStreamOperator<Tuple3<Integer, Integer, Integer>> reduceres = items.keyBy(0).reduce(new ReduceFunction<Tuple3<Integer, Integer, Integer>>() {
@Override
public Tuple3<Integer, Integer, Integer> reduce(Tuple3<Integer, Integer, Integer> t1, Tuple3<Integer, Integer, Integer> t2) throws Exception {

Tuple3<Integer, Integer, Integer> tuple3 = new Tuple3<>();

tuple3.setFields(0, 0, (Integer) t1.getField(2) + (Integer) t2.getField(2));
return tuple3;
}
});
reduceres.printToErr().setParallelism(1);
env.execute()
}
}


(0,1,0)
(0,0,1)
(0,0,3)
(0,0,6)
(1,2,5)
(0,0,14)
(0,0,25)
(0,0,38)

Process finished with exit code 0

                   ————保持饥饿，保持学习
Jackson_MVP


02-24 779
08-26 186
02-28 268
12-04 378
02-02 3872

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

SuperBigData~

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。