Java Code Examples for org.apache.beam.sdk.io.Read#Unbounded

The following examples show how to use org.apache.beam.sdk.io.Read#Unbounded . 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: StreamingTransformTranslator.java    From beam with Apache License 2.0 6 votes vote down vote up
private static <T> TransformEvaluator<Read.Unbounded<T>> readUnbounded() {
  return new TransformEvaluator<Read.Unbounded<T>>() {
    @Override
    public void evaluate(Read.Unbounded<T> transform, EvaluationContext context) {
      final String stepName = context.getCurrentTransform().getFullName();
      context.putDataset(
          transform,
          SparkUnboundedSource.read(
              context.getStreamingContext(),
              context.getSerializableOptions(),
              transform.getSource(),
              stepName));
    }

    @Override
    public String toNativeString() {
      return "streamingContext.<readFrom(<source>)>()";
    }
  };
}
 
Example 2
Source File: FlinkStreamingTransformTranslatorsTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void readSourceTranslatorUnboundedWithMaxParallelism() {

  final int maxParallelism = 6;
  final int parallelism = 2;

  Read.Unbounded transform = Read.from(new TestUnboundedSource());
  StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  env.setParallelism(parallelism);
  env.setMaxParallelism(maxParallelism);

  OneInputTransformation<?, ?> sourceTransform =
      (OneInputTransformation)
          applyReadSourceTransform(transform, PCollection.IsBounded.UNBOUNDED, env);

  UnboundedSourceWrapper source =
      (UnboundedSourceWrapper)
          ((SourceTransformation) sourceTransform.getInput()).getOperator().getUserFunction();

  assertEquals(maxParallelism, source.getSplitSources().size());
}
 
Example 3
Source File: FlinkStreamingTransformTranslatorsTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void readSourceTranslatorUnboundedWithoutMaxParallelism() {

  final int parallelism = 2;

  Read.Unbounded transform = Read.from(new TestUnboundedSource());
  StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  env.setParallelism(parallelism);

  OneInputTransformation<?, ?> sourceTransform =
      (OneInputTransformation)
          applyReadSourceTransform(transform, PCollection.IsBounded.UNBOUNDED, env);

  UnboundedSourceWrapper source =
      (UnboundedSourceWrapper)
          ((SourceTransformation) sourceTransform.getInput()).getOperator().getUserFunction();

  assertEquals(parallelism, source.getSplitSources().size());
}
 
Example 4
Source File: PipelineTranslator.java    From incubator-nemo with Apache License 2.0 5 votes vote down vote up
/**
 * @param ctx       provides translation context
 * @param beamNode  the beam node to be translated
 * @param transform transform which can be obtained from {@code beamNode}
 */
@PrimitiveTransformTranslator(Read.Unbounded.class)
private static void unboundedReadTranslator(final PipelineTranslationContext ctx,
                                            final TransformHierarchy.Node beamNode,
                                            final Read.Unbounded<?> transform) {
  final IRVertex vertex = new BeamUnboundedSourceVertex<>(transform.getSource(), DisplayData.from(transform));
  ctx.addVertex(vertex);
  beamNode.getInputs().values().forEach(input -> ctx.addEdgeTo(vertex, input));
  beamNode.getOutputs().values().forEach(output -> ctx.registerMainOutputFrom(beamNode, vertex, output));
}
 
Example 5
Source File: ReadTranslation.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public FunctionSpec translate(
    AppliedPTransform<?, ?, Read.Unbounded<?>> transform, SdkComponents components) {
  ReadPayload payload = toProto(transform.getTransform(), components);
  return RunnerApi.FunctionSpec.newBuilder()
      .setUrn(getUrn(transform.getTransform()))
      .setPayload(payload.toByteString())
      .build();
}
 
Example 6
Source File: ReadTranslationTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testToFromProtoUnbounded() throws Exception {
  assumeThat(source, instanceOf(UnboundedSource.class));
  UnboundedSource<?, ?> unboundedSource = (UnboundedSource<?, ?>) this.source;
  Read.Unbounded<?> unboundedRead = Read.from(unboundedSource);
  SdkComponents components = SdkComponents.create();
  // No environment set for unbounded sources
  ReadPayload payload = ReadTranslation.toProto(unboundedRead, components);
  assertThat(payload.getIsBounded(), equalTo(RunnerApi.IsBounded.Enum.UNBOUNDED));
  UnboundedSource<?, ?> deserializedSource = ReadTranslation.unboundedSourceFromProto(payload);
  assertThat(deserializedSource, equalTo(source));
}
 
Example 7
Source File: SparkNativePipelineVisitor.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
  try {
    Class<? extends PTransform> transformClass = transform.getClass();
    if ("KafkaIO.Read".equals(node.getFullName())) {
      return "KafkaUtils.createDirectStream(...)";
    }
    if (composite) {
      return "_.<" + transformClass.getName() + ">";
    }
    String transformString = transformEvaluator.toNativeString();
    if (transformString.contains("<fn>")) {
      transformString = replaceFnString(transformClass, transformString, "fn");
    } else if (transformString.contains("<windowFn>")) {
      transformString = replaceFnString(transformClass, transformString, "windowFn");
    } else if (transformString.contains("<source>")) {
      String sourceName = "...";
      if (transform instanceof Read.Bounded) {
        sourceName = ((Read.Bounded<?>) transform).getSource().getClass().getName();
      } else if (transform instanceof Read.Unbounded) {
        sourceName = ((Read.Unbounded<?>) transform).getSource().getClass().getName();
      }
      transformString = transformString.replace("<source>", sourceName);
    }
    if (transformString.startsWith("sparkContext")
        || transformString.startsWith("streamingContext")) {
      return transformString;
    }
    return "_." + transformString;
  } catch (NoSuchMethodException
      | InvocationTargetException
      | IllegalAccessException
      | NoSuchFieldException e) {
    return "<FailedTranslation>";
  }
}
 
Example 8
Source File: UnboundedReadEvaluatorFactoryTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private void processElement(final TestUnboundedSource<String> source) throws Exception {
  final EvaluationContext context =
      EvaluationContext.create(
          MockClock.fromInstant(Instant.now()),
          CloningBundleFactory.create(),
          DirectGraph.create(
              emptyMap(), emptyMap(), LinkedListMultimap.create(), emptySet(), emptyMap()),
          emptySet(),
          Executors.newCachedThreadPool());
  final UnboundedReadEvaluatorFactory factory =
      new UnboundedReadEvaluatorFactory(context, options);

  final Read.Unbounded<String> unbounded = Read.from(source);
  final Pipeline pipeline = Pipeline.create(options);
  final PCollection<String> pCollection = pipeline.apply(unbounded);
  final AppliedPTransform<PBegin, PCollection<String>, Read.Unbounded<String>> application =
      AppliedPTransform.of(
          "test",
          new HashMap<>(),
          singletonMap(new TupleTag(), pCollection),
          unbounded,
          pipeline);
  final TransformEvaluator<UnboundedSourceShard<String, TestCheckpointMark>> evaluator =
      factory.forApplication(application, null);
  final UnboundedSource.UnboundedReader<String> reader = source.createReader(options, null);
  final UnboundedSourceShard<String, TestCheckpointMark> shard =
      UnboundedSourceShard.of(source, new NeverDeduplicator(), reader, null);
  final WindowedValue<UnboundedSourceShard<String, TestCheckpointMark>> value =
      WindowedValue.of(
          shard, BoundedWindow.TIMESTAMP_MAX_VALUE, GlobalWindow.INSTANCE, PaneInfo.NO_FIRING);
  TestUnboundedSource.readerClosedCount = 0;
  evaluator.processElement(value);
}
 
Example 9
Source File: ReadTranslation.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public String getUrn(Read.Unbounded<?> transform) {
  return PTransformTranslation.READ_TRANSFORM_URN;
}
 
Example 10
Source File: PTransformTranslationTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private static AppliedPTransform<?, ?, ?> read(Pipeline pipeline) {
  Read.Unbounded<Long> transform = Read.from(CountingSource.unbounded());
  PCollection<Long> pcollection = pipeline.apply(transform);
  return AppliedPTransform.of(
      "ReadTheCount", pipeline.begin().expand(), pcollection.expand(), transform, pipeline);
}
 
Example 11
Source File: DataflowRunner.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public PTransformReplacement<PBegin, PCollection<T>> getReplacementTransform(
    AppliedPTransform<PBegin, PCollection<T>, Read.Unbounded<T>> transform) {
  return PTransformReplacement.of(
      transform.getPipeline().begin(), new StreamingUnboundedRead<>(transform.getTransform()));
}
 
Example 12
Source File: DataflowRunner.java    From beam with Apache License 2.0 4 votes vote down vote up
public StreamingUnboundedRead(Read.Unbounded<T> transform) {
  this.source = transform.getSource();
}
 
Example 13
Source File: UnboundedReadEvaluatorFactory.java    From beam with Apache License 2.0 4 votes vote down vote up
private <OutputT> TransformEvaluator<?> createEvaluator(
    AppliedPTransform<PBegin, PCollection<OutputT>, Read.Unbounded<OutputT>> application) {
  return new UnboundedReadEvaluator<>(application, evaluationContext, options, readerReuseChance);
}