當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python pyflink WindowedStream.aggregate用法及代碼示例

本文簡要介紹 python 語言中 pyflink.datastream.WindowedStream.aggregate 的用法。

用法:

aggregate(aggregate_function: pyflink.datastream.functions.AggregateFunction, window_function: Union[pyflink.datastream.functions.WindowFunction, pyflink.datastream.functions.ProcessWindowFunction] = None, accumulator_type: pyflink.common.typeinfo.TypeInformation = None, output_type: pyflink.common.typeinfo.TypeInformation = None) → pyflink.datastream.data_stream.DataStream

將給定的窗口函數應用於每個窗口。每次對每個鍵的窗口進行評估時,都會調用窗口函數。窗口函數的輸出被解釋為常規的非窗口流。

使用給定的聚合函數對到達的數據進行增量聚合。這意味著窗口函數在調用時通常隻有一個值要處理。

例子:

>>> class AverageAggregate(AggregateFunction):
...     def create_accumulator(self) -> Tuple[int, int]:
...         return 0, 0
...
...     def add(self, value: Tuple[str, int], accumulator: Tuple[int, int]) \
...             -> Tuple[int, int]:
...         return accumulator[0] + value[1], accumulator[1] + 1
...
...     def get_result(self, accumulator: Tuple[int, int]) -> float:
...         return accumulator[0] / accumulator[1]
...
...     def merge(self, a: Tuple[int, int], b: Tuple[int, int]) -> Tuple[int, int]:
...         return a[0] + b[0], a[1] + b[1]
>>> ds.key_by(lambda x: x[1]) \
...     .window(TumblingEventTimeWindows.of(Time.seconds(5))) \
...     .aggregate(AverageAggregate(),
...                accumulator_type=Types.TUPLE([Types.LONG(), Types.LONG()]),
...                output_type=Types.DOUBLE())

參數:

  • aggregate_function- 用於增量聚合的聚合函數。

  • window_function- 窗口函數。

  • accumulator_type- 聚合函數的內部累加器類型的類型信息。

  • output_type- 窗口函數的結果類型的類型信息。

返回:

將窗口函數應用於窗口的結果的數據流。

版本 1.16.0 中的新函數。

相關用法


注:本文由純淨天空篩選整理自apache.org大神的英文原創作品 pyflink.datastream.WindowedStream.aggregate。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。