Java Code Examples for org.apache.beam.model.pipeline.v1.RunnerApi#TestStreamPayload

The following examples show how to use org.apache.beam.model.pipeline.v1.RunnerApi#TestStreamPayload . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestStreamTranslation.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Converts an {@link AppliedPTransform}, which may be a rehydrated transform or an original
 * {@link TestStream}, to a {@link TestStream}.
 */
public static <T> TestStream<T> getTestStream(
    AppliedPTransform<PBegin, PCollection<T>, PTransform<PBegin, PCollection<T>>> application)
    throws IOException {
  // For robustness, we don't take this shortcut:
  // if (application.getTransform() instanceof TestStream) {
  //   return application.getTransform()
  // }

  SdkComponents sdkComponents = SdkComponents.create(application.getPipeline().getOptions());
  RunnerApi.PTransform transformProto = PTransformTranslation.toProto(application, sdkComponents);
  checkArgument(
      TEST_STREAM_TRANSFORM_URN.equals(transformProto.getSpec().getUrn()),
      "Attempt to get %s from a transform with wrong URN %s",
      TestStream.class.getSimpleName(),
      transformProto.getSpec().getUrn());
  RunnerApi.TestStreamPayload testStreamPayload =
      RunnerApi.TestStreamPayload.parseFrom(transformProto.getSpec().getPayload());

  return (TestStream<T>)
      testStreamFromProtoPayload(
          testStreamPayload, RehydratedComponents.forComponents(sdkComponents.toComponents()));
}
 
Example 2
Source File: TestStreamTranslation.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Produces a {@link RunnerApi.TestStreamPayload} from a {@link TestStream}. */
static <T> RunnerApi.TestStreamPayload payloadForTestStream(
    final TestStream<T> transform, SdkComponents components) throws IOException {
  List<RunnerApi.TestStreamPayload.Event> protoEvents = new ArrayList<>();
  try {
    for (TestStream.Event<T> event : transform.getEvents()) {
      protoEvents.add(eventToProto(event, transform.getValueCoder()));
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  }

  return RunnerApi.TestStreamPayload.newBuilder()
      .setCoderId(components.registerCoder(transform.getValueCoder()))
      .addAllEvents(protoEvents)
      .build();
}
 
Example 3
Source File: TestStreamTranslationTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testRegistrarEncodedProto() throws Exception {
  PCollection<String> output = p.apply(testStream);

  AppliedPTransform<PBegin, PCollection<String>, TestStream<String>> appliedTestStream =
      AppliedPTransform.of("fakeName", PBegin.in(p).expand(), output.expand(), testStream, p);

  SdkComponents components = SdkComponents.create();
  components.registerEnvironment(Environments.createDockerEnvironment("java"));
  RunnerApi.FunctionSpec spec =
      PTransformTranslation.toProto(appliedTestStream, components).getSpec();

  assertThat(spec.getUrn(), equalTo(TEST_STREAM_TRANSFORM_URN));

  RunnerApi.TestStreamPayload payload = TestStreamPayload.parseFrom(spec.getPayload());

  verifyTestStreamEncoding(
      testStream, payload, RehydratedComponents.forComponents(components.toComponents()));
}
 
Example 4
Source File: TestStreamTranslationTest.java    From beam with Apache License 2.0 6 votes vote down vote up
private static <T> void verifyTestStreamEncoding(
    TestStream<T> testStream,
    RunnerApi.TestStreamPayload payload,
    RehydratedComponents protoComponents)
    throws Exception {

  // This reverse direction is only valid for Java-based coders
  assertThat(protoComponents.getCoder(payload.getCoderId()), equalTo(testStream.getValueCoder()));

  assertThat(payload.getEventsList().size(), equalTo(testStream.getEvents().size()));

  for (int i = 0; i < payload.getEventsList().size(); ++i) {
    assertThat(
        TestStreamTranslation.eventFromProto(payload.getEvents(i), testStream.getValueCoder()),
        equalTo(testStream.getEvents().get(i)));
  }
}
 
Example 5
Source File: TestStreamTranslation.java    From beam with Apache License 2.0 5 votes vote down vote up
public static TestStream<?> testStreamFromProtoPayload(
    RunnerApi.TestStreamPayload testStreamPayload, RehydratedComponents components)
    throws IOException {

  Coder<Object> coder = (Coder<Object>) components.getCoder(testStreamPayload.getCoderId());

  List<TestStream.Event<Object>> events = new ArrayList<>();

  for (RunnerApi.TestStreamPayload.Event event : testStreamPayload.getEventsList()) {
    events.add(eventFromProto(event, coder));
  }
  return TestStream.fromRawEvents(coder, events);
}
 
Example 6
Source File: TestStreamTranslationTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodedProto() throws Exception {
  SdkComponents components = SdkComponents.create();
  components.registerEnvironment(Environments.createDockerEnvironment("java"));
  RunnerApi.TestStreamPayload payload =
      TestStreamTranslation.payloadForTestStream(testStream, components);

  verifyTestStreamEncoding(
      testStream, payload, RehydratedComponents.forComponents(components.toComponents()));
}
 
Example 7
Source File: FlinkStreamingPortablePipelineTranslator.java    From beam with Apache License 2.0 5 votes vote down vote up
private <T> void translateTestStream(
    String id, RunnerApi.Pipeline pipeline, StreamingTranslationContext context) {
  RunnerApi.Components components = pipeline.getComponents();

  SerializableFunction<byte[], TestStream<T>> testStreamDecoder =
      bytes -> {
        try {
          RunnerApi.TestStreamPayload testStreamPayload =
              RunnerApi.TestStreamPayload.parseFrom(bytes);
          @SuppressWarnings("unchecked")
          TestStream<T> testStream =
              (TestStream<T>)
                  TestStreamTranslation.testStreamFromProtoPayload(
                      testStreamPayload, RehydratedComponents.forComponents(components));
          return testStream;
        } catch (Exception e) {
          throw new RuntimeException("Can't decode TestStream payload.", e);
        }
      };

  RunnerApi.PTransform transform = components.getTransformsOrThrow(id);
  String outputPCollectionId = Iterables.getOnlyElement(transform.getOutputsMap().values());
  Coder<WindowedValue<T>> coder = instantiateCoder(outputPCollectionId, components);

  DataStream<WindowedValue<T>> source =
      context
          .getExecutionEnvironment()
          .addSource(
              new TestStreamSource<>(
                  testStreamDecoder, transform.getSpec().getPayload().toByteArray()),
              new CoderTypeInformation<>(coder));

  context.addDataStream(outputPCollectionId, source);
}