Java Code Examples for org.apache.beam.sdk.coders.Coder#NonDeterministicException

The following examples show how to use org.apache.beam.sdk.coders.Coder#NonDeterministicException . 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: ParDo.java    From beam with Apache License 2.0 6 votes vote down vote up
private static void validateStateApplicableForInput(DoFn<?, ?> fn, PCollection<?> input) {
  Coder<?> inputCoder = input.getCoder();
  checkArgument(
      inputCoder instanceof KvCoder,
      "%s requires its input to use %s in order to use state and timers.",
      ParDo.class.getSimpleName(),
      KvCoder.class.getSimpleName());

  KvCoder<?, ?> kvCoder = (KvCoder<?, ?>) inputCoder;
  try {
    kvCoder.getKeyCoder().verifyDeterministic();
  } catch (Coder.NonDeterministicException exc) {
    throw new IllegalArgumentException(
        String.format(
            "%s requires a deterministic key coder in order to use state and timers",
            ParDo.class.getSimpleName()));
  }
}
 
Example 2
Source File: GroupNonMergingWindowsFunctionsTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testGroupByKeyIterator() throws Coder.NonDeterministicException {
  GroupByKeyIterator<String, Integer, GlobalWindow> iteratorUnderTest = createGbkIterator();

  Assert.assertTrue(iteratorUnderTest.hasNext());
  WindowedValue<KV<String, Iterable<Integer>>> k1Win = iteratorUnderTest.next();
  // testing that calling 2x hasNext doesn't move to next key iterator
  Assert.assertTrue(iteratorUnderTest.hasNext());
  Assert.assertTrue(iteratorUnderTest.hasNext());

  Iterator<Integer> valuesIteratorForK1 = k1Win.getValue().getValue().iterator();

  Assert.assertTrue("Now we expect first value for K1 to pop up.", valuesIteratorForK1.hasNext());
  assertEquals(1L, valuesIteratorForK1.next().longValue());
  Assert.assertTrue(valuesIteratorForK1.hasNext());
  Assert.assertTrue(valuesIteratorForK1.hasNext());
  assertEquals(2L, valuesIteratorForK1.next().longValue());

  WindowedValue<KV<String, Iterable<Integer>>> k2Win = iteratorUnderTest.next();
  Iterator<Integer> valuesIteratorForK2 = k2Win.getValue().getValue().iterator();
  assertEquals(3L, valuesIteratorForK2.next().longValue());
}
 
Example 3
Source File: GroupNonMergingWindowsFunctionsTest.java    From beam with Apache License 2.0 6 votes vote down vote up
private <W extends BoundedWindow> GroupByKeyIterator<String, Integer, W> createGbkIterator(
    W window, Coder<W> winCoder, WindowingStrategy<Object, W> winStrategy)
    throws Coder.NonDeterministicException {

  StringUtf8Coder keyCoder = StringUtf8Coder.of();
  final WindowedValue.FullWindowedValueCoder<KV<String, Integer>> winValCoder =
      WindowedValue.getFullCoder(
          KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of()),
          winStrategy.getWindowFn().windowCoder());

  ItemFactory<String, Integer, W> factory =
      ItemFactory.forWindow(keyCoder, winValCoder, winCoder, window);
  List<Tuple2<ByteArray, byte[]>> items =
      Arrays.asList(
          factory.create("k1", 1),
          factory.create("k1", 2),
          factory.create("k2", 3),
          factory.create("k2", 4),
          factory.create("k2", 5));
  return new GroupByKeyIterator<>(items.iterator(), keyCoder, winStrategy, winValCoder);
}
 
Example 4
Source File: SketchFrequencies.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link CountMinSketchFn} combiner with the given input coder. <br>
 * <b>Warning :</b> the coder must be deterministic.
 *
 * @param coder the coder that encodes the elements' type
 */
public static <InputT> CountMinSketchFn<InputT> create(Coder<InputT> coder) {
  try {
    coder.verifyDeterministic();
  } catch (Coder.NonDeterministicException e) {
    throw new IllegalArgumentException(
        "Coder must be deterministic to perform this sketch." + e.getMessage(), e);
  }
  return new CountMinSketchFn<>(coder, 0.01, 0.999);
}
 
Example 5
Source File: ApproximateDistinct.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an {@link ApproximateDistinctFn} combiner with the given input coder.
 *
 * @param coder the coder that encodes the elements' type
 */
public static <InputT> ApproximateDistinctFn<InputT> create(Coder<InputT> coder) {
  try {
    coder.verifyDeterministic();
  } catch (Coder.NonDeterministicException e) {
    throw new IllegalArgumentException(
        "Coder must be deterministic to perform this sketch." + e.getMessage(), e);
  }
  return new ApproximateDistinctFn<>(12, 0, coder);
}
 
Example 6
Source File: GroupNonMergingWindowsFunctions.java    From beam with Apache License 2.0 5 votes vote down vote up
private static boolean isKeyAndWindowCoderConsistentWithEquals(
    Coder<?> keyCoder, Coder<?> windowCoder) {
  try {
    keyCoder.verifyDeterministic();
    windowCoder.verifyDeterministic();
    return keyCoder.consistentWithEquals() && windowCoder.consistentWithEquals();
  } catch (Coder.NonDeterministicException ex) {
    throw new IllegalArgumentException(
        "Coder for both key " + keyCoder + " and " + windowCoder + " must be deterministic", ex);
  }
}
 
Example 7
Source File: GroupNonMergingWindowsFunctions.java    From beam with Apache License 2.0 5 votes vote down vote up
GroupByKeyIterator(
    Iterator<Tuple2<ByteArray, byte[]>> inner,
    Coder<K> keyCoder,
    WindowingStrategy<?, W> windowingStrategy,
    WindowedValue.FullWindowedValueCoder<KV<K, V>> windowedValueCoder)
    throws Coder.NonDeterministicException {

  this.inner = Iterators.peekingIterator(inner);
  this.keyCoder = keyCoder;
  this.windowingStrategy = windowingStrategy;
  this.windowedValueCoder = windowedValueCoder;
}
 
Example 8
Source File: GroupNonMergingWindowsFunctionsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testGroupByKeyIteratorOnNonGlobalWindows() throws Coder.NonDeterministicException {
  Instant now = Instant.now();
  GroupByKeyIterator<String, Integer, IntervalWindow> iteratorUnderTest =
      createGbkIterator(
          new IntervalWindow(now, now.plus(1)),
          IntervalWindow.getCoder(),
          WindowingStrategy.of(FixedWindows.of(Duration.millis(1))));

  Assert.assertTrue(iteratorUnderTest.hasNext());
  WindowedValue<KV<String, Iterable<Integer>>> k1Win = iteratorUnderTest.next();
  // testing that calling 2x hasNext doesn't move to next key iterator
  Assert.assertTrue(iteratorUnderTest.hasNext());
  Assert.assertTrue(iteratorUnderTest.hasNext());

  Iterator<Integer> valuesIteratorForK1 = k1Win.getValue().getValue().iterator();

  Assert.assertTrue("Now we expect first value for K1 to pop up.", valuesIteratorForK1.hasNext());
  assertEquals(1L, valuesIteratorForK1.next().longValue());
  Assert.assertTrue(valuesIteratorForK1.hasNext());
  Assert.assertTrue(valuesIteratorForK1.hasNext());
  assertEquals(2L, valuesIteratorForK1.next().longValue());

  WindowedValue<KV<String, Iterable<Integer>>> k2Win = iteratorUnderTest.next();
  Iterator<Integer> valuesIteratorForK2 = k2Win.getValue().getValue().iterator();
  assertEquals(3L, valuesIteratorForK2.next().longValue());
}
 
Example 9
Source File: Combine.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public void verifyDeterministic() throws Coder.NonDeterministicException {
  inputCoder.verifyDeterministic();
  accumCoder.verifyDeterministic();
}
 
Example 10
Source File: KeyedWorkItemCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public void verifyDeterministic() throws Coder.NonDeterministicException {
  keyCoder.verifyDeterministic();
  timersCoder.verifyDeterministic();
  elemsCoder.verifyDeterministic();
}
 
Example 11
Source File: GroupNonMergingWindowsFunctionsTest.java    From beam with Apache License 2.0 4 votes vote down vote up
private GroupByKeyIterator<String, Integer, GlobalWindow> createGbkIterator()
    throws Coder.NonDeterministicException {
  return createGbkIterator(
      GlobalWindow.INSTANCE, GlobalWindow.Coder.INSTANCE, WindowingStrategy.globalDefault());
}
 
Example 12
Source File: IsmFormat.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public void verifyDeterministic() throws Coder.NonDeterministicException {
  verifyDeterministic(
      this, "Key component coders expected to be deterministic.", keyComponentCoders);
  verifyDeterministic(this, "Value coder expected to be deterministic.", valueCoder);
}