/** * Returns a new {@link TimestampedValue} with the {@link BoundedWindow#TIMESTAMP_MIN_VALUE * minimum timestamp}. */ public static <V> TimestampedValue<V> atMinimumTimestamp(@Nullable V value) { return of(value, BoundedWindow.TIMESTAMP_MIN_VALUE); }
@ProcessElement public void processElement(@Element TimestampedValue<T> element, OutputReceiver<T> r) { r.outputWithTimestamp(element.getValue(), element.getTimestamp()); } }
@Test public void testAtMinimumTimestamp() { TimestampedValue<String> tsv = TimestampedValue.atMinimumTimestamp("foobar"); assertEquals(BoundedWindow.TIMESTAMP_MIN_VALUE, tsv.getTimestamp()); }
@Override public Object structuralValue(TimestampedValue<T> value) { Object structuralValue = valueCoder.structuralValue(value.getValue()); return TimestampedValue.of(structuralValue, value.getTimestamp()); }
@Test public void testNullValue() { TimestampedValue<String> tsv = TimestampedValue.atMinimumTimestamp(null); assertEquals(null, tsv.getValue()); }
@Override public Instant timestamp() { return timestampedValue.getTimestamp(); }
@Test public void testEquality() { new EqualsTester() .addEqualityGroup( TimestampedValue.of("foo", new Instant(1000)), TimestampedValue.of("foo", new Instant(1000))) .addEqualityGroup(TimestampedValue.of("foo", new Instant(2000))) .addEqualityGroup(TimestampedValue.of("bar", new Instant(1000))) .addEqualityGroup( TimestampedValue.of("foo", BoundedWindow.TIMESTAMP_MIN_VALUE), TimestampedValue.atMinimumTimestamp("foo")) .testEquals(); }
@Override public T extractOutput(TimestampedValue<T> accumulator) { return accumulator.getValue(); } }
@Override public TimestampedValue<T> createAccumulator() { return TimestampedValue.atMinimumTimestamp(null); }
@Test public void testValues() { Instant now = Instant.now(); TimestampedValue<String> tsv = TimestampedValue.of("foobar", now); assertEquals(now, tsv.getTimestamp()); assertEquals("foobar", tsv.getValue()); }
private Instant getTimestamp(Object value) { if (value instanceof TimestampedValue) { return ((TimestampedValue) value).getTimestamp(); } else { return Instant.now(); } } }
@Override public T element() { return timestampedValue.getValue(); }
/** For non-timestamped elements. */ @SafeVarargs public final CreateStream<T> nextBatch(T... batchElements) { List<TimestampedValue<T>> timestamped = Lists.newArrayListWithCapacity(batchElements.length); // as TimestampedValue. for (T element : batchElements) { timestamped.add(TimestampedValue.atMinimumTimestamp(element)); } batches.offer(timestamped); return this; }
/** * Adds the specified elements to the source with timestamp equal to the current watermark. * * @return A {@link TestStream.Builder} like this one that will add the provided elements after * all earlier events have completed. */ @SafeVarargs public final Builder<T> addElements(T element, T... elements) { TimestampedValue<T> firstElement = TimestampedValue.of(element, currentWatermark); @SuppressWarnings({"unchecked", "rawtypes"}) TimestampedValue<T>[] remainingElements = new TimestampedValue[elements.length]; for (int i = 0; i < elements.length; i++) { remainingElements[i] = TimestampedValue.of(elements[i], currentWatermark); } return addElements(firstElement, remainingElements); }
/** @deprecated Use {@link TestPipeline} with the {@code DirectRunner}. */ @Deprecated public void processTimestampedElement(TimestampedValue<InputT> element) throws Exception { checkNotNull(element, "Timestamped element cannot be null"); processWindowedElement(element.getValue(), element.getTimestamp(), GlobalWindow.INSTANCE); }
/** * Enqueue next micro-batch elements. This is backed by a {@link Queue} so stream input order * would keep the population order (FIFO). */ @SafeVarargs public final CreateStream<T> nextBatch(TimestampedValue<T>... batchElements) { // validate timestamps if timestamped elements. for (final TimestampedValue<T> timestampedValue : batchElements) { checkArgument( timestampedValue.getTimestamp().isBefore(BoundedWindow.TIMESTAMP_MAX_VALUE), "Elements must have timestamps before %s. Got: %s", BoundedWindow.TIMESTAMP_MAX_VALUE, timestampedValue.getTimestamp()); } batches.offer(Arrays.asList(batchElements)); return this; }
private String tryClaimNextPending(GrowthTracker<String, ?, ?> tracker) { assertTrue(tracker.hasPending()); Map.Entry<HashCode, TimestampedValue<String>> entry = tracker.getNextPending(); tracker.tryClaim(entry.getKey()); return entry.getValue().getValue(); }
/** @deprecated Use {@link TestPipeline} with the {@code DirectRunner}. */ @Deprecated public void processElement(InputT element) throws Exception { processTimestampedElement(TimestampedValue.atMinimumTimestamp(element)); }
private static <OutputT> List<TimestampedValue<OutputT>> addTimestamp( Instant timestamp, List<OutputT> outputs) { List<TimestampedValue<OutputT>> res = Lists.newArrayListWithExpectedSize(outputs.size()); for (OutputT output : outputs) { res.add(TimestampedValue.of(output, timestamp)); } return res; } }
@Override public void encode(TimestampedValue<T> windowedElem, OutputStream outStream) throws IOException { valueCoder.encode(windowedElem.getValue(), outStream); InstantCoder.of().encode(windowedElem.getTimestamp(), outStream); }