org.apache.beam.sdk.transforms.Sum Java Examples

The following examples show how to use org.apache.beam.sdk.transforms.Sum. 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: PortablePipelineDotRendererTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompositePipeline() {
  p.apply(Create.timestamped(TimestampedValue.of(KV.of(1, 1), new Instant(1))))
      .apply(Window.into(FixedWindows.of(Duration.millis(10))))
      .apply(Sum.integersPerKey());

  assertEquals(
      "digraph {"
          + "    rankdir=LR"
          + "    0 [label=\"Create.TimestampedValues\\n\"]"
          + "    1 [label=\"Window.Into()\\n\"]"
          + "    0 -> 1 [style=solid label=\"Create.TimestampedValues/ParDo(ConvertTimestamps)/ParMultiDo(ConvertTimestamps).output\"]"
          + "    2 [label=\"Combine.perKey(SumInteger)\\nbeam:transform:combine_per_key:v1\"]"
          + "    1 -> 2 [style=solid label=\"Window.Into()/Window.Assign.out\"]"
          + "}",
      PortablePipelineDotRenderer.toDotString(PipelineTranslation.toProto(p))
          .replaceAll(System.lineSeparator(), ""));
}
 
Example #2
Source File: ConfigGeneratorTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testSamzaLocalExecutionEnvironmentConfig() {
  SamzaPipelineOptions options = PipelineOptionsFactory.create().as(SamzaPipelineOptions.class);
  options.setJobName("TestEnvConfig");
  options.setRunner(SamzaRunner.class);
  options.setSamzaExecutionEnvironment(SamzaExecutionEnvironment.LOCAL);

  Pipeline pipeline = Pipeline.create(options);
  pipeline.apply(Create.of(1, 2, 3)).apply(Sum.integersGlobally());

  pipeline.replaceAll(SamzaTransformOverrides.getDefaultOverrides());

  final Map<PValue, String> idMap = PViewToIdMapper.buildIdMap(pipeline);
  final ConfigBuilder configBuilder = new ConfigBuilder(options);
  SamzaPipelineTranslator.createConfig(pipeline, options, idMap, configBuilder);
  final Config config = configBuilder.build();

  assertTrue(
      Maps.difference(config, ConfigBuilder.localRunConfig()).entriesOnlyOnRight().isEmpty());
}
 
Example #3
Source File: WordCount.java    From incubator-nemo with Apache License 2.0 6 votes vote down vote up
/**
 * Static method to generate the word count Beam pipeline.
 * @param options options for the pipeline.
 * @param inputFilePath the input file path.
 * @param outputFilePath the output file path.
 * @return the generated pipeline.
 */
static Pipeline generateWordCountPipeline(final PipelineOptions options,
                                                 final String inputFilePath, final String outputFilePath) {
  final Pipeline p = Pipeline.create(options);
  final PCollection<String> result = GenericSourceSink.read(p, inputFilePath)
    .apply(MapElements.<String, KV<String, Long>>via(new SimpleFunction<String, KV<String, Long>>() {
      @Override
      public KV<String, Long> apply(final String line) {
        final String[] words = line.split(" +");
        final String documentId = words[0] + "#" + words[1];
        final Long count = Long.parseLong(words[2]);
        return KV.of(documentId, count);
      }
    }))
    .apply(Sum.longsPerKey())
    .apply(MapElements.<KV<String, Long>, String>via(new SimpleFunction<KV<String, Long>, String>() {
      @Override
      public String apply(final KV<String, Long> kv) {
        return kv.getKey() + ": " + kv.getValue();
      }
    }));
  GenericSourceSink.write(result, outputFilePath);
  return p;
}
 
Example #4
Source File: CombineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testCombinePerKeyWithSlidingWindows() {
  PCollection<KV<Integer, Integer>> input =
      pipeline
          .apply(
              Create.timestamped(
                  TimestampedValue.of(KV.of(1, 1), new Instant(1)),
                  TimestampedValue.of(KV.of(1, 3), new Instant(2)),
                  TimestampedValue.of(KV.of(1, 5), new Instant(3)),
                  TimestampedValue.of(KV.of(1, 2), new Instant(1)),
                  TimestampedValue.of(KV.of(1, 4), new Instant(2)),
                  TimestampedValue.of(KV.of(1, 6), new Instant(3))))
          .apply(Window.into(SlidingWindows.of(Duration.millis(3)).every(Duration.millis(1))))
          .apply(Sum.integersPerKey());
  PAssert.that(input)
      .containsInAnyOrder(
          KV.of(1, 1 + 2),
          KV.of(1, 1 + 2 + 3 + 4),
          KV.of(1, 1 + 3 + 5 + 2 + 4 + 6),
          KV.of(1, 3 + 4 + 5 + 6),
          KV.of(1, 5 + 6));
  pipeline.run();
}
 
Example #5
Source File: CombineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testCombinePerKeyPreservesWindowing() {
  PCollection<KV<Integer, Integer>> input =
      pipeline
          .apply(
              Create.timestamped(
                  TimestampedValue.of(KV.of(1, 1), new Instant(1)),
                  TimestampedValue.of(KV.of(1, 3), new Instant(2)),
                  TimestampedValue.of(KV.of(1, 5), new Instant(11)),
                  TimestampedValue.of(KV.of(2, 2), new Instant(3)),
                  TimestampedValue.of(KV.of(2, 4), new Instant(11)),
                  TimestampedValue.of(KV.of(2, 6), new Instant(12))))
          .apply(Window.into(FixedWindows.of(Duration.millis(10))))
          .apply(Sum.integersPerKey());
  PAssert.that(input).containsInAnyOrder(KV.of(1, 4), KV.of(1, 5), KV.of(2, 2), KV.of(2, 10));
  pipeline.run();
}
 
Example #6
Source File: CombineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testCombineGloballyPreservesWindowing() {
  PCollection<Integer> input =
      pipeline
          .apply(
              Create.timestamped(
                  TimestampedValue.of(1, new Instant(1)),
                  TimestampedValue.of(2, new Instant(2)),
                  TimestampedValue.of(3, new Instant(11)),
                  TimestampedValue.of(4, new Instant(3)),
                  TimestampedValue.of(5, new Instant(11)),
                  TimestampedValue.of(6, new Instant(12))))
          .apply(Window.into(FixedWindows.of(Duration.millis(10))))
          .apply(Combine.globally(Sum.ofIntegers()).withoutDefaults());
  PAssert.that(input).containsInAnyOrder(7, 14);
}
 
Example #7
Source File: BeamBuiltinAggregations.java    From beam with Apache License 2.0 6 votes vote down vote up
/** {@link CombineFn} for Sum based on {@link Sum} and {@link Combine.BinaryCombineFn}. */
static CombineFn createSum(Schema.FieldType fieldType) {
  switch (fieldType.getTypeName()) {
    case INT32:
      return Sum.ofIntegers();
    case INT16:
      return new ShortSum();
    case BYTE:
      return new ByteSum();
    case INT64:
      return Sum.ofLongs();
    case FLOAT:
      return new FloatSum();
    case DOUBLE:
      return Sum.ofDoubles();
    case DECIMAL:
      return new BigDecimalSum();
    default:
      throw new UnsupportedOperationException(
          String.format("[%s] is not supported in SUM", fieldType));
  }
}
 
Example #8
Source File: WindowAssignTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testWindowAssign() {
  PCollection<Integer> input =
      pipeline
          .apply(
              Create.timestamped(
                  TimestampedValue.of(1, new Instant(1)),
                  TimestampedValue.of(2, new Instant(2)),
                  TimestampedValue.of(3, new Instant(3)),
                  TimestampedValue.of(4, new Instant(10)),
                  TimestampedValue.of(5, new Instant(11))))
          .apply(Window.into(FixedWindows.of(Duration.millis(10))))
          .apply(Sum.integersGlobally().withoutDefaults());
  PAssert.that(input).containsInAnyOrder(6, 9);
  pipeline.run();
}
 
Example #9
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that if end-of-window and GC timers come in together, that the pane is correctly marked
 * as final.
 */
@Test
public void testCombiningAccumulatingEventTime() throws Exception {
  WindowingStrategy<?, IntervalWindow> strategy =
      WindowingStrategy.of((WindowFn<?, IntervalWindow>) FixedWindows.of(Duration.millis(100)))
          .withTimestampCombiner(TimestampCombiner.EARLIEST)
          .withMode(AccumulationMode.ACCUMULATING_FIRED_PANES)
          .withAllowedLateness(Duration.millis(1))
          .withTrigger(Repeatedly.forever(AfterWatermark.pastEndOfWindow()));

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

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

  tester.advanceInputWatermark(new Instant(1000));

  assertThat(
      tester.extractOutput(),
      contains(
          isSingleWindowedValue(
              equalTo(7), 2, 0, 100, PaneInfo.createPane(true, true, Timing.ON_TIME, 0, 0))));
}
 
Example #10
Source File: CombineFnTesterTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void usesMatcher() {
  final AtomicBoolean matcherUsed = new AtomicBoolean();
  Matcher<Integer> matcher =
      new TypeSafeMatcher<Integer>() {
        @Override
        public void describeTo(Description description) {}

        @Override
        protected boolean matchesSafely(Integer item) {
          matcherUsed.set(true);
          return item == 30;
        }
      };
  CombineFnTester.testCombineFn(
      Sum.ofIntegers(), Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), matcher);
  assertThat(matcherUsed.get(), is(true));
  try {
    CombineFnTester.testCombineFn(
        Sum.ofIntegers(), Arrays.asList(1, 2, 3, 4, 5), Matchers.not(Matchers.equalTo(15)));
  } catch (AssertionError ignored) {
    // Success! Return to avoid the call to fail();
    return;
  }
  fail("The matcher should have failed, throwing an error");
}
 
Example #11
Source File: GroupTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
@Category(NeedsRunner.class)
public void testAggregateBaseValuesGlobally() {
  Collection<BasicEnum> elements =
      Lists.newArrayList(
          BasicEnum.of("a", BasicEnum.Test.ONE), BasicEnum.of("a", BasicEnum.Test.TWO));

  PCollection<Row> aggregate =
      pipeline
          .apply(Create.of(elements))
          .apply(
              Group.<BasicEnum>globally()
                  .aggregateFieldBaseValue("enumeration", Sum.ofIntegers(), "enum_sum"));
  Schema aggregateSchema = Schema.builder().addInt32Field("enum_sum").build();
  Row expectedRow = Row.withSchema(aggregateSchema).addValues(3).build();
  PAssert.that(aggregate).containsInAnyOrder(expectedRow);

  pipeline.run();
}
 
Example #12
Source File: AdaptiveThrottler.java    From beam with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
AdaptiveThrottler(long samplePeriodMs, long sampleUpdateMs, double overloadRatio, Random random) {
  allRequests =
      new MovingFunction(
          samplePeriodMs,
          sampleUpdateMs,
          1 /* numSignificantBuckets */,
          1 /* numSignificantSamples */,
          Sum.ofLongs());
  successfulRequests =
      new MovingFunction(
          samplePeriodMs,
          sampleUpdateMs,
          1 /* numSignificantBuckets */,
          1 /* numSignificantSamples */,
          Sum.ofLongs());
  this.overloadRatio = overloadRatio;
  this.random = random;
}
 
Example #13
Source File: CombineLoadTest.java    From beam with Apache License 2.0 6 votes vote down vote up
public PTransform<PCollection<KV<byte[], Long>>, ? extends PCollection> getPerKeyCombiner(
    CombinerType combinerType) {
  switch (combinerType) {
    case MEAN:
      return Mean.perKey();
    case TOP_LARGEST:
      Preconditions.checkArgument(
          options.getTopCount() != null,
          "You should set \"--topCount\" option to use TOP combiners.");
      return Top.largestPerKey(options.getTopCount());
    case SUM:
      return Sum.longsPerKey();
    case COUNT:
      return Count.perKey();
    default:
      throw new IllegalArgumentException("No such combiner!");
  }
}
 
Example #14
Source File: MovingAverage.java    From beam with Apache License 2.0 6 votes vote down vote up
public MovingAverage(
    long samplePeriodMs,
    long sampleUpdateMs,
    int numSignificantBuckets,
    int numSignificantSamples) {
  sum =
      new MovingFunction(
          samplePeriodMs,
          sampleUpdateMs,
          numSignificantBuckets,
          numSignificantSamples,
          Sum.ofLongs());
  count =
      new MovingFunction(
          samplePeriodMs,
          sampleUpdateMs,
          numSignificantBuckets,
          numSignificantSamples,
          Sum.ofLongs());
}
 
Example #15
Source File: PartialGroupByKeyParDoFnsTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithCombinerAndStreaming() throws Exception {
  StreamingOptions options = PipelineOptionsFactory.as(StreamingOptions.class);
  options.setStreaming(true);

  Coder keyCoder = StringUtf8Coder.of();
  Coder valueCoder = BigEndianIntegerCoder.of();
  KvCoder<String, Integer> kvCoder = KvCoder.of(keyCoder, valueCoder);

  TestOutputReceiver receiver =
      new TestOutputReceiver(
          new ElementByteSizeObservableCoder(WindowedValue.getValueOnlyCoder(kvCoder)),
          counterSet,
          NameContextsForTests.nameContextForTest());

  ParDoFn pgbk =
      PartialGroupByKeyParDoFns.create(
          options,
          kvCoder,
          AppliedCombineFn.withInputCoder(
              Sum.ofIntegers(), CoderRegistry.createDefault(), kvCoder),
          NullSideInputReader.empty(),
          receiver,
          null);
  assertTrue(pgbk instanceof SimplePartialGroupByKeyParDoFn);
}
 
Example #16
Source File: ConfigGeneratorTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testSamzaYarnExecutionEnvironmentConfig() {
  final String yarnPackagePath = "yarn.package.path";
  SamzaPipelineOptions options = PipelineOptionsFactory.create().as(SamzaPipelineOptions.class);
  options.setJobName("TestEnvConfig");
  options.setRunner(SamzaRunner.class);
  options.setSamzaExecutionEnvironment(SamzaExecutionEnvironment.YARN);
  options.setConfigOverride(
      ImmutableMap.<String, String>builder()
          .put(
              yarnPackagePath,
              "file://${basedir}/target/${project.artifactId}-${pom.version}-dist.tar.gz")
          .build());

  Pipeline pipeline = Pipeline.create(options);
  pipeline.apply(Create.of(1, 2, 3)).apply(Sum.integersGlobally());

  pipeline.replaceAll(SamzaTransformOverrides.getDefaultOverrides());

  final Map<PValue, String> idMap = PViewToIdMapper.buildIdMap(pipeline);
  final ConfigBuilder configBuilder = new ConfigBuilder(options);
  SamzaPipelineTranslator.createConfig(pipeline, options, idMap, configBuilder);
  try {
    Config config = configBuilder.build();
    assertEquals(config.get(APP_RUNNER_CLASS), RemoteApplicationRunner.class.getName());
    assertEquals(config.get(JOB_FACTORY_CLASS), YarnJobFactory.class.getName());
  } catch (IllegalArgumentException e) {
    throw new AssertionError(
        String.format(
            "Failed to validate correct configs for %s samza execution environment",
            SamzaExecutionEnvironment.YARN),
        e);
  }
}
 
Example #17
Source File: BeamModel.java    From streamingbook with Apache License 2.0 5 votes vote down vote up
@Override
public PCollection<String> expand(PCollection<KV<String, Integer>> input) {
    return input
        .apply(Window.<KV<String, Integer>>into(FixedWindows.of(TWO_MINUTES)))
        .apply(Sum.integersPerKey())
        .apply(ParDo.of(new FormatAsStrings()));
}
 
Example #18
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 it is just ignored.
 */
@Test
public void testLateProcessingTimeTimer() 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);

  // After this advancement, the window is expired and only the GC process
  // should be allowed to touch it
  tester.advanceInputWatermarkNoTimers(new Instant(100));

  // This should not output
  tester.advanceProcessingTime(new Instant(6000));

  assertThat(tester.extractOutput(), emptyIterable());
}
 
Example #19
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnElementCombiningDiscarding() throws Exception {
  // Test basic execution of a trigger using a non-combining window set and discarding mode.
  WindowingStrategy<?, IntervalWindow> strategy =
      WindowingStrategy.of((WindowFn<?, IntervalWindow>) FixedWindows.of(Duration.millis(10)))
          .withTimestampCombiner(TimestampCombiner.EARLIEST)
          .withMode(AccumulationMode.DISCARDING_FIRED_PANES)
          .withAllowedLateness(Duration.millis(100));

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

  injectElement(tester, 2);

  when(mockTriggerStateMachine.shouldFire(anyTriggerContext())).thenReturn(true);
  injectElement(tester, 3);

  when(mockTriggerStateMachine.shouldFire(anyTriggerContext())).thenReturn(true);
  triggerShouldFinish(mockTriggerStateMachine);
  injectElement(tester, 4);

  // This element shouldn't be seen, because the trigger has finished
  injectElement(tester, 6);

  assertThat(
      tester.extractOutput(),
      contains(
          isSingleWindowedValue(equalTo(5), 2, 0, 10),
          isSingleWindowedValue(equalTo(4), 4, 0, 10)));
  assertTrue(tester.isMarkedFinished(firstWindow));
  tester.assertHasOnlyGlobalAndFinishedSetsFor(firstWindow);
}
 
Example #20
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that with the default trigger we will not produce two ON_TIME panes, even if there are
 * two outputs that are both candidates.
 */
@Test
public void testOnlyOneOnTimePane() throws Exception {
  WindowingStrategy<?, IntervalWindow> strategy =
      WindowingStrategy.of((WindowFn<?, IntervalWindow>) FixedWindows.of(Duration.millis(10)))
          .withTrigger(DefaultTrigger.of())
          .withMode(AccumulationMode.ACCUMULATING_FIRED_PANES)
          .withAllowedLateness(Duration.millis(100));

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

  tester.advanceInputWatermark(new Instant(0));

  int value1 = 1;
  int value2 = 3;

  // A single element that should be in the ON_TIME output
  tester.injectElements(TimestampedValue.of(value1, new Instant(1)));

  // Should fire ON_TIME
  tester.advanceInputWatermark(new Instant(10));

  // The DefaultTrigger should cause output labeled LATE, even though it does not have to be
  // labeled as such.
  tester.injectElements(TimestampedValue.of(value2, new Instant(3)));

  List<WindowedValue<Integer>> output = tester.extractOutput();
  assertEquals(2, output.size());

  assertThat(output.get(0), isWindowedValue(equalTo(value1)));
  assertThat(output.get(1), isWindowedValue(equalTo(value1 + value2)));

  assertThat(
      output.get(0),
      WindowMatchers.valueWithPaneInfo(PaneInfo.createPane(true, false, Timing.ON_TIME, 0, 0)));
  assertThat(
      output.get(1),
      WindowMatchers.valueWithPaneInfo(PaneInfo.createPane(false, false, Timing.LATE, 1, 1)));
}
 
Example #21
Source File: GroupAlsoByWindowsAndCombineDoFnTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testCombinesIntoSessions() throws Exception {
  CombineFn<Long, ?, Long> combineFn = Sum.ofLongs();

  GroupAlsoByWindowProperties.combinesElementsPerSession(
      new GABWAndCombineDoFnFactory<>(combineFn), combineFn);
}
 
Example #22
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Tests that a processing time timer does not cause window GC. */
@Test
public void testProcessingTimeTimerDoesNotGc() 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.advanceProcessingTime(new Instant(10000));

  tester.assertHasOnlyGlobalAndStateFor(new IntervalWindow(new Instant(0), new Instant(100)));

  assertThat(
      tester.extractOutput(),
      contains(
          isSingleWindowedValue(
              equalTo(7), 2, 0, 100, PaneInfo.createPane(true, false, Timing.EARLY, 0, 0))));
}
 
Example #23
Source File: WatermarkCallbackExecutorTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  PCollection<Integer> created = p.apply(Create.of(1, 2, 3));
  PCollection<Integer> summed = created.apply(Sum.integersGlobally());
  DirectGraphs.performDirectOverrides(p);
  DirectGraph graph = DirectGraphs.getGraph(p);
  create = graph.getProducer(created);
  sum = graph.getProducer(summed);
}
 
Example #24
Source File: PTransformMatchersTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void writeWithRunnerDeterminedSharding() {
  ResourceId outputDirectory = LocalResources.fromString("/foo/bar", true /* isDirectory */);
  FilenamePolicy policy =
      DefaultFilenamePolicy.fromStandardParameters(
          StaticValueProvider.of(outputDirectory),
          DefaultFilenamePolicy.DEFAULT_UNWINDOWED_SHARD_TEMPLATE,
          "",
          false);
  WriteFiles<Integer, Void, Integer> write =
      WriteFiles.to(
          new FileBasedSink<Integer, Void, Integer>(
              StaticValueProvider.of(outputDirectory), DynamicFileDestinations.constant(policy)) {
            @Override
            public WriteOperation<Void, Integer> createWriteOperation() {
              return null;
            }
          });
  assertThat(
      PTransformMatchers.writeWithRunnerDeterminedSharding().matches(appliedWrite(write)),
      is(true));

  WriteFiles<Integer, Void, Integer> withStaticSharding = write.withNumShards(3);
  assertThat(
      PTransformMatchers.writeWithRunnerDeterminedSharding()
          .matches(appliedWrite(withStaticSharding)),
      is(false));

  WriteFiles<Integer, Void, Integer> withCustomSharding =
      write.withSharding(Sum.integersGlobally().asSingletonView());
  assertThat(
      PTransformMatchers.writeWithRunnerDeterminedSharding()
          .matches(appliedWrite(withCustomSharding)),
      is(false));
}
 
Example #25
Source File: CombineTranslationTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Parameters(name = "{index}: {0}")
public static Iterable<Combine.CombineFn<Integer, ?, ?>> params() {
  BinaryCombineIntegerFn sum = Sum.ofIntegers();
  CombineFn<Integer, ?, Long> count = Count.combineFn();
  TestCombineFn test = new TestCombineFn();
  return ImmutableList.<CombineFn<Integer, ?, ?>>builder()
      .add(sum)
      .add(count)
      .add(test)
      .build();
}
 
Example #26
Source File: WindmillStateInternalsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewCombiningNoFetch() throws Exception {
  GroupingState<Integer, Integer> value = underTestNewKey.state(NAMESPACE, COMBINING_ADDR);

  assertThat(value.isEmpty().read(), Matchers.is(true));
  assertThat(value.read(), Matchers.is(Sum.ofIntegers().identity()));
  assertThat(value.isEmpty().read(), Matchers.is(false));

  // Shouldn't need to read from windmill for this.
  Mockito.verifyZeroInteractions(mockReader);
}
 
Example #27
Source File: StateFetcherTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyFetchGlobalData() throws Exception {
  StateFetcher fetcher = new StateFetcher(server);

  ByteString encodedIterable = ByteString.EMPTY;

  PCollectionView<Long> view =
      TestPipeline.create()
          .apply(Create.empty(VarLongCoder.of()))
          .apply(Sum.longsGlobally().asSingletonView());

  String tag = view.getTagInternal().getId();

  // Test three calls in a row. First, data is not ready, then data is ready,
  // then the data is already cached.
  when(server.getSideInputData(any(Windmill.GlobalDataRequest.class)))
      .thenReturn(buildGlobalDataResponse(tag, ByteString.EMPTY, true, encodedIterable));

  assertEquals(
      0L,
      (long)
          fetcher
              .fetchSideInput(
                  view,
                  GlobalWindow.INSTANCE,
                  STATE_FAMILY,
                  SideInputState.UNKNOWN,
                  readStateSupplier)
              .orNull());

  verify(server).getSideInputData(buildGlobalDataRequest(tag, ByteString.EMPTY));
  verifyNoMoreInteractions(server);
}
 
Example #28
Source File: BatchGroupAlsoByWindowFnsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCombiningMerging() throws Exception {
  AppliedCombineFn<String, Long, ?, Long> appliedFn =
      AppliedCombineFn.withInputCoder(
          Sum.ofLongs(),
          CoderRegistry.createDefault(),
          KvCoder.of(StringUtf8Coder.of(), VarLongCoder.of()));
  WindowingStrategy<?, IntervalWindow> windowingStrategy =
      WindowingStrategy.of(Sessions.withGapDuration(Duration.millis(10)));

  assertThat(
      BatchGroupAlsoByWindowsDoFns.create(windowingStrategy, appliedFn),
      instanceOf(BatchGroupAlsoByWindowAndCombineFn.class));
}
 
Example #29
Source File: GroupAlsoByWindowsAndCombineDoFnTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testCombinesIntoSessionsWithEndOfWindowTimestamp() throws Exception {
  CombineFn<Long, ?, Long> combineFn = Sum.ofLongs();

  GroupAlsoByWindowProperties.combinesElementsPerSessionWithEndOfWindowTimestamp(
      new GABWAndCombineDoFnFactory<>(combineFn), combineFn);
}
 
Example #30
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that when a processing time timers comes in after a window is expired and GC'd it does
 * not cause a spurious output.
 */
@Test
public void testCombiningAccumulatingProcessingTimeSeparateBundles() 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.advanceInputWatermark(new Instant(100));
  tester.advanceProcessingTime(new Instant(5011));

  assertThat(
      tester.extractOutput(),
      contains(
          isSingleWindowedValue(
              equalTo(7), 2, 0, 100, PaneInfo.createPane(true, true, Timing.ON_TIME, 0, 0))));
}