Search in sources :

Example 66 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class TimeWindowedCogroupedKStreamImplTest method timeWindowMixAggregatorsTest.

@Test
public void timeWindowMixAggregatorsTest() {
    final KTable<Windowed<String>, String> customers = windowedCogroupedStream.aggregate(MockInitializer.STRING_INIT, Materialized.with(Serdes.String(), Serdes.String()));
    customers.toStream().to(OUTPUT);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, String> testInputTopic = driver.createInputTopic(TOPIC, new StringSerializer(), new StringSerializer());
        final TestInputTopic<String, String> testInputTopic2 = driver.createInputTopic(TOPIC2, new StringSerializer(), new StringSerializer());
        final TestOutputTopic<Windowed<String>, String> testOutputTopic = driver.createOutputTopic(OUTPUT, new TimeWindowedDeserializer<>(new StringDeserializer(), WINDOW_SIZE), new StringDeserializer());
        testInputTopic.pipeInput("k1", "A", 0);
        testInputTopic.pipeInput("k2", "A", 0);
        testInputTopic.pipeInput("k2", "A", 1);
        testInputTopic.pipeInput("k1", "A", 2);
        testInputTopic2.pipeInput("k1", "B", 3);
        testInputTopic2.pipeInput("k2", "B", 3);
        testInputTopic2.pipeInput("k2", "B", 4);
        testInputTopic2.pipeInput("k1", "B", 4);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A", 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A", 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A+A", 1);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A+A", 2);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A+A-B", 3);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A+A-B", 3);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", "0+A+A-B-B", 4);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", "0+A+A-B-B", 4);
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Example 67 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class CachingPersistentSessionStoreTest method shouldForwardChangedValuesDuringFlush.

@Test
public void shouldForwardChangedValuesDuringFlush() {
    final Windowed<Bytes> a = new Windowed<>(keyA, new SessionWindow(2, 4));
    final Windowed<Bytes> b = new Windowed<>(keyA, new SessionWindow(1, 2));
    final Windowed<String> aDeserialized = new Windowed<>("a", new SessionWindow(2, 4));
    final Windowed<String> bDeserialized = new Windowed<>("a", new SessionWindow(1, 2));
    final CacheFlushListenerStub<Windowed<String>, String> flushListener = new CacheFlushListenerStub<>(new SessionWindowedDeserializer<>(new StringDeserializer()), new StringDeserializer());
    cachingStore.setFlushListener(flushListener, true);
    cachingStore.put(b, "1".getBytes());
    cachingStore.flush();
    assertEquals(Collections.singletonList(new KeyValueTimestamp<>(bDeserialized, new Change<>("1", null), DEFAULT_TIMESTAMP)), flushListener.forwarded);
    flushListener.forwarded.clear();
    cachingStore.put(a, "1".getBytes());
    cachingStore.flush();
    assertEquals(Collections.singletonList(new KeyValueTimestamp<>(aDeserialized, new Change<>("1", null), DEFAULT_TIMESTAMP)), flushListener.forwarded);
    flushListener.forwarded.clear();
    cachingStore.put(a, "2".getBytes());
    cachingStore.flush();
    assertEquals(Collections.singletonList(new KeyValueTimestamp<>(aDeserialized, new Change<>("2", "1"), DEFAULT_TIMESTAMP)), flushListener.forwarded);
    flushListener.forwarded.clear();
    cachingStore.remove(a);
    cachingStore.flush();
    assertEquals(Collections.singletonList(new KeyValueTimestamp<>(aDeserialized, new Change<>(null, "2"), DEFAULT_TIMESTAMP)), flushListener.forwarded);
    flushListener.forwarded.clear();
    cachingStore.put(a, "1".getBytes());
    cachingStore.put(a, "2".getBytes());
    cachingStore.remove(a);
    cachingStore.flush();
    assertEquals(Collections.emptyList(), flushListener.forwarded);
    flushListener.forwarded.clear();
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) Test(org.junit.Test)

Example 68 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class CachingPersistentSessionStoreTest method shouldNotForwardChangedValuesDuringFlushWhenSendOldValuesDisabled.

@Test
public void shouldNotForwardChangedValuesDuringFlushWhenSendOldValuesDisabled() {
    final Windowed<Bytes> a = new Windowed<>(keyA, new SessionWindow(0, 0));
    final Windowed<String> aDeserialized = new Windowed<>("a", new SessionWindow(0, 0));
    final CacheFlushListenerStub<Windowed<String>, String> flushListener = new CacheFlushListenerStub<>(new SessionWindowedDeserializer<>(new StringDeserializer()), new StringDeserializer());
    cachingStore.setFlushListener(flushListener, false);
    cachingStore.put(a, "1".getBytes());
    cachingStore.flush();
    cachingStore.put(a, "2".getBytes());
    cachingStore.flush();
    cachingStore.remove(a);
    cachingStore.flush();
    assertEquals(asList(new KeyValueTimestamp<>(aDeserialized, new Change<>("1", null), DEFAULT_TIMESTAMP), new KeyValueTimestamp<>(aDeserialized, new Change<>("2", null), DEFAULT_TIMESTAMP), new KeyValueTimestamp<>(aDeserialized, new Change<>(null, null), DEFAULT_TIMESTAMP)), flushListener.forwarded);
    flushListener.forwarded.clear();
    cachingStore.put(a, "1".getBytes());
    cachingStore.put(a, "2".getBytes());
    cachingStore.remove(a);
    cachingStore.flush();
    assertEquals(Collections.emptyList(), flushListener.forwarded);
    flushListener.forwarded.clear();
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) Bytes(org.apache.kafka.common.utils.Bytes) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) Test(org.junit.Test)

Example 69 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class CachingPersistentWindowStoreTest method setUp.

@Before
public void setUp() {
    keySchema = new WindowKeySchema();
    bytesStore = new RocksDBSegmentedBytesStore("test", "metrics-scope", 0, SEGMENT_INTERVAL, keySchema);
    underlyingStore = new RocksDBWindowStore(bytesStore, false, WINDOW_SIZE);
    final TimeWindowedDeserializer<String> keyDeserializer = new TimeWindowedDeserializer<>(new StringDeserializer(), WINDOW_SIZE);
    keyDeserializer.setIsChangelogTopic(true);
    cacheListener = new CacheFlushListenerStub<>(keyDeserializer, new StringDeserializer());
    cachingStore = new CachingWindowStore(underlyingStore, WINDOW_SIZE, SEGMENT_INTERVAL);
    cachingStore.setFlushListener(cacheListener, false);
    cache = new ThreadCache(new LogContext("testCache "), MAX_CACHE_SIZE_BYTES, new MockStreamsMetrics(new Metrics()));
    context = new InternalMockProcessorContext<>(TestUtils.tempDirectory(), null, null, null, cache);
    context.setRecordContext(new ProcessorRecordContext(DEFAULT_TIMESTAMP, 0, 0, TOPIC, new RecordHeaders()));
    cachingStore.init((StateStoreContext) context, cachingStore);
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) LogContext(org.apache.kafka.common.utils.LogContext) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) TimeWindowedDeserializer(org.apache.kafka.streams.kstream.TimeWindowedDeserializer) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ProcessorRecordContext(org.apache.kafka.streams.processor.internals.ProcessorRecordContext) Before(org.junit.Before)

Example 70 with StringDeserializer

use of org.apache.kafka.common.serialization.StringDeserializer in project kafka by apache.

the class TopologyTestDriverTest method shouldThrowNoSuchElementExceptionForUnusedOutputTopicWithDynamicRouting.

@Test
public void shouldThrowNoSuchElementExceptionForUnusedOutputTopicWithDynamicRouting() {
    testDriver = new TopologyTestDriver(setupSourceSinkTopology(), config);
    final TestOutputTopic<String, String> outputTopic = new TestOutputTopic<>(testDriver, "unused-topic", new StringDeserializer(), new StringDeserializer());
    assertTrue(outputTopic.isEmpty());
    assertThrows(NoSuchElementException.class, outputTopic::readRecord);
}
Also used : StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Test(org.junit.jupiter.api.Test)

Aggregations

StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)152 Test (org.junit.Test)91 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)59 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)46 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)35 HashMap (java.util.HashMap)33 Properties (java.util.Properties)32 IntegerDeserializer (org.apache.kafka.common.serialization.IntegerDeserializer)31 Windowed (org.apache.kafka.streams.kstream.Windowed)31 List (java.util.List)29 KeyValue (org.apache.kafka.streams.KeyValue)29 IntegrationTest (org.apache.kafka.test.IntegrationTest)27 ArrayList (java.util.ArrayList)26 LongDeserializer (org.apache.kafka.common.serialization.LongDeserializer)25 Map (java.util.Map)20 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)20 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)17 Serdes (org.apache.kafka.common.serialization.Serdes)17 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)17 KStream (org.apache.kafka.streams.kstream.KStream)17