use of org.apache.beam.sdk.transforms.PTransform in project beam by apache.
the class ProjectionProducerVisitor method enterCompositeTransform.
@Override
public CompositeBehavior enterCompositeTransform(Node node) {
PTransform<?, ?> transform = node.getTransform();
// TODO(BEAM-13658) Support inputs other than PBegin.
if (!node.getInputs().isEmpty()) {
return CompositeBehavior.DO_NOT_ENTER_TRANSFORM;
}
if (!(transform instanceof ProjectionProducer)) {
return CompositeBehavior.ENTER_TRANSFORM;
}
ProjectionProducer<PTransform<?, ?>> pushdownProjector = (ProjectionProducer<PTransform<?, ?>>) transform;
if (!pushdownProjector.supportsProjectionPushdown()) {
return CompositeBehavior.ENTER_TRANSFORM;
}
ImmutableMap.Builder<PCollection<?>, FieldAccessDescriptor> builder = ImmutableMap.builder();
for (PCollection<?> output : node.getOutputs().values()) {
FieldAccessDescriptor fieldAccess = pCollectionFieldAccess.get(output);
if (fieldAccess != null && !fieldAccess.getAllFields()) {
builder.put(output, fieldAccess);
}
}
Map<PCollection<?>, FieldAccessDescriptor> localOpportunities = builder.build();
if (localOpportunities.isEmpty()) {
return CompositeBehavior.ENTER_TRANSFORM;
}
pushdownOpportunities.put(pushdownProjector, localOpportunities);
// If there are nested PushdownProjector implementations, apply only the outermost one.
return CompositeBehavior.DO_NOT_ENTER_TRANSFORM;
}
use of org.apache.beam.sdk.transforms.PTransform in project beam by apache.
the class FlinkStreamingTransformTranslatorsTest method applyReadSourceTransform.
private Object applyReadSourceTransform(PTransform<?, ?> transform, PCollection.IsBounded isBounded, StreamExecutionEnvironment env) {
FlinkStreamingPipelineTranslator.StreamTransformTranslator<PTransform<?, ?>> translator = getReadSourceTranslator();
FlinkStreamingTranslationContext ctx = new FlinkStreamingTranslationContext(env, PipelineOptionsFactory.create());
Pipeline pipeline = Pipeline.create();
PCollection<String> pc = PCollection.createPrimitiveOutputInternal(pipeline, WindowingStrategy.globalDefault(), isBounded, StringUtf8Coder.of());
pc.setName("output");
Map<TupleTag<?>, PValue> outputs = new HashMap<>();
outputs.put(new TupleTag<>(), pc);
AppliedPTransform<?, ?, ?> appliedTransform = AppliedPTransform.of("test-transform", Collections.emptyMap(), PValues.fullyExpand(outputs), transform, ResourceHints.create(), Pipeline.create());
ctx.setCurrentTransform(appliedTransform);
translator.translateNode(transform, ctx);
return ctx.getInputDataStream(pc).getTransformation();
}
use of org.apache.beam.sdk.transforms.PTransform in project beam by apache.
the class DataflowRunnerTest method testApplyIsScopedToExactClass.
@Test
public void testApplyIsScopedToExactClass() throws IOException {
DataflowPipelineOptions options = buildPipelineOptions();
Pipeline p = Pipeline.create(options);
Create.TimestampedValues<String> transform = Create.timestamped(Arrays.asList(TimestampedValue.of("TestString", Instant.now())));
p.apply(transform);
CompositeTransformRecorder recorder = new CompositeTransformRecorder();
p.traverseTopologically(recorder);
// The recorder will also have seen a Create.Values composite as well, but we can't obtain that
// transform.
assertThat("Expected to have seen CreateTimestamped composite transform.", recorder.getCompositeTransforms(), hasItem(transform));
assertThat("Expected to have two composites, CreateTimestamped and Create.Values", recorder.getCompositeTransforms(), hasItem(Matchers.<PTransform<?, ?>>isA((Class) Create.Values.class)));
}
use of org.apache.beam.sdk.transforms.PTransform in project beam by apache.
the class ReadSourceTranslatorBatch method translateTransform.
@SuppressWarnings("unchecked")
@Override
public void translateTransform(PTransform<PBegin, PCollection<T>> transform, AbstractTranslationContext context) {
AppliedPTransform<PBegin, PCollection<T>, PTransform<PBegin, PCollection<T>>> rootTransform = (AppliedPTransform<PBegin, PCollection<T>, PTransform<PBegin, PCollection<T>>>) context.getCurrentTransform();
BoundedSource<T> source;
try {
source = ReadTranslation.boundedSourceFromTransform(rootTransform);
} catch (IOException e) {
throw new RuntimeException(e);
}
SparkSession sparkSession = context.getSparkSession();
String serializedSource = Base64Serializer.serializeUnchecked(source);
Dataset<Row> rowDataset = sparkSession.read().format(sourceProviderClass).option(BEAM_SOURCE_OPTION, serializedSource).option(DEFAULT_PARALLELISM, String.valueOf(context.getSparkSession().sparkContext().defaultParallelism())).option(PIPELINE_OPTIONS, context.getSerializableOptions().toString()).load();
// extract windowedValue from Row
WindowedValue.FullWindowedValueCoder<T> windowedValueCoder = WindowedValue.FullWindowedValueCoder.of(source.getOutputCoder(), GlobalWindow.Coder.INSTANCE);
Dataset<WindowedValue<T>> dataset = rowDataset.map(RowHelpers.extractWindowedValueFromRowMapFunction(windowedValueCoder), EncoderHelpers.fromBeamCoder(windowedValueCoder));
PCollection<T> output = (PCollection<T>) context.getOutput();
context.putDataset(output, dataset);
}
use of org.apache.beam.sdk.transforms.PTransform in project beam by apache.
the class PTransformTranslation method toProto.
/**
* Translates an {@link AppliedPTransform} into a runner API proto.
*
* <p>Does not register the {@code appliedPTransform} within the provided {@link SdkComponents}.
*/
static RunnerApi.PTransform toProto(AppliedPTransform<?, ?, ?> appliedPTransform, List<AppliedPTransform<?, ?, ?>> subtransforms, SdkComponents components) throws IOException {
RunnerApi.PTransform.Builder transformBuilder = RunnerApi.PTransform.newBuilder();
for (Map.Entry<TupleTag<?>, PValue> taggedInput : appliedPTransform.getInputs().entrySet()) {
checkArgument(taggedInput.getValue() instanceof PCollection, "Unexpected input type %s", taggedInput.getValue().getClass());
transformBuilder.putInputs(toProto(taggedInput.getKey()), components.registerPCollection((PCollection<?>) taggedInput.getValue()));
}
for (Map.Entry<TupleTag<?>, PValue> taggedOutput : appliedPTransform.getOutputs().entrySet()) {
// TODO: Remove gating
if (taggedOutput.getValue() instanceof PCollection) {
checkArgument(taggedOutput.getValue() instanceof PCollection, "Unexpected output type %s", taggedOutput.getValue().getClass());
transformBuilder.putOutputs(toProto(taggedOutput.getKey()), components.registerPCollection((PCollection<?>) taggedOutput.getValue()));
}
}
for (AppliedPTransform<?, ?, ?> subtransform : subtransforms) {
transformBuilder.addSubtransforms(components.getExistingPTransformId(subtransform));
}
transformBuilder.setUniqueName(appliedPTransform.getFullName());
// TODO: Display Data
PTransform<?, ?> transform = appliedPTransform.getTransform();
if (KNOWN_PAYLOAD_TRANSLATORS.containsKey(transform.getClass())) {
FunctionSpec payload = KNOWN_PAYLOAD_TRANSLATORS.get(transform.getClass()).translate(appliedPTransform, components);
transformBuilder.setSpec(payload);
}
return transformBuilder.build();
}
Aggregations