Search in sources :

Example 16 with ValueAndTimestamp

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)));
        }
    }
}
Also used : MockInitializer(org.apache.kafka.test.MockInitializer) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Assert.assertThrows(org.junit.Assert.assertThrows) MockReducer(org.apache.kafka.test.MockReducer) TopologyException(org.apache.kafka.streams.errors.TopologyException) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) KGroupedTable(org.apache.kafka.streams.kstream.KGroupedTable) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Map(java.util.Map) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockMapper(org.apache.kafka.test.MockMapper) KTable(org.apache.kafka.streams.kstream.KTable) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) Properties(java.util.Properties) DoubleSerializer(org.apache.kafka.common.serialization.DoubleSerializer) Consumed(org.apache.kafka.streams.kstream.Consumed) KeyValue(org.apache.kafka.streams.KeyValue) Test(org.junit.Test) Grouped(org.apache.kafka.streams.kstream.Grouped) MockAggregator(org.apache.kafka.test.MockAggregator) Bytes(org.apache.kafka.common.utils.Bytes) Assert.assertNull(org.junit.Assert.assertNull) Materialized(org.apache.kafka.streams.kstream.Materialized) TestInputTopic(org.apache.kafka.streams.TestInputTopic) StreamsTestUtils(org.apache.kafka.test.StreamsTestUtils) Assert.assertEquals(org.junit.Assert.assertEquals) Bytes(org.apache.kafka.common.utils.Bytes) KeyValue(org.apache.kafka.streams.KeyValue) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Test(org.junit.Test)

Example 17 with ValueAndTimestamp

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))));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer)

Example 18 with ValueAndTimestamp

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))));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer)

Example 19 with ValueAndTimestamp

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);
}
Also used : ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) MetricName(org.apache.kafka.common.MetricName) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) Test(org.junit.Test)

Example 20 with ValueAndTimestamp

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);
}
Also used : ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) Test(org.junit.Test)

Aggregations

ValueAndTimestamp (org.apache.kafka.streams.state.ValueAndTimestamp)20 Test (org.junit.Test)15 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)14 Windowed (org.apache.kafka.streams.kstream.Windowed)12 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)10 MockApiProcessorSupplier (org.apache.kafka.test.MockApiProcessorSupplier)9 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)8 HashMap (java.util.HashMap)6 Bytes (org.apache.kafka.common.utils.Bytes)4 WindowBytesStoreSupplier (org.apache.kafka.streams.state.WindowBytesStoreSupplier)4 List (java.util.List)3 WindowStore (org.apache.kafka.streams.state.WindowStore)3 InMemoryWindowBytesStoreSupplier (org.apache.kafka.streams.state.internals.InMemoryWindowBytesStoreSupplier)3 IntegrationTest (org.apache.kafka.test.IntegrationTest)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 KafkaMetric (org.apache.kafka.common.metrics.KafkaMetric)2 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)2 InMemoryWindowStore (org.apache.kafka.streams.state.internals.InMemoryWindowStore)2 Field (java.lang.reflect.Field)1