DataStream 전체 윈도우 파티션 처리
DataStream 전체 윈도우 파티션 처리 (Full Window Partition Processing)
이 페이지는 DataStream에서 전체 윈도우 파티션 처리(full window partition processing) API의 사용법을 설명해요. Flink는 이제 keyed 및 non-keyed DataStream 모두를 PartitionWindowedStream으로 직접 변환할 수 있게 해요.
출처: 문서
본문
PartitionWindowedStream은 각 subtask의 모든 레코드를 각각 전체 윈도우(full window)로 수집하는 것을 나타내요. PartitionWindowedStream은 mapPartition, sortPartition, aggregate, reduce 네 가지 API를 지원해요.
참고: 전체 윈도우 파티션 처리의 설계와 구현에 대한 자세한 내용은 제안·설계 문서인 FLIP-380: Support Full Partition Processing On Non-keyed DataStream에서 찾을 수 있어요.
MapPartition
MapPartition은 각 subtask의 모든 레코드를 각각 전체 윈도우로 수집하고, 각 subtask 내에서 주어진 MapPartitionFunction을 사용해 처리하는 것을 나타내요. MapPartitionFunction은 입력의 끝에서 호출돼요.
각 subtask의 요소 합계를 계산하는 예시는 다음과 같아요.
DataStream<Integer> dataStream = //...
PartitionWindowedStream<Integer> partitionWindowedDataStream = dataStream.fullWindowPartition();
DataStream<Integer> resultStream = partitionWindowedDataStream.mapPartition(
new MapPartitionFunction<Integer, Integer>() {
@Override
public void mapPartition(
Iterable<Integer> values, Collector<Integer> out) {
int result = 0;
for (Integer value : values) {
result += value;
}
out.collect(result);
}
}
);
SortPartition
SortPartition은 각 subtask의 모든 레코드를 각각 전체 윈도우로 수집하고, 입력의 끝에서 각 subtask에서 주어진 레코드 비교기(comparator)로 정렬하는 것을 나타내요.
각 subtask에서 tuple의 첫 번째 요소로 레코드를 정렬하는 예시는 다음과 같아요.
DataStream<Tuple2<Integer, Integer>> dataStream = //...
PartitionWindowedStream<Tuple2<Integer, Integer>> partitionWindowedDataStream = dataStream.fullWindowPartition();
DataStream<Integer> resultStream = partitionWindowedDataStream.sortPartition(0, Order.ASCENDING);
Aggregate
Aggregate는 각 subtask의 모든 레코드를 각각 전체 윈도우로 수집하고, 주어진 AggregateFunction을 윈도우의 레코드에 적용하는 것을 나타내요. AggregateFunction은 각 요소에 대해 호출되며, 윈도우 내에서 값을 증분으로 집계해요.
각 subtask의 레코드를 집계하는 예시는 다음과 같아요.
DataStream<Tuple2<Integer, Integer>> dataStream = //...
PartitionWindowedStream<Tuple2<Integer, Integer>> partitionWindowedDataStream = dataStream.fullWindowPartition();
DataStream<Integer> resultStream = partitionWindowedDataStream.aggregate(new AggregateFunction<>{...});
Reduce
Reduce는 파티션의 모든 레코드에 reduce 변환을 적용하는 것을 나타내요. ReduceFunction은 윈도우의 모든 레코드에 대해 호출돼요.
예시는 다음과 같아요.
DataStream<Tuple2<Integer, Integer>> dataStream = //...
PartitionWindowedStream<Tuple2<Integer, Integer>> partitionWindowedDataStream = dataStream.fullWindowPartition();
DataStream<Integer> resultStream = partitionWindowedDataStream.aggregate(new ReduceFunction<>{...});