use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class TransformHierarchy method setOutput.
/**
* Set the output of the current {@link Node}. If the output is new (setOutput has
* not previously been called with it as the parameter), the current node is set as the producer
* of that {@link POutput}.
*
* <p>Also validates the output - specifically, a Primitive {@link PTransform} produces all of
* its outputs, and a Composite {@link PTransform} produces none of its outputs. Verifies that the
* expanded output does not contain {@link PValue PValues} produced by both this node and other
* nodes.
*/
public void setOutput(POutput output) {
for (PValue value : output.expand().values()) {
if (!producers.containsKey(value)) {
producers.put(value, current);
value.finishSpecifyingOutput(current.getFullName(), unexpandedInputs.get(current), current.transform);
}
producerInput.put(value, unexpandedInputs.get(current));
}
output.finishSpecifyingOutput(current.getFullName(), unexpandedInputs.get(current), current.transform);
current.setOutput(output);
}
use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class TransformHierarchy method replaceNode.
public Node replaceNode(Node existing, PInput input, PTransform<?, ?> transform) {
checkNotNull(existing);
checkNotNull(input);
checkNotNull(transform);
checkState(unexpandedInputs.isEmpty(), "Replacing a node when the graph has an unexpanded input. This is an SDK bug.");
Node replacement = new Node(existing.getEnclosingNode(), transform, existing.getFullName(), input);
for (PValue output : existing.getOutputs().values()) {
Node producer = producers.get(output);
boolean producedInExisting = false;
do {
if (producer.equals(existing)) {
producedInExisting = true;
} else {
producer = producer.getEnclosingNode();
}
} while (!producedInExisting && !producer.isRootNode());
if (producedInExisting) {
producers.remove(output);
LOG.debug("Removed producer for value {} as it is part of a replaced composite {}", output, existing.getFullName());
} else {
LOG.debug("Value {} not produced in existing node {}", output, existing.getFullName());
}
}
existing.getEnclosingNode().replaceChild(existing, replacement);
unexpandedInputs.remove(existing);
unexpandedInputs.put(replacement, input);
current = replacement;
return replacement;
}
use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class ReplacementOutputs method tagged.
public static Map<PValue, ReplacementOutput> tagged(Map<TupleTag<?>, PValue> original, POutput replacement) {
Map<TupleTag<?>, TaggedPValue> originalTags = new HashMap<>();
for (Map.Entry<TupleTag<?>, PValue> originalValue : original.entrySet()) {
originalTags.put(originalValue.getKey(), TaggedPValue.of(originalValue.getKey(), originalValue.getValue()));
}
ImmutableMap.Builder<PValue, ReplacementOutput> resultBuilder = ImmutableMap.builder();
Set<TupleTag<?>> missingTags = new HashSet<>(originalTags.keySet());
for (Map.Entry<TupleTag<?>, PValue> replacementValue : replacement.expand().entrySet()) {
TaggedPValue mapped = originalTags.get(replacementValue.getKey());
checkArgument(mapped != null, "Missing original output for Tag %s and Value %s Between original %s and replacement %s", replacementValue.getKey(), replacementValue.getValue(), original, replacement.expand());
resultBuilder.put(replacementValue.getValue(), ReplacementOutput.of(mapped, TaggedPValue.of(replacementValue.getKey(), replacementValue.getValue())));
missingTags.remove(replacementValue.getKey());
}
ImmutableMap<PValue, ReplacementOutput> result = resultBuilder.build();
checkArgument(missingTags.isEmpty(), "Missing replacement for tags %s. Encountered tags: %s", missingTags, result.keySet());
return result;
}
use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class ReplacementOutputsTest method singletonSucceeds.
@Test
public void singletonSucceeds() {
Map<PValue, ReplacementOutput> replacements = ReplacementOutputs.singleton(ints.expand(), replacementInts);
assertThat(replacements, Matchers.<PValue>hasKey(replacementInts));
ReplacementOutput replacement = replacements.get(replacementInts);
Map.Entry<TupleTag<?>, PValue> taggedInts = Iterables.getOnlyElement(ints.expand().entrySet());
assertThat(replacement.getOriginal().getTag(), Matchers.<TupleTag<?>>equalTo(taggedInts.getKey()));
assertThat(replacement.getOriginal().getValue(), equalTo(taggedInts.getValue()));
assertThat(replacement.getReplacement().getValue(), Matchers.<PValue>equalTo(replacementInts));
}
use of org.apache.beam.sdk.values.PValue in project beam by apache.
the class SdkComponentsTest method translatePipeline.
@Test
public void translatePipeline() {
BigEndianLongCoder customCoder = BigEndianLongCoder.of();
PCollection<Long> elems = pipeline.apply(GenerateSequence.from(0L).to(207L));
PCollection<Long> counted = elems.apply(Count.<Long>globally()).setCoder(customCoder);
PCollection<Long> windowed = counted.apply(Window.<Long>into(FixedWindows.of(Duration.standardMinutes(7))).triggering(AfterWatermark.pastEndOfWindow().withEarlyFirings(AfterPane.elementCountAtLeast(19))).accumulatingFiredPanes().withAllowedLateness(Duration.standardMinutes(3L)));
final WindowingStrategy<?, ?> windowedStrategy = windowed.getWindowingStrategy();
PCollection<KV<String, Long>> keyed = windowed.apply(WithKeys.<String, Long>of("foo"));
PCollection<KV<String, Iterable<Long>>> grouped = keyed.apply(GroupByKey.<String, Long>create());
final RunnerApi.Pipeline pipelineProto = SdkComponents.translatePipeline(pipeline);
pipeline.traverseTopologically(new PipelineVisitor.Defaults() {
Set<Node> transforms = new HashSet<>();
Set<PCollection<?>> pcollections = new HashSet<>();
Set<Equivalence.Wrapper<? extends Coder<?>>> coders = new HashSet<>();
Set<WindowingStrategy<?, ?>> windowingStrategies = new HashSet<>();
@Override
public void leaveCompositeTransform(Node node) {
if (node.isRootNode()) {
assertThat("Unexpected number of PTransforms", pipelineProto.getComponents().getTransformsCount(), equalTo(transforms.size()));
assertThat("Unexpected number of PCollections", pipelineProto.getComponents().getPcollectionsCount(), equalTo(pcollections.size()));
assertThat("Unexpected number of Coders", pipelineProto.getComponents().getCodersCount(), equalTo(coders.size()));
assertThat("Unexpected number of Windowing Strategies", pipelineProto.getComponents().getWindowingStrategiesCount(), equalTo(windowingStrategies.size()));
} else {
transforms.add(node);
}
}
@Override
public void visitPrimitiveTransform(Node node) {
transforms.add(node);
}
@Override
public void visitValue(PValue value, Node producer) {
if (value instanceof PCollection) {
PCollection pc = (PCollection) value;
pcollections.add(pc);
addCoders(pc.getCoder());
windowingStrategies.add(pc.getWindowingStrategy());
addCoders(pc.getWindowingStrategy().getWindowFn().windowCoder());
}
}
private void addCoders(Coder<?> coder) {
coders.add(Equivalence.<Coder<?>>identity().wrap(coder));
if (coder instanceof StructuredCoder) {
for (Coder<?> component : ((StructuredCoder<?>) coder).getComponents()) {
addCoders(component);
}
}
}
});
}
Aggregations