gpt4 book ai didi

apache-flink - KeyedStream 中的 max 和 maxBy 有什么区别

转载 作者:行者123 更新时间:2023-12-03 22:11:14 25 4
gpt4 key购买 nike

  • KeyedStream#max(字符串字段)

  • Applies an aggregation that gives the current maximum of the data stream at the given field expression by the given key. An independent aggregate is kept per key. A field expression is either the name of a public field or a getter method with parentheses of the {@link DataStream}'s underlying type. A dot can be used to drill down into objects, as in {@code "field1.fieldxy" }.


  • KeyedStream#maxBy(String field)

  • Applies an aggregation that gives the current element with the maximum value at the given position by the given key. An independent aggregate is kept per key. If more elements have the maximum value at the given position, the operator returns the first one by default.



    这两个API的javadoc看起来很相似,请问他们有什么区别,什么时候选择这个或那个

    最佳答案

    difference max 和 maxBy 之间是 max 返回最大值,而 maxBy 返回该字段中具有最大值的元素。

     keyedStream.max(0);
    keyedStream.max("key");
    keyedStream.maxBy(0);
    keyedStream.maxBy("key");

    在下面的例子中,我们也可以看到不同之处:

    使用 max :
      // Create a Tumbling Window with the values of 1 day:
    .timeWindow(Time.of(1, TimeUnit.DAYS))
    // Use the max Temperature of the day:
    .max("temperature")
    // And perform an Identity map, because we want to write all values of this day to the Database:
    .map(new MapFunction<elastic.model.LocalWeatherData, elastic.model.LocalWeatherData>() {
    @Override
    public elastic.model.LocalWeatherData map(elastic.model.LocalWeatherData localWeatherData) throws Exception {
    return localWeatherData;
    }
    });

    使用 maxBy :
      // Now take the Maximum Temperature per day from the KeyedStream:
    DataStream<LocalWeatherData> maxTemperaturePerDay =
    localWeatherDataByStation
    // Use non-overlapping tumbling window with 1 day length:
    .timeWindow(Time.days(1))
    // And use the maximum temperature:
    .maxBy("temperature");

    关于apache-flink - KeyedStream 中的 max 和 maxBy 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54216672/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com