sideInput()
sideInput()은 파이프라인의 한 단계에서 처리되는 메인 입력 PCollection 외에 추가적인 입력 데이터를 제공하는 방식이다.
파이프라인은 PCollection을 처리해 결과 데이터를 다시 PCollection으로 반환한다. 그런데 PCollection 처리 과정에서 다른 데이터를 참고해야 하는 경우가 있다. 예를 들어, 주문 정보를 처리하며 할인 정보를 참고하는 경우가 있을 수 있다.
추가적인 입력 데이터를 파이프라인 처리 전에 묶어서 새로운 타입의 input을 만들 수도 있겠지만, 각 데이터 처리에서 공용으로 접근해서 참고할 불변의 데이터라면 sideInput을 사용하는 게 낫다. sideinput은 메모리에 한 번만 로드하면 되기에 더 효율적이다.
sideInput()은 두 가지 형태로 제공된다.
- Singleton Side Inputs: 단일 값 형태의 입력 데이터
- asSingleton()
- Iterable Side Inputs: 여러 개의 값을 포함하는 입력 데이터
- asIterable()
- asList()
- asMap()
- asMultimap()
sideInput()은 ParDo 단계에서 사용된다.
(ParDo는 아파치 빔의 가장 일반적인 PTransform 중 하나로, 각 요소를 병렬로 처리한다. 'Parallel Do')
// Singleton Side Inputs
PCollection<String> mainInput = ...
PCollectionView<Integer> sideInput = mainInput.apply(View.asSingleton());
PCollection<String> output = mainInput.apply(ParDo.of(new DoFn<String, String>() {
@ProcessElement
public void processElement(ProcessContext c) {
Integer value = c.sideInput(sideInput);
// use sideInput value
...
}
}).withSideInputs(sideInput));
PCollectionView
PCollection은 파이프라인에서 처리되는 데이터의 기본 단위로, 동일한 유형의 일련의 데이터 묶음을 나타낸다.
PCollectionView는
- PCollection의 전체 혹은 일부 요소에 대한 뷰를 나타낸다.
- 변경 불가능하다. 파이프라인 실행동안에 데이터를 로드하는 게 아니라, 실행 전에 로드한 외부 데이터 소스 또는 이전 파이프라인의 결과이다.
- 각 데이터 요소에 대해 ParDo 변환을 실행하는 동안 공유된다. 값이 한 번만 메모리에 로드되고 캐시되어 사용되기 때문에 성능을 향상시킬 수 있다.
Beam Programming Guide
beam.apache.org
Side input in Apache Beam
Very often dealing with a single PCollection in the pipeline is sufficient. However there are some cases, for instance when one dataset complements another, when several different distributed collections must be joined in order to produce meaningful result
www.waitingforcode.com
'Data engineering' 카테고리의 다른 글
책 <아파치 카프카 애플리케이션 프로그래밍 with 자바> 1 ~ 3장 (5) | 2024.09.04 |
---|