use of org.apache.beam.sdk.values.PCollectionTuple in project beam by apache.
the class DataflowPipelineTranslatorTest method testTaggedNamesOverridden.
/**
* Test that in translation the name for collections of a multi-output ParDo - a special case
* because the user can name tags - are overridden to be what the Dataflow service expects.
*/
@Test
public void testTaggedNamesOverridden() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
DataflowRunner runner = DataflowRunner.fromOptions(options);
options.setStreaming(false);
DataflowPipelineTranslator translator = DataflowPipelineTranslator.fromOptions(options);
Pipeline pipeline = Pipeline.create(options);
TupleTag<Integer> tag1 = new TupleTag<Integer>("frazzle") {
};
TupleTag<Integer> tag2 = new TupleTag<Integer>("bazzle") {
};
TupleTag<Integer> tag3 = new TupleTag<Integer>() {
};
PCollectionTuple outputs = pipeline.apply(Create.of(3)).apply(ParDo.of(new DoFn<Integer, Integer>() {
@ProcessElement
public void drop() {
}
}).withOutputTags(tag1, TupleTagList.of(tag2).and(tag3)));
outputs.get(tag1).setName("bizbazzle");
outputs.get(tag2).setName("gonzaggle");
outputs.get(tag3).setName("froonazzle");
runner.replaceTransforms(pipeline);
Job job = translator.translate(pipeline, runner, Collections.<DataflowPackage>emptyList()).getJob();
// The ParDo step
Step step = job.getSteps().get(1);
String stepName = Structs.getString(step.getProperties(), PropertyNames.USER_NAME);
List<Map<String, Object>> outputInfos = Structs.getListOfMaps(step.getProperties(), PropertyNames.OUTPUT_INFO, null);
assertThat(outputInfos.size(), equalTo(3));
// The names set by the user _and_ the tags _must_ be ignored, or metrics will not show up.
for (int i = 0; i < outputInfos.size(); ++i) {
assertThat(Structs.getString(outputInfos.get(i), PropertyNames.USER_NAME), equalTo(String.format("%s.out%s", stepName, i)));
}
}
use of org.apache.beam.sdk.values.PCollectionTuple in project beam by apache.
the class SplittableDoFnTest method testAdditionalOutput.
@Test
@Category({ ValidatesRunner.class, UsesSplittableParDo.class })
public void testAdditionalOutput() throws Exception {
TupleTag<String> mainOutputTag = new TupleTag<String>("main") {
};
TupleTag<String> additionalOutputTag = new TupleTag<String>("additional") {
};
PCollectionTuple res = p.apply("input", Create.of(0, 1, 2)).apply(ParDo.of(new SDFWithAdditionalOutput(additionalOutputTag)).withOutputTags(mainOutputTag, TupleTagList.of(additionalOutputTag)));
PAssert.that(res.get(mainOutputTag)).containsInAnyOrder(Arrays.asList("main:0", "main:1", "main:2"));
PAssert.that(res.get(additionalOutputTag)).containsInAnyOrder(Arrays.asList("additional:0", "additional:1", "additional:2"));
p.run();
}
use of org.apache.beam.sdk.values.PCollectionTuple in project beam by apache.
the class CreateStreamTest method testMultiOutputParDo.
/**
* Test multiple output {@link ParDo} in streaming pipelines.
* This is currently needed as a test for https://issues.apache.org/jira/browse/BEAM-2029 since
* {@link org.apache.beam.sdk.testing.ValidatesRunner} tests do not currently run for Spark runner
* in streaming mode.
*/
@Test
public void testMultiOutputParDo() throws IOException {
Instant instant = new Instant(0);
CreateStream<Integer> source1 = CreateStream.of(VarIntCoder.of(), batchDuration()).emptyBatch().advanceWatermarkForNextBatch(instant.plus(Duration.standardMinutes(5))).nextBatch(TimestampedValue.of(1, instant), TimestampedValue.of(2, instant), TimestampedValue.of(3, instant)).advanceNextBatchWatermarkToInfinity();
PCollection<Integer> inputs = p.apply(source1);
final TupleTag<Integer> mainTag = new TupleTag<>();
final TupleTag<Integer> additionalTag = new TupleTag<>();
PCollectionTuple outputs = inputs.apply(ParDo.of(new DoFn<Integer, Integer>() {
@SuppressWarnings("unused")
@ProcessElement
public void process(ProcessContext context) {
Integer element = context.element();
context.output(element);
context.output(additionalTag, element + 1);
}
}).withOutputTags(mainTag, TupleTagList.of(additionalTag)));
PCollection<Integer> output1 = outputs.get(mainTag).setCoder(VarIntCoder.of());
PCollection<Integer> output2 = outputs.get(additionalTag).setCoder(VarIntCoder.of());
PAssert.that(output1).containsInAnyOrder(1, 2, 3);
PAssert.that(output2).containsInAnyOrder(2, 3, 4);
p.run();
}
use of org.apache.beam.sdk.values.PCollectionTuple 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.PCollectionTuple in project beam by apache.
the class ReplacementOutputsTest method taggedMissingReplacementThrows.
@Test
public void taggedMissingReplacementThrows() {
PCollectionTuple original = PCollectionTuple.of(intsTag, ints).and(strsTag, strs).and(moreIntsTag, moreInts);
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Missing replacement");
thrown.expectMessage(intsTag.toString());
thrown.expectMessage(ints.toString());
ReplacementOutputs.tagged(original.expand(), PCollectionTuple.of(strsTag, replacementStrs).and(moreIntsTag, moreReplacementInts));
}
Aggregations