org.apache.beam.sdk.coders.VarIntCoder Java Examples

The following examples show how to use org.apache.beam.sdk.coders.VarIntCoder. 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: DoFnSignaturesTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testStateIdNonFinal() throws Exception {
  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage("State declarations must be final");
  thrown.expectMessage("Non-final field");
  thrown.expectMessage("myfield");
  thrown.expectMessage(not(mentionsTimers()));
  DoFnSignatures.getSignature(
      new DoFn<KV<String, Integer>, Long>() {
        @StateId("my-id")
        private StateSpec<ValueState<Integer>> myfield = StateSpecs.value(VarIntCoder.of());

        @ProcessElement
        public void foo(ProcessContext context) {}
      }.getClass());
}
 
Example #2
Source File: PTransformMatchersTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void emptyFlattenWithNonEmptyFlatten() {
  AppliedPTransform application =
      AppliedPTransform.of(
          "Flatten",
          Collections.singletonMap(
              new TupleTag<Integer>(),
              PCollection.createPrimitiveOutputInternal(
                  p, WindowingStrategy.globalDefault(), IsBounded.BOUNDED, VarIntCoder.of())),
          Collections.singletonMap(
              new TupleTag<Integer>(),
              PCollection.createPrimitiveOutputInternal(
                  p, WindowingStrategy.globalDefault(), IsBounded.BOUNDED, VarIntCoder.of())),
          Flatten.pCollections(),
          p);

  assertThat(PTransformMatchers.emptyFlatten().matches(application), is(false));
}
 
Example #3
Source File: BatchSideInputHandlerFactoryTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void groupsValuesByKey() {
  when(context.getSideInput(COLLECTION_ID))
      .thenReturn(
          Arrays.asList(
              WindowedValue.valueInGlobalWindow(KV.of("foo", 2)),
              WindowedValue.valueInGlobalWindow(KV.of("bar", 3)),
              WindowedValue.valueInGlobalWindow(KV.of("foo", 5))));

  BatchSideInputHandlerFactory factory =
      BatchSideInputHandlerFactory.forStage(EXECUTABLE_STAGE, context);
  MultimapSideInputHandler<String, Integer, GlobalWindow> handler =
      factory.forMultimapSideInput(
          TRANSFORM_ID,
          SIDE_INPUT_NAME,
          KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()),
          GlobalWindow.Coder.INSTANCE);
  Iterable<String> keys = handler.get(GlobalWindow.INSTANCE);
  assertThat(keys, containsInAnyOrder("foo", "bar"));
  Iterable<Integer> values = handler.get("foo", GlobalWindow.INSTANCE);
  assertThat(values, containsInAnyOrder(2, 5));
}
 
Example #4
Source File: DoFnSignaturesTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleStateIdNamedDoFn() throws Exception {
  class DoFnForTestSimpleStateIdNamedDoFn extends DoFn<KV<String, Integer>, Long> {
    @StateId("foo")
    private final StateSpec<ValueState<Integer>> bizzle = StateSpecs.value(VarIntCoder.of());

    @ProcessElement
    public void foo(ProcessContext context) {}
  }

  // Test classes at the bottom of the file
  DoFnSignature sig = DoFnSignatures.signatureForDoFn(new DoFnForTestSimpleStateIdNamedDoFn());

  assertThat(sig.stateDeclarations().size(), equalTo(1));
  DoFnSignature.StateDeclaration decl = sig.stateDeclarations().get("foo");

  assertThat(decl.id(), equalTo("foo"));
  assertThat(
      decl.field(), equalTo(DoFnForTestSimpleStateIdNamedDoFn.class.getDeclaredField("bizzle")));
  assertThat(
      decl.stateType(),
      Matchers.<TypeDescriptor<?>>equalTo(new TypeDescriptor<ValueState<Integer>>() {}));
}
 
Example #5
Source File: BatchViewOverrides.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public PCollection<KV<Integer, Iterable<KV<KV<K, W>, WindowedValue<V>>>>> expand(
    PCollection<KV<K, V>> input) {

  @SuppressWarnings("unchecked")
  Coder<W> windowCoder = (Coder<W>) input.getWindowingStrategy().getWindowFn().windowCoder();
  @SuppressWarnings("unchecked")
  KvCoder<K, V> inputCoder = (KvCoder<K, V>) input.getCoder();

  PCollection<KV<Integer, KV<KV<K, W>, WindowedValue<V>>>> keyedByHash;
  keyedByHash =
      input.apply(ParDo.of(new GroupByKeyHashAndSortByKeyAndWindowDoFn<K, V, W>(coder)));
  keyedByHash.setCoder(
      KvCoder.of(
          VarIntCoder.of(),
          KvCoder.of(
              KvCoder.of(inputCoder.getKeyCoder(), windowCoder),
              FullWindowedValueCoder.of(inputCoder.getValueCoder(), windowCoder))));

  return keyedByHash.apply(new GroupByKeyAndSortValuesOnly<>());
}
 
Example #6
Source File: ViewTest.java    From beam with Apache License 2.0 6 votes vote down vote up
private void testViewUnbounded(
    Pipeline pipeline,
    PTransform<PCollection<KV<String, Integer>>, ? extends PCollectionView<?>> view) {
  thrown.expect(IllegalStateException.class);
  thrown.expectMessage("Unable to create a side-input view from input");
  thrown.expectCause(
      ThrowableMessageMatcher.hasMessage(Matchers.containsString("non-bounded PCollection")));
  pipeline
      .apply(
          new PTransform<PBegin, PCollection<KV<String, Integer>>>() {
            @Override
            public PCollection<KV<String, Integer>> expand(PBegin input) {
              return PCollection.createPrimitiveOutputInternal(
                  input.getPipeline(),
                  WindowingStrategy.globalDefault(),
                  PCollection.IsBounded.UNBOUNDED,
                  KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()));
            }
          })
      .apply(view);
}
 
Example #7
Source File: TrackStreamingSourcesTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testTrackSingle() {
  options.setRunner(SparkRunner.class);
  JavaSparkContext jsc = SparkContextFactory.getSparkContext(options);
  JavaStreamingContext jssc =
      new JavaStreamingContext(
          jsc, new org.apache.spark.streaming.Duration(options.getBatchIntervalMillis()));

  Pipeline p = Pipeline.create(options);

  CreateStream<Integer> emptyStream =
      CreateStream.of(VarIntCoder.of(), Duration.millis(options.getBatchIntervalMillis()))
          .emptyBatch();

  p.apply(emptyStream).apply(ParDo.of(new PassthroughFn<>()));

  p.traverseTopologically(new StreamingSourceTracker(jssc, p, ParDo.MultiOutput.class, 0));
  assertThat(StreamingSourceTracker.numAssertions, equalTo(1));
}
 
Example #8
Source File: ReshuffleTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
@Category(ValidatesRunner.class)
public void testReshuffleAfterFixedWindows() {

  PCollection<KV<String, Integer>> input =
      pipeline
          .apply(
              Create.of(ARBITRARY_KVS)
                  .withCoder(KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of())))
          .apply(Window.into(FixedWindows.of(Duration.standardMinutes(10L))));

  PCollection<KV<String, Integer>> output = input.apply(Reshuffle.of());

  PAssert.that(output).containsInAnyOrder(ARBITRARY_KVS);

  assertEquals(input.getWindowingStrategy(), output.getWindowingStrategy());

  pipeline.run();
}
 
Example #9
Source File: StateTagTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapEquality() {
  StateTag<?> fooStringVarInt1 = StateTags.map("foo", StringUtf8Coder.of(), VarIntCoder.of());
  StateTag<?> fooStringVarInt2 = StateTags.map("foo", StringUtf8Coder.of(), VarIntCoder.of());
  StateTag<?> fooStringBigEndian =
      StateTags.map("foo", StringUtf8Coder.of(), BigEndianIntegerCoder.of());
  StateTag<?> fooVarIntBigEndian =
      StateTags.map("foo", VarIntCoder.of(), BigEndianIntegerCoder.of());
  StateTag<?> barStringVarInt = StateTags.map("bar", StringUtf8Coder.of(), VarIntCoder.of());

  assertEquals(fooStringVarInt1, fooStringVarInt2);
  assertEquals(fooStringVarInt1, fooStringBigEndian);
  assertEquals(fooStringBigEndian, fooVarIntBigEndian);
  assertEquals(fooStringVarInt1, fooVarIntBigEndian);
  assertNotEquals(fooStringVarInt1, barStringVarInt);
}
 
Example #10
Source File: ReduceFnTester.java    From beam with Apache License 2.0 6 votes vote down vote up
public static <W extends BoundedWindow, AccumT, OutputT>
    ReduceFnTester<Integer, OutputT, W> combining(
        WindowingStrategy<?, W> strategy,
        CombineFnWithContext<Integer, AccumT, OutputT> combineFn,
        Coder<OutputT> outputCoder,
        PipelineOptions options,
        SideInputReader sideInputReader)
        throws Exception {
  CoderRegistry registry = CoderRegistry.createDefault();
  // Ensure that the CombineFn can be converted into an AppliedCombineFn
  AppliedCombineFn.withInputCoder(
      combineFn, registry, KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()));

  return combining(
      strategy,
      TriggerStateMachines.stateMachineForTrigger(
          TriggerTranslation.toProto(strategy.getTrigger())),
      combineFn,
      outputCoder,
      options,
      sideInputReader);
}
 
Example #11
Source File: BatchSideInputHandlerFactoryTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void singleElementForCollection() {
  when(context.getSideInput(COLLECTION_ID))
      .thenReturn(
          Arrays.asList(WindowedValue.valueInGlobalWindow(KV.<Void, Integer>of(null, 3))));

  BatchSideInputHandlerFactory factory =
      BatchSideInputHandlerFactory.forStage(EXECUTABLE_STAGE, context);
  MultimapSideInputHandler<Void, Integer, GlobalWindow> handler =
      factory.forMultimapSideInput(
          TRANSFORM_ID,
          SIDE_INPUT_NAME,
          KvCoder.of(VoidCoder.of(), VarIntCoder.of()),
          GlobalWindow.Coder.INSTANCE);
  Iterable<Void> keys = handler.get(GlobalWindow.INSTANCE);
  assertThat(keys, contains((Void) null));
  Iterable<Integer> values = handler.get(null, GlobalWindow.INSTANCE);
  assertThat(values, contains(3));
}
 
Example #12
Source File: WriteFiles.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public PCollection<FileResult<DestinationT>> expand(PCollection<UserT> input) {
  List<PCollectionView<?>> shardingSideInputs = Lists.newArrayList(getSideInputs());
  if (numShardsView != null) {
    shardingSideInputs.add(numShardsView);
  }

  ShardingFunction<UserT, DestinationT> shardingFunction =
      getShardingFunction() == null
          ? new RandomShardingFunction(destinationCoder)
          : getShardingFunction();

  return input
      .apply(
          "ApplyShardingKey",
          ParDo.of(new ApplyShardingFunctionFn(shardingFunction, numShardsView))
              .withSideInputs(shardingSideInputs))
      .setCoder(KvCoder.of(ShardedKeyCoder.of(VarIntCoder.of()), input.getCoder()))
      .apply("GroupIntoShards", GroupByKey.create())
      .apply(
          "WriteShardsIntoTempFiles",
          ParDo.of(new WriteShardsIntoTempFilesFn()).withSideInputs(getSideInputs()))
      .setCoder(fileResultCoder);
}
 
Example #13
Source File: DirectRunnerTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a {@link DoFn} that mutates its input with a good equals() fails in the {@link
 * DirectRunner}.
 */
@Test
public void testMutatingInputDoFnError() throws Exception {
  Pipeline pipeline = getPipeline();

  pipeline
      .apply(
          Create.of(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6))
              .withCoder(ListCoder.of(VarIntCoder.of())))
      .apply(
          ParDo.of(
              new DoFn<List<Integer>, Integer>() {
                @ProcessElement
                public void processElement(ProcessContext c) {
                  List<Integer> inputList = c.element();
                  inputList.set(0, 37);
                  c.output(12);
                }
              }));

  thrown.expect(IllegalMutationException.class);
  thrown.expectMessage("Input");
  thrown.expectMessage("must not be mutated");
  pipeline.run();
}
 
Example #14
Source File: DoFnInvokersTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that the generated {@link DoFnInvoker} passes the state parameter that it should. */
@Test
public void testDoFnWithState() throws Exception {
  ValueState<Integer> mockState = mock(ValueState.class);
  final String stateId = "my-state-id-here";
  when(mockArgumentProvider.state(stateId, false)).thenReturn(mockState);

  class MockFn extends DoFn<String, String> {
    @StateId(stateId)
    private final StateSpec<ValueState<Integer>> spec = StateSpecs.value(VarIntCoder.of());

    @ProcessElement
    public void processElement(ProcessContext c, @StateId(stateId) ValueState<Integer> valueState)
        throws Exception {}
  }

  MockFn fn = mock(MockFn.class);
  assertEquals(stop(), invokeProcessElement(fn));
  verify(fn).processElement(mockProcessContext, mockState);
}
 
Example #15
Source File: DataflowViewTest.java    From beam with Apache License 2.0 6 votes vote down vote up
private void testViewUnbounded(
    Pipeline pipeline,
    PTransform<PCollection<KV<String, Integer>>, ? extends PCollectionView<?>> view) {
  thrown.expect(IllegalStateException.class);
  thrown.expectMessage("Unable to create a side-input view from input");
  thrown.expectCause(
      ThrowableMessageMatcher.hasMessage(Matchers.containsString("non-bounded PCollection")));
  pipeline
      .apply(
          new PTransform<PBegin, PCollection<KV<String, Integer>>>() {
            @Override
            public PCollection<KV<String, Integer>> expand(PBegin input) {
              return PCollection.createPrimitiveOutputInternal(
                  input.getPipeline(),
                  WindowingStrategy.globalDefault(),
                  PCollection.IsBounded.UNBOUNDED,
                  KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()));
            }
          })
      .apply(view);
}
 
Example #16
Source File: ParDoTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
@Category(ValidatesRunner.class)
public void testParDoWithEmptyTaggedOutput() {
  TupleTag<String> mainOutputTag = new TupleTag<String>("main") {};
  TupleTag<String> additionalOutputTag1 = new TupleTag<String>("additional1") {};
  TupleTag<String> additionalOutputTag2 = new TupleTag<String>("additional2") {};

  PCollectionTuple outputs =
      pipeline
          .apply(Create.empty(VarIntCoder.of()))
          .apply(
              ParDo.of(new TestNoOutputDoFn())
                  .withOutputTags(
                      mainOutputTag,
                      TupleTagList.of(additionalOutputTag1).and(additionalOutputTag2)));

  PAssert.that(outputs.get(mainOutputTag)).empty();

  PAssert.that(outputs.get(additionalOutputTag1)).empty();
  PAssert.that(outputs.get(additionalOutputTag2)).empty();

  pipeline.run();
}
 
Example #17
Source File: KeyedWorkItemCoderTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecodeEqual() throws Exception {
  Iterable<TimerData> timers =
      ImmutableList.of(
          TimerData.of(
              StateNamespaces.global(),
              new Instant(500L),
              new Instant(500L),
              TimeDomain.EVENT_TIME));
  Iterable<WindowedValue<Integer>> elements =
      ImmutableList.of(
          WindowedValue.valueInGlobalWindow(1),
          WindowedValue.valueInGlobalWindow(4),
          WindowedValue.valueInGlobalWindow(8));

  KeyedWorkItemCoder<String, Integer> coder =
      KeyedWorkItemCoder.of(StringUtf8Coder.of(), VarIntCoder.of(), GlobalWindow.Coder.INSTANCE);

  CoderProperties.coderDecodeEncodeEqual(coder, KeyedWorkItems.workItem("foo", timers, elements));
  CoderProperties.coderDecodeEncodeEqual(coder, KeyedWorkItems.elementsWorkItem("foo", elements));
  CoderProperties.coderDecodeEncodeEqual(coder, KeyedWorkItems.timersWorkItem("foo", timers));
}
 
Example #18
Source File: ReduceFnTester.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link ReduceFnTester} for the given {@link WindowingStrategy} and {@link CombineFn},
 * creating a {@link TriggerStateMachine} from the {@link Trigger} in the {@link
 * WindowingStrategy}.
 */
public static <W extends BoundedWindow, AccumT, OutputT>
    ReduceFnTester<Integer, OutputT, W> combining(
        WindowingStrategy<?, W> strategy,
        CombineFn<Integer, AccumT, OutputT> combineFn,
        Coder<OutputT> outputCoder)
        throws Exception {

  CoderRegistry registry = CoderRegistry.createDefault();
  // Ensure that the CombineFn can be converted into an AppliedCombineFn
  AppliedCombineFn.withInputCoder(
      combineFn, registry, KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()));

  return combining(
      strategy,
      TriggerStateMachines.stateMachineForTrigger(
          TriggerTranslation.toProto(strategy.getTrigger())),
      combineFn,
      outputCoder);
}
 
Example #19
Source File: PTransformMatchersTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void classEqualToDoesNotMatchSubclass() {
  class MyPTransform extends PTransform<PCollection<KV<String, Integer>>, PCollection<Integer>> {
    @Override
    public PCollection<Integer> expand(PCollection<KV<String, Integer>> input) {
      return PCollection.createPrimitiveOutputInternal(
          input.getPipeline(), input.getWindowingStrategy(), input.isBounded(), VarIntCoder.of());
    }
  }

  PTransformMatcher matcher = PTransformMatchers.classEqualTo(MyPTransform.class);
  MyPTransform subclass = new MyPTransform() {};

  assertThat(subclass.getClass(), not(Matchers.<Class<?>>equalTo(MyPTransform.class)));
  assertThat(subclass, instanceOf(MyPTransform.class));

  AppliedPTransform<?, ?, ?> application = getAppliedTransform(subclass);

  assertThat(matcher.matches(application), is(false));
}
 
Example #20
Source File: MutationDetectorsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that {@link MutationDetectors#forValueWithCoder} does not false positive on a {@link
 * LinkedList} that will clone as an {@code ArrayList}.
 */
@Test
public void testUnmodifiedLinkedList() throws Exception {
  List<Integer> value = Lists.newLinkedList(Arrays.asList(1, 2, 3, 4));
  MutationDetector detector =
      MutationDetectors.forValueWithCoder(value, ListCoder.of(VarIntCoder.of()));
  detector.verifyUnmodified();
}
 
Example #21
Source File: MutationDetectorsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that {@link MutationDetectors#forValueWithCoder} does not false positive on a {@link Set}
 * coded as an {@link Iterable}.
 */
@Test
public void testImmutableSet() throws Exception {
  Set<Integer> value = Sets.newHashSet(Arrays.asList(1, 2, 3, 4));
  MutationDetector detector =
      MutationDetectors.forValueWithCoder(value, IterableCoder.of(VarIntCoder.of()));
  detector.verifyUnmodified();
}
 
Example #22
Source File: MutationDetectorsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that {@link MutationDetectors#forValueWithCoder} does not false positive on a {@link
 * LinkedList} coded as an {@link Iterable}.
 */
@Test
public void testImmutableList() throws Exception {
  List<Integer> value = Lists.newLinkedList(Arrays.asList(1, 2, 3, 4));
  MutationDetector detector =
      MutationDetectors.forValueWithCoder(value, IterableCoder.of(VarIntCoder.of()));
  detector.verifyUnmodified();
}
 
Example #23
Source File: BigtableIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Tests that at least one result is emitted per element written in each window. */
@Test
public void testWritingEmitsResultsWhenDoneInFixedWindow() throws Exception {
  final String table = "table";
  final String key = "key";
  final String value = "value";

  service.createTable(table);

  Instant elementTimestamp = Instant.parse("2019-06-10T00:00:00");
  Duration windowDuration = Duration.standardMinutes(1);

  TestStream<Integer> input =
      TestStream.create(VarIntCoder.of())
          .advanceWatermarkTo(elementTimestamp)
          .addElements(1)
          .advanceWatermarkTo(elementTimestamp.plus(windowDuration))
          .addElements(2)
          .advanceWatermarkToInfinity();

  BoundedWindow expectedFirstWindow = new IntervalWindow(elementTimestamp, windowDuration);
  BoundedWindow expectedSecondWindow =
      new IntervalWindow(elementTimestamp.plus(windowDuration), windowDuration);

  PCollection<BigtableWriteResult> results =
      p.apply("rows", input)
          .apply("window", Window.into(FixedWindows.of(windowDuration)))
          .apply("expand", ParDo.of(new WriteGeneratorDoFn()))
          .apply("write", defaultWrite.withTableId(table).withWriteResults());
  PAssert.that(results)
      .inWindow(expectedFirstWindow)
      .containsInAnyOrder(BigtableWriteResult.create(1));
  PAssert.that(results)
      .inWindow(expectedSecondWindow)
      .containsInAnyOrder(BigtableWriteResult.create(2));

  p.run();
}
 
Example #24
Source File: BatchSideInputHandlerFactoryTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void groupsValuesByWindowAndKey() {
  Instant instantA = new DateTime(2018, 1, 1, 1, 1, DateTimeZone.UTC).toInstant();
  Instant instantB = new DateTime(2018, 1, 1, 1, 2, DateTimeZone.UTC).toInstant();
  Instant instantC = new DateTime(2018, 1, 1, 1, 3, DateTimeZone.UTC).toInstant();
  IntervalWindow windowA = new IntervalWindow(instantA, instantB);
  IntervalWindow windowB = new IntervalWindow(instantB, instantC);
  when(context.getSideInput(COLLECTION_ID))
      .thenReturn(
          Arrays.asList(
              WindowedValue.of(KV.of("foo", 1), instantA, windowA, PaneInfo.NO_FIRING),
              WindowedValue.of(KV.of("baz", 2), instantA, windowA, PaneInfo.NO_FIRING),
              WindowedValue.of(KV.of("foo", 3), instantA, windowA, PaneInfo.NO_FIRING),
              WindowedValue.of(KV.of("foo", 4), instantB, windowB, PaneInfo.NO_FIRING),
              WindowedValue.of(KV.of("bar", 5), instantB, windowB, PaneInfo.NO_FIRING),
              WindowedValue.of(KV.of("foo", 6), instantB, windowB, PaneInfo.NO_FIRING)));

  BatchSideInputHandlerFactory factory =
      BatchSideInputHandlerFactory.forStage(EXECUTABLE_STAGE, context);
  MultimapSideInputHandler<String, Integer, IntervalWindow> handler =
      factory.forMultimapSideInput(
          TRANSFORM_ID,
          SIDE_INPUT_NAME,
          KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()),
          IntervalWindowCoder.of());
  Iterable<String> keysA = handler.get(windowA);
  Iterable<String> keysB = handler.get(windowB);
  assertThat(keysA, containsInAnyOrder("foo", "baz"));
  assertThat(keysB, containsInAnyOrder("foo", "bar"));
  Iterable<Integer> valuesA = handler.get("foo", windowA);
  Iterable<Integer> valuesB = handler.get("foo", windowB);
  assertThat(valuesA, containsInAnyOrder(1, 3));
  assertThat(valuesB, containsInAnyOrder(4, 6));
}
 
Example #25
Source File: BatchViewOverrides.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public PCollection<KV<Integer, Iterable<KV<W, WindowedValue<T>>>>> expand(
    PCollection<T> input) {
  @SuppressWarnings("unchecked")
  Coder<W> windowCoder = (Coder<W>) input.getWindowingStrategy().getWindowFn().windowCoder();
  PCollection<KV<Integer, KV<W, WindowedValue<T>>>> rval =
      input.apply(
          ParDo.of(new UseWindowHashAsKeyAndWindowAsSortKeyDoFn<T, W>(ismCoderForHash)));
  rval.setCoder(
      KvCoder.of(
          VarIntCoder.of(),
          KvCoder.of(windowCoder, FullWindowedValueCoder.of(input.getCoder(), windowCoder))));
  return rval.apply(new GroupByKeyAndSortValuesOnly<>());
}
 
Example #26
Source File: DataflowPTransformMatchersTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Creates a simple pipeline with a {@link Combine.PerKey}. */
private static TestPipeline createCombinePerKeyPipeline() {
  TestPipeline pipeline = TestPipeline.create().enableAbandonedNodeEnforcement(false);
  PCollection<KV<String, Integer>> input =
      pipeline
          .apply(Create.of(KV.of("key", 1)))
          .setCoder(KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()));
  input.apply(Combine.perKey(new SumCombineFn()));

  return pipeline;
}
 
Example #27
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that when a processing time timer comes in after a window is expired but in the same
 * bundle it does not cause a spurious output.
 */
@Test
public void testCombiningAccumulatingProcessingTime() throws Exception {
  WindowingStrategy<?, IntervalWindow> strategy =
      WindowingStrategy.of((WindowFn<?, IntervalWindow>) FixedWindows.of(Duration.millis(100)))
          .withTimestampCombiner(TimestampCombiner.EARLIEST)
          .withMode(AccumulationMode.ACCUMULATING_FIRED_PANES)
          .withAllowedLateness(Duration.ZERO)
          .withTrigger(
              Repeatedly.forever(
                  AfterProcessingTime.pastFirstElementInPane().plusDelayOf(Duration.millis(10))));

  ReduceFnTester<Integer, Integer, IntervalWindow> tester =
      ReduceFnTester.combining(strategy, Sum.ofIntegers(), VarIntCoder.of());

  tester.advanceProcessingTime(new Instant(5000));
  injectElement(tester, 2); // processing timer @ 5000 + 10; EOW timer @ 100
  injectElement(tester, 5);

  tester.advanceInputWatermarkNoTimers(new Instant(100));
  tester.advanceProcessingTimeNoTimers(new Instant(5010));

  // Fires the GC/EOW timer at the same time as the processing time timer.
  tester.fireTimers(
      new IntervalWindow(new Instant(0), new Instant(100)),
      TimestampedValue.of(TimeDomain.EVENT_TIME, new Instant(100)),
      TimestampedValue.of(TimeDomain.PROCESSING_TIME, new Instant(5010)));

  assertThat(
      tester.extractOutput(),
      contains(
          isSingleWindowedValue(
              equalTo(7), 2, 0, 100, PaneInfo.createPane(true, true, Timing.ON_TIME, 0, 0))));
}
 
Example #28
Source File: KeyedPValueTrackingVisitorTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void noInputUnkeyedOutput() {
  PCollection<KV<Integer, Iterable<Void>>> unkeyed =
      p.apply(
          Create.of(KV.<Integer, Iterable<Void>>of(-1, Collections.emptyList()))
              .withCoder(KvCoder.of(VarIntCoder.of(), IterableCoder.of(VoidCoder.of()))));

  p.traverseTopologically(visitor);
  assertThat(visitor.getKeyedPValues(), not(hasItem(unkeyed)));
}
 
Example #29
Source File: ReduceFnTester.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link ReduceFnTester} for the given {@link WindowingStrategy}, creating a {@link
 * TriggerStateMachine} from its {@link Trigger}.
 */
public static <W extends BoundedWindow>
    ReduceFnTester<Integer, Iterable<Integer>, W> nonCombining(
        WindowingStrategy<?, W> windowingStrategy) throws Exception {
  return new ReduceFnTester<>(
      windowingStrategy,
      TriggerStateMachines.stateMachineForTrigger(
          TriggerTranslation.toProto(windowingStrategy.getTrigger())),
      SystemReduceFn.buffering(VarIntCoder.of()),
      IterableCoder.of(VarIntCoder.of()),
      PipelineOptionsFactory.create(),
      NullSideInputReader.empty());
}
 
Example #30
Source File: TestCountingSource.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getCurrentRecordId() {
  try {
    return encodeToByteArray(KvCoder.of(VarIntCoder.of(), VarIntCoder.of()), getCurrent());
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}