use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class PTransformTranslationTest method toAndFromProto.
@Test
public void toAndFromProto() throws IOException {
SdkComponents components = SdkComponents.create();
RunnerApi.PTransform converted = convert(spec, components);
Components protoComponents = components.toComponents();
// Sanity checks
assertThat(converted.getInputsCount(), equalTo(spec.getTransform().getInputs().size()));
assertThat(converted.getOutputsCount(), equalTo(spec.getTransform().getOutputs().size()));
assertThat(converted.getSubtransformsCount(), equalTo(spec.getChildren().size()));
assertThat(converted.getUniqueName(), equalTo(spec.getTransform().getFullName()));
for (PValue inputValue : spec.getTransform().getInputs().values()) {
PCollection<?> inputPc = (PCollection<?>) inputValue;
protoComponents.getPcollectionsOrThrow(components.registerPCollection(inputPc));
}
for (PValue outputValue : spec.getTransform().getOutputs().values()) {
PCollection<?> outputPc = (PCollection<?>) outputValue;
protoComponents.getPcollectionsOrThrow(components.registerPCollection(outputPc));
}
}
use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class ReplacementOutputsTest method taggedSucceeds.
@Test
public void taggedSucceeds() {
PCollectionTuple original = PCollectionTuple.of(intsTag, ints).and(strsTag, strs).and(moreIntsTag, moreInts);
Map<PValue, ReplacementOutput> replacements = ReplacementOutputs.tagged(original.expand(), PCollectionTuple.of(strsTag, replacementStrs).and(moreIntsTag, moreReplacementInts).and(intsTag, replacementInts));
assertThat(replacements.keySet(), Matchers.<PValue>containsInAnyOrder(replacementStrs, replacementInts, moreReplacementInts));
ReplacementOutput intsReplacement = replacements.get(replacementInts);
ReplacementOutput strsReplacement = replacements.get(replacementStrs);
ReplacementOutput moreIntsReplacement = replacements.get(moreReplacementInts);
assertThat(intsReplacement, equalTo(ReplacementOutput.of(TaggedPValue.of(intsTag, ints), TaggedPValue.of(intsTag, replacementInts))));
assertThat(strsReplacement, equalTo(ReplacementOutput.of(TaggedPValue.of(strsTag, strs), TaggedPValue.of(strsTag, replacementStrs))));
assertThat(moreIntsReplacement, equalTo(ReplacementOutput.of(TaggedPValue.of(moreIntsTag, moreInts), TaggedPValue.of(moreIntsTag, moreReplacementInts))));
}
use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class TransformHierarchy method finishSpecifying.
/**
* Finish specifying any remaining nodes within the {@link TransformHierarchy}. These are {@link
* PValue PValues} that are produced as output of some {@link PTransform} but are never consumed
* as input. These values must still be finished specifying.
*/
private void finishSpecifying() {
for (Entry<PValue, PInput> producerInputEntry : producerInput.entrySet()) {
PValue value = producerInputEntry.getKey();
value.finishSpecifying(producerInputEntry.getValue(), getProducer(value).getTransform());
}
producerInput.clear();
}
use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class TransformHierarchy method finishSpecifyingInput.
/**
* Finish specifying all of the input {@link PValue PValues} of the current {@link
* Node}. Ensures that all of the inputs to the current node have been fully
* specified, and have been produced by a node in this graph.
*/
public void finishSpecifyingInput() {
// Inputs must be completely specified before they are consumed by a transform.
for (PValue inputValue : current.getInputs().values()) {
Node producerNode = getProducer(inputValue);
PInput input = producerInput.remove(inputValue);
inputValue.finishSpecifying(input, producerNode.getTransform());
checkState(producers.get(inputValue) != null, "Producer unknown for input %s", inputValue);
checkState(producers.get(inputValue) != null, "Producer unknown for input %s", inputValue);
}
}
use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class TransformHierarchyTest method visitVisitsAllPushed.
@Test
public void visitVisitsAllPushed() {
TransformHierarchy.Node root = hierarchy.getCurrent();
PBegin begin = PBegin.in(pipeline);
Create.Values<Long> create = Create.of(1L);
Read.Bounded<Long> read = Read.from(CountingSource.upTo(1L));
PCollection<Long> created = PCollection.createPrimitiveOutputInternal(pipeline, WindowingStrategy.globalDefault(), IsBounded.BOUNDED);
SingleOutput<Long, Long> pardo = ParDo.of(new DoFn<Long, Long>() {
@ProcessElement
public void processElement(ProcessContext ctxt) {
ctxt.output(ctxt.element());
}
});
PCollection<Long> mapped = PCollection.createPrimitiveOutputInternal(pipeline, WindowingStrategy.globalDefault(), IsBounded.BOUNDED);
TransformHierarchy.Node compositeNode = hierarchy.pushNode("Create", begin, create);
hierarchy.finishSpecifyingInput();
assertThat(hierarchy.getCurrent(), equalTo(compositeNode));
assertThat(compositeNode.getInputs().entrySet(), Matchers.empty());
assertThat(compositeNode.getTransform(), Matchers.<PTransform<?, ?>>equalTo(create));
// Not yet set
assertThat(compositeNode.getOutputs().entrySet(), Matchers.emptyIterable());
assertThat(compositeNode.getEnclosingNode().isRootNode(), is(true));
TransformHierarchy.Node primitiveNode = hierarchy.pushNode("Create/Read", begin, read);
assertThat(hierarchy.getCurrent(), equalTo(primitiveNode));
hierarchy.finishSpecifyingInput();
hierarchy.setOutput(created);
hierarchy.popNode();
assertThat(primitiveNode.getOutputs().values(), Matchers.<PValue>containsInAnyOrder(created));
assertThat(primitiveNode.getInputs().entrySet(), Matchers.emptyIterable());
assertThat(primitiveNode.getTransform(), Matchers.<PTransform<?, ?>>equalTo(read));
assertThat(primitiveNode.getEnclosingNode(), equalTo(compositeNode));
hierarchy.setOutput(created);
// The composite is listed as outputting a PValue created by the contained primitive
assertThat(compositeNode.getOutputs().values(), Matchers.<PValue>containsInAnyOrder(created));
// The producer of that PValue is still the primitive in which it is first output
assertThat(hierarchy.getProducer(created), equalTo(primitiveNode));
hierarchy.popNode();
TransformHierarchy.Node otherPrimitive = hierarchy.pushNode("ParDo", created, pardo);
hierarchy.finishSpecifyingInput();
hierarchy.setOutput(mapped);
hierarchy.popNode();
final Set<TransformHierarchy.Node> visitedCompositeNodes = new HashSet<>();
final Set<TransformHierarchy.Node> visitedPrimitiveNodes = new HashSet<>();
final Set<PValue> visitedValuesInVisitor = new HashSet<>();
Set<PValue> visitedValues = hierarchy.visit(new PipelineVisitor.Defaults() {
@Override
public CompositeBehavior enterCompositeTransform(TransformHierarchy.Node node) {
visitedCompositeNodes.add(node);
return CompositeBehavior.ENTER_TRANSFORM;
}
@Override
public void visitPrimitiveTransform(TransformHierarchy.Node node) {
visitedPrimitiveNodes.add(node);
}
@Override
public void visitValue(PValue value, TransformHierarchy.Node producer) {
visitedValuesInVisitor.add(value);
}
});
assertThat(visitedCompositeNodes, containsInAnyOrder(root, compositeNode));
assertThat(visitedPrimitiveNodes, containsInAnyOrder(primitiveNode, otherPrimitive));
assertThat(visitedValuesInVisitor, Matchers.<PValue>containsInAnyOrder(created, mapped));
assertThat(visitedValuesInVisitor, equalTo(visitedValues));
}
Aggregations