
org.apache.beam.runners.flink.CreateStreamingFlinkView Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.beam.runners.flink;
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.beam.runners.core.construction.ReplacementOutputs;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.coders.CoderRegistry;
import org.apache.beam.sdk.coders.ListCoder;
import org.apache.beam.sdk.runners.AppliedPTransform;
import org.apache.beam.sdk.runners.PTransformOverrideFactory;
import org.apache.beam.sdk.transforms.Combine;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.View.CreatePCollectionView;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.PCollectionView;
import org.apache.beam.sdk.values.PValue;
import org.apache.beam.sdk.values.TupleTag;
/** Flink streaming overrides for various view (side input) transforms. */
class CreateStreamingFlinkView
extends PTransform, PCollection> {
private final PCollectionView view;
public static final String CREATE_STREAMING_FLINK_VIEW_URN =
"beam:transform:flink:create-streaming-flink-view:v1";
public CreateStreamingFlinkView(PCollectionView view) {
this.view = view;
}
@Override
public PCollection expand(PCollection input) {
input
.apply(Combine.globally(new Concatenate()).withoutDefaults())
.apply(CreateFlinkPCollectionView.of(view));
return input;
}
/**
* Combiner that combines {@code T}s into a single {@code List} containing all inputs.
*
* For internal use by {@link CreateStreamingFlinkView}. This combiner requires that the input
* {@link PCollection} fits in memory. For a large {@link PCollection} this is expected to crash!
*
* @param the type of elements to concatenate.
*/
private static class Concatenate extends Combine.CombineFn, List> {
@Override
public List createAccumulator() {
return new ArrayList();
}
@Override
public List addInput(List accumulator, T input) {
accumulator.add(input);
return accumulator;
}
@Override
public List mergeAccumulators(Iterable> accumulators) {
List result = createAccumulator();
for (List accumulator : accumulators) {
result.addAll(accumulator);
}
return result;
}
@Override
public List extractOutput(List accumulator) {
return accumulator;
}
@Override
public Coder> getAccumulatorCoder(CoderRegistry registry, Coder inputCoder) {
return ListCoder.of(inputCoder);
}
@Override
public Coder> getDefaultOutputCoder(CoderRegistry registry, Coder inputCoder) {
return ListCoder.of(inputCoder);
}
}
/**
* Creates a primitive {@link PCollectionView}.
*
* For internal use only by runner implementors.
*
* @param The type of the elements of the input PCollection
* @param The type associated with the {@link PCollectionView} used as a side input
*/
public static class CreateFlinkPCollectionView
extends PTransform>, PCollection>> {
private PCollectionView view;
private CreateFlinkPCollectionView(PCollectionView view) {
this.view = view;
}
public static CreateFlinkPCollectionView of(
PCollectionView view) {
return new CreateFlinkPCollectionView<>(view);
}
@Override
public PCollection> expand(PCollection> input) {
return PCollection.createPrimitiveOutputInternal(
input.getPipeline(), input.getWindowingStrategy(), input.isBounded(), input.getCoder());
}
public PCollectionView getView() {
return view;
}
}
public static class Factory
implements PTransformOverrideFactory<
PCollection, PCollection, CreatePCollectionView> {
public Factory() {}
@Override
public PTransformReplacement, PCollection> getReplacementTransform(
AppliedPTransform<
PCollection, PCollection, CreatePCollectionView>
transform) {
return PTransformReplacement.of(
(PCollection) Iterables.getOnlyElement(transform.getInputs().values()),
new CreateStreamingFlinkView(transform.getTransform().getView()));
}
@Override
public Map mapOutputs(
Map, PValue> outputs, PCollection newOutput) {
return ReplacementOutputs.singleton(outputs, newOutput);
}
}
}