use of org.apache.kafka.streams.state.ValueAndTimestamp in project kafka by apache.
the class KGroupedTableImplTest method shouldReduceAndMaterializeResults.
@Test
public void shouldReduceAndMaterializeResults() {
final KeyValueMapper<String, Number, KeyValue<String, Integer>> intProjection = (key, value) -> KeyValue.pair(key, value.intValue());
final KTable<String, Integer> reduced = builder.table(topic, Consumed.with(Serdes.String(), Serdes.Double())).groupBy(intProjection).reduce(MockReducer.INTEGER_ADDER, MockReducer.INTEGER_SUBTRACTOR, Materialized.<String, Integer, KeyValueStore<Bytes, byte[]>>as("reduce").withKeySerde(Serdes.String()).withValueSerde(Serdes.Integer()));
final MockApiProcessorSupplier<String, Integer, Void, Void> supplier = getReducedResults(reduced);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
assertReduced(supplier.theCapturedProcessor().lastValueAndTimestampPerKey(), topic, driver);
{
final KeyValueStore<String, Integer> reduce = driver.getKeyValueStore("reduce");
assertThat(reduce.get("A"), equalTo(5));
assertThat(reduce.get("B"), equalTo(6));
}
{
final KeyValueStore<String, ValueAndTimestamp<Integer>> reduce = driver.getTimestampedKeyValueStore("reduce");
assertThat(reduce.get("A"), equalTo(ValueAndTimestamp.make(5, 50L)));
assertThat(reduce.get("B"), equalTo(ValueAndTimestamp.make(6, 30L)));
}
}
}
use of org.apache.kafka.streams.state.ValueAndTimestamp in project kafka by apache.
the class KGroupedStreamImplTest method doCountSessionWindows.
private void doCountSessionWindows(final MockApiProcessorSupplier<Windowed<String>, Long, Void, Void> supplier) {
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
inputTopic.pipeInput("1", "1", 10);
inputTopic.pipeInput("2", "2", 15);
inputTopic.pipeInput("1", "1", 30);
inputTopic.pipeInput("1", "1", 70);
inputTopic.pipeInput("1", "1", 100);
inputTopic.pipeInput("1", "1", 90);
}
final Map<Windowed<String>, ValueAndTimestamp<Long>> result = supplier.theCapturedProcessor().lastValueAndTimestampPerKey();
assertEquals(ValueAndTimestamp.make(2L, 30L), result.get(new Windowed<>("1", new SessionWindow(10L, 30L))));
assertEquals(ValueAndTimestamp.make(1L, 15L), result.get(new Windowed<>("2", new SessionWindow(15L, 15L))));
assertEquals(ValueAndTimestamp.make(3L, 100L), result.get(new Windowed<>("1", new SessionWindow(70L, 100L))));
}
use of org.apache.kafka.streams.state.ValueAndTimestamp in project kafka by apache.
the class KGroupedStreamImplTest method doAggregateSessionWindows.
private void doAggregateSessionWindows(final MockApiProcessorSupplier<Windowed<String>, Integer, Void, Void> supplier) {
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
inputTopic.pipeInput("1", "1", 10);
inputTopic.pipeInput("2", "2", 15);
inputTopic.pipeInput("1", "1", 30);
inputTopic.pipeInput("1", "1", 70);
inputTopic.pipeInput("1", "1", 100);
inputTopic.pipeInput("1", "1", 90);
}
final Map<Windowed<String>, ValueAndTimestamp<Integer>> result = supplier.theCapturedProcessor().lastValueAndTimestampPerKey();
assertEquals(ValueAndTimestamp.make(2, 30L), result.get(new Windowed<>("1", new SessionWindow(10L, 30L))));
assertEquals(ValueAndTimestamp.make(1, 15L), result.get(new Windowed<>("2", new SessionWindow(15L, 15L))));
assertEquals(ValueAndTimestamp.make(3, 100L), result.get(new Windowed<>("1", new SessionWindow(70L, 100L))));
}
use of org.apache.kafka.streams.state.ValueAndTimestamp in project kafka by apache.
the class MeteredTimestampedKeyValueStoreTest method shouldGetAllFromInnerStoreAndRecordAllMetric.
@Test
public void shouldGetAllFromInnerStoreAndRecordAllMetric() {
expect(inner.all()).andReturn(new KeyValueIteratorStub<>(Collections.singletonList(byteKeyValueTimestampPair).iterator()));
init();
final KeyValueIterator<String, ValueAndTimestamp<String>> iterator = metered.all();
assertThat(iterator.next().value, equalTo(VALUE_AND_TIMESTAMP));
assertFalse(iterator.hasNext());
iterator.close();
final KafkaMetric metric = metric(new MetricName("all-rate", STORE_LEVEL_GROUP, "", tags));
assertTrue((Double) metric.metricValue() > 0);
verify(inner);
}
use of org.apache.kafka.streams.state.ValueAndTimestamp in project kafka by apache.
the class MeteredTimestampedKeyValueStoreTest method shouldGetRangeFromInnerStoreAndRecordRangeMetric.
@Test
public void shouldGetRangeFromInnerStoreAndRecordRangeMetric() {
expect(inner.range(KEY_BYTES, KEY_BYTES)).andReturn(new KeyValueIteratorStub<>(Collections.singletonList(byteKeyValueTimestampPair).iterator()));
init();
final KeyValueIterator<String, ValueAndTimestamp<String>> iterator = metered.range(KEY, KEY);
assertThat(iterator.next().value, equalTo(VALUE_AND_TIMESTAMP));
assertFalse(iterator.hasNext());
iterator.close();
final KafkaMetric metric = metric("range-rate");
assertTrue((Double) metric.metricValue() > 0);
verify(inner);
}
Aggregations