@Test public void testDisplayData() { Top.Natural<Integer> comparer = new Top.Natural<>(); Combine.Globally<Integer, Integer> max = Max.globally(comparer); assertThat(DisplayData.from(max), hasDisplayItem("comparer", comparer.getClass())); } }
/** {@link CombineFn} for MAX based on {@link Max} and {@link Combine.BinaryCombineFn}. */ static CombineFn createMax(Schema.TypeName fieldType) { switch (fieldType) { case BOOLEAN: case INT16: case BYTE: case FLOAT: case DATETIME: case DECIMAL: return new CustMax<>(); case INT32: return Max.ofIntegers(); case INT64: return Max.ofLongs(); case DOUBLE: return Max.ofDoubles(); default: throw new UnsupportedOperationException( String.format("[%s] is not support in MAX", fieldType)); } }
@Test public void testMaxGetNames() { assertEquals("Combine.globally(MaxInteger)", Max.integersGlobally().getName()); assertEquals("Combine.globally(MaxDouble)", Max.doublesGlobally().getName()); assertEquals("Combine.globally(MaxLong)", Max.longsGlobally().getName()); assertEquals("Combine.perKey(MaxInteger)", Max.integersPerKey().getName()); assertEquals("Combine.perKey(MaxDouble)", Max.doublesPerKey().getName()); assertEquals("Combine.perKey(MaxLong)", Max.longsPerKey().getName()); }
@Test public void testMaxIntegerFn() { testCombineFn(Max.ofIntegers(), Lists.newArrayList(1, 2, 3, 4), 4); }
/** * Returns a {@code PTransform} that takes an input {@code PCollection<T>} and returns a {@code * PCollection<T>} whose contents is the maximum according to the natural ordering of {@code T} of * the input {@code PCollection}'s elements, or {@code null} if there are no elements. */ public static <T extends Comparable<? super T>> Combine.Globally<T, T> globally() { return Combine.globally(Max.<T>naturalOrder()); }
@Test public void testMaxDoubleFn() { testCombineFn(Max.ofDoubles(), Lists.newArrayList(1.0, 2.0, 3.0, 4.0), 4.0); }
@Test public void testMaxLongFn() { testCombineFn(Max.ofLongs(), Lists.newArrayList(1L, 2L, 3L, 4L), 4L); }
@Override public PCollection<TableRow> expand(PCollection<TableRow> rows) { // row... => <month, mean_temp> ... PCollection<KV<Integer, Double>> temps = rows.apply(ParDo.of(new ExtractTempFn())); // month, mean_temp... => <month, max mean temp>... PCollection<KV<Integer, Double>> tempMaxes = temps.apply(Max.doublesPerKey()); // <month, max>... => row... PCollection<TableRow> results = tempMaxes.apply(ParDo.of(new FormatMaxesFn())); return results; } }
@Test public void testCombinerNames() { Combine.PerKey<String, Integer, Integer> min = Min.integersPerKey(); Combine.PerKey<String, Integer, Integer> max = Max.integersPerKey(); Combine.PerKey<String, Integer, Double> mean = Mean.perKey(); Combine.PerKey<String, Integer, Integer> sum = Sum.integersPerKey(); assertThat(min.getName(), equalTo("Combine.perKey(MinInteger)")); assertThat(max.getName(), equalTo("Combine.perKey(MaxInteger)")); assertThat(mean.getName(), equalTo("Combine.perKey(Mean)")); assertThat(sum.getName(), equalTo("Combine.perKey(SumInteger)")); }
assertEquals( nameToTransformClass.get("original_application/custom_name"), Max.integersGlobally().getClass());
@Test public void testDuplicatedTagsKeyed() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("it is already present in the composition"); TupleTag<Integer> tag = new TupleTag<>(); CombineFns.compose() .with(new GetIntegerFunction(), Max.ofIntegers(), tag) .with(new GetIntegerFunction(), Min.ofIntegers(), tag); }
/** * Returns a {@code PTransform} that takes an input {@code PCollection<KV<K, T>>} and returns a * {@code PCollection<KV<K, T>>} that contains an output element mapping each distinct key in the * input {@code PCollection} to the maximum according to the natural ordering of {@code T} of the * values associated with that key in the input {@code PCollection}. * * <p>See {@link Combine.PerKey} for how this affects timestamps and windowing. */ public static <K, T extends Comparable<? super T>> Combine.PerKey<K, T, T> perKey() { return Combine.perKey(Max.<T>naturalOrder()); }
@Test public void testDoubleStats() { for (TestCase<Double> t : DOUBLE_CASES) { assertEquals(t.sum, Sum.ofDoubles().apply(t.data), DOUBLE_COMPARISON_ACCURACY); assertEquals(t.min, Min.ofDoubles().apply(t.data), DOUBLE_COMPARISON_ACCURACY); assertEquals(t.max, Max.ofDoubles().apply(t.data), DOUBLE_COMPARISON_ACCURACY); assertEquals(t.mean, Mean.<Double>of().apply(t.data), DOUBLE_COMPARISON_ACCURACY); } }
@Test public void testLongStats() { for (TestCase<Long> t : LONG_CASES) { assertEquals(t.sum, Sum.ofLongs().apply(t.data)); assertEquals(t.min, Min.ofLongs().apply(t.data)); assertEquals(t.max, Max.ofLongs().apply(t.data)); assertEquals(t.mean, Mean.<Long>of().apply(t.data)); } } }
@Test public void testDuplicatedTags() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("it is already present in the composition"); TupleTag<Integer> tag = new TupleTag<>(); CombineFns.compose() .with(new GetIntegerFunction(), Max.ofIntegers(), tag) .with(new GetIntegerFunction(), Min.ofIntegers(), tag); }
public static void addCountingAsserts(PCollection<Long> input, long numElements) { // Count == numElements PAssert.thatSingleton(input.apply("Count", Count.globally())).isEqualTo(numElements); // Unique count == numElements PAssert.thatSingleton(input.apply(Distinct.create()).apply("UniqueCount", Count.globally())) .isEqualTo(numElements); // Min == 0 PAssert.thatSingleton(input.apply("Min", Min.globally())).isEqualTo(0L); // Max == numElements-1 PAssert.thatSingleton(input.apply("Max", Max.globally())).isEqualTo(numElements - 1); }
@Test public void testInstantStats() { assertEquals( new Instant(1000), Min.<Instant>naturalOrder().apply(Arrays.asList(new Instant(1000), new Instant(2000)))); assertEquals(null, Min.<Instant>naturalOrder().apply(Collections.emptyList())); assertEquals( new Instant(5000), Min.naturalOrder(new Instant(5000)).apply(Collections.emptyList())); assertEquals( new Instant(2000), Max.<Instant>naturalOrder().apply(Arrays.asList(new Instant(1000), new Instant(2000)))); assertEquals(null, Max.<Instant>naturalOrder().apply(Collections.emptyList())); assertEquals( new Instant(5000), Max.naturalOrder(new Instant(5000)).apply(Collections.emptyList())); }
@Test public void testIntegerStats() { for (TestCase<Integer> t : INTEGER_CASES) { assertEquals(t.sum, Sum.ofIntegers().apply(t.data)); assertEquals(t.min, Min.ofIntegers().apply(t.data)); assertEquals(t.max, Max.ofIntegers().apply(t.data)); assertEquals(t.mean, Mean.<Integer>of().apply(t.data)); } }
public static void addCountingAsserts( PCollection<Long> input, long count, long uniqueCount, long min, long max) { PAssert.thatSingleton(input.apply("Count", Count.globally())).isEqualTo(count); PAssert.thatSingleton(input.apply(Distinct.create()).apply("UniqueCount", Count.globally())) .isEqualTo(uniqueCount); PAssert.thatSingleton(input.apply("Min", Min.globally())).isEqualTo(min); PAssert.thatSingleton(input.apply("Max", Max.globally())).isEqualTo(max); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testCombiningValueEquality() { Combine.BinaryCombineIntegerFn maxFn = Max.ofIntegers(); Coder<Integer> input1 = VarIntCoder.of(); Coder<Integer> input2 = BigEndianIntegerCoder.of();