[flink operator] min和minBy的区别

学习摘录,原文来自 https://www.cnblogs.com/ipoo/p/13084151.html

 

解释

官方文档中:

The difference between min and minBy is that min returns the minimum value, whereas minBy returns the element that has the minimum value in this field (same for max and maxBy).

翻译:

min和minBy之间的区别是min返回最小值,而minBy返回在此字段中具有最小值的元素(与max和maxBy相同)。

但是事实上,min与max 也会返回整个元素。

不同的是min会根据指定的字段取最小值,并且把这个值保存在对应的位置上,对于其他的字段取了最先获取的值,不能保证每个元素的数值正确,max同理。

而minBy会返回指定字段取最小值的元素,并且会覆盖指定字段小于当前已找到的最小值元素。maxBy同理。

示例论证

先拿min()与minBy()举例:

取第三个元素的最小值

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //获取数据源
        List data = new ArrayList<Tuple3<Integer,Integer,Integer>>();
        data.add(new Tuple3<>(0,2,2));
        data.add(new Tuple3<>(0,1,1));
        data.add(new Tuple3<>(0,5,6));
        data.add(new Tuple3<>(0,3,5));
        data.add(new Tuple3<>(1,1,9));
        data.add(new Tuple3<>(1,2,8));
        data.add(new Tuple3<>(1,3,10));
        data.add(new Tuple3<>(1,2,9));

        DataStreamSource<Tuple3<Integer,Integer,Integer>> items = env.fromCollection(data);
        items.keyBy(0).min(2).print();
        
        env.execute("defined streaming source");
    }

输出结果:

(0,2,2)
(0,2,1)
(0,2,1)
(0,2,1)
(1,1,9)
(1,1,8)
(1,1,8)
(1,1,8)

可以看到返回的元素第二个字段取的是获取到第一个元素的字段值; 往下找,第二个元素的指定值是最小的,则把这个值保存的对应位置。

接下来再看minBy()的运行结果:

(0,2,2)
(0,1,1)
(0,1,1)
(0,1,1)
(1,1,9)
(1,2,8)
(1,2,8)
(1,2,8)

返回的是指定字段最小值的元素。可以看到元素数值的正确。

当然max(),maxBy同理。

 

注:这里取min的过程,就是min=initValue, if (min < current) { min = current}; 然后输出每次结果。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值