Search in sources :

Example 31 with LongDeserializer

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

the class AbstractJoinIntegrationTest method runTestWithDriver.

void runTestWithDriver(final List<List<TestRecord<Long, String>>> expectedResult, final String storeName) {
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(STREAMS_CONFIG), STREAMS_CONFIG)) {
        final TestInputTopic<Long, String> right = driver.createInputTopic(INPUT_TOPIC_RIGHT, new LongSerializer(), new StringSerializer());
        final TestInputTopic<Long, String> left = driver.createInputTopic(INPUT_TOPIC_LEFT, new LongSerializer(), new StringSerializer());
        final TestOutputTopic<Long, String> outputTopic = driver.createOutputTopic(OUTPUT_TOPIC, new LongDeserializer(), new StringDeserializer());
        final Map<String, TestInputTopic<Long, String>> testInputTopicMap = new HashMap<>();
        testInputTopicMap.put(INPUT_TOPIC_RIGHT, right);
        testInputTopicMap.put(INPUT_TOPIC_LEFT, left);
        TestRecord<Long, String> expectedFinalResult = null;
        final long firstTimestamp = time.milliseconds();
        long eventTimestamp = firstTimestamp;
        final Iterator<List<TestRecord<Long, String>>> resultIterator = expectedResult.iterator();
        for (final Input<String> singleInputRecord : input) {
            testInputTopicMap.get(singleInputRecord.topic).pipeInput(singleInputRecord.record.key, singleInputRecord.record.value, ++eventTimestamp);
            final List<TestRecord<Long, String>> expected = resultIterator.next();
            if (expected != null) {
                final List<TestRecord<Long, String>> updatedExpected = new LinkedList<>();
                for (final TestRecord<Long, String> record : expected) {
                    updatedExpected.add(new TestRecord<>(record.key(), record.value(), null, firstTimestamp + record.timestamp()));
                }
                final List<TestRecord<Long, String>> output = outputTopic.readRecordsToList();
                assertThat(output, equalTo(updatedExpected));
                expectedFinalResult = updatedExpected.get(expected.size() - 1);
            }
        }
        if (storeName != null) {
            checkQueryableStore(storeName, expectedFinalResult, driver);
        }
    }
}
Also used : LongSerializer(org.apache.kafka.common.serialization.LongSerializer) HashMap(java.util.HashMap) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) LinkedList(java.util.LinkedList) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) TestInputTopic(org.apache.kafka.streams.TestInputTopic) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord)

Example 32 with LongDeserializer

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

the class AbstractJoinIntegrationTest method runTestWithDriver.

void runTestWithDriver(final TestRecord<Long, String> expectedFinalResult, final String storeName) throws InterruptedException {
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(STREAMS_CONFIG), STREAMS_CONFIG)) {
        final TestInputTopic<Long, String> right = driver.createInputTopic(INPUT_TOPIC_RIGHT, new LongSerializer(), new StringSerializer());
        final TestInputTopic<Long, String> left = driver.createInputTopic(INPUT_TOPIC_LEFT, new LongSerializer(), new StringSerializer());
        final TestOutputTopic<Long, String> outputTopic = driver.createOutputTopic(OUTPUT_TOPIC, new LongDeserializer(), new StringDeserializer());
        final Map<String, TestInputTopic<Long, String>> testInputTopicMap = new HashMap<>();
        testInputTopicMap.put(INPUT_TOPIC_RIGHT, right);
        testInputTopicMap.put(INPUT_TOPIC_LEFT, left);
        final long firstTimestamp = time.milliseconds();
        long eventTimestamp = firstTimestamp;
        for (final Input<String> singleInputRecord : input) {
            testInputTopicMap.get(singleInputRecord.topic).pipeInput(singleInputRecord.record.key, singleInputRecord.record.value, ++eventTimestamp);
        }
        final TestRecord<Long, String> updatedExpectedFinalResult = new TestRecord<>(expectedFinalResult.key(), expectedFinalResult.value(), null, firstTimestamp + expectedFinalResult.timestamp());
        final List<TestRecord<Long, String>> output = outputTopic.readRecordsToList();
        assertThat(output.get(output.size() - 1), equalTo(updatedExpectedFinalResult));
        if (storeName != null) {
            checkQueryableStore(storeName, updatedExpectedFinalResult, driver);
        }
    }
}
Also used : LongSerializer(org.apache.kafka.common.serialization.LongSerializer) HashMap(java.util.HashMap) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) TestInputTopic(org.apache.kafka.streams.TestInputTopic) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord)

Example 33 with LongDeserializer

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

the class KStreamAggregationIntegrationTest method shouldGroupByKey.

@SuppressWarnings("deprecation")
@Test
public void shouldGroupByKey() throws Exception {
    final long timestamp = mockTime.milliseconds();
    produceMessages(timestamp);
    produceMessages(timestamp);
    // noinspection deprecation
    stream.groupByKey(Grouped.with(Serdes.Integer(), Serdes.String())).windowedBy(TimeWindows.of(ofMillis(500L))).count().toStream((windowedKey, value) -> windowedKey.key() + "@" + windowedKey.window().start()).to(outputTopic, Produced.with(Serdes.String(), Serdes.Long()));
    startStreams();
    final List<KeyValueTimestamp<String, Long>> results = receiveMessages(new StringDeserializer(), new LongDeserializer(), 10);
    results.sort(KStreamAggregationIntegrationTest::compare);
    final long window = timestamp / 500 * 500;
    assertThat(results, is(Arrays.asList(new KeyValueTimestamp("1@" + window, 1L, timestamp), new KeyValueTimestamp("1@" + window, 2L, timestamp), new KeyValueTimestamp("2@" + window, 1L, timestamp), new KeyValueTimestamp("2@" + window, 2L, timestamp), new KeyValueTimestamp("3@" + window, 1L, timestamp), new KeyValueTimestamp("3@" + window, 2L, timestamp), new KeyValueTimestamp("4@" + window, 1L, timestamp), new KeyValueTimestamp("4@" + window, 2L, timestamp), new KeyValueTimestamp("5@" + window, 1L, timestamp), new KeyValueTimestamp("5@" + window, 2L, timestamp))));
}
Also used : Arrays(java.util.Arrays) Produced(org.apache.kafka.streams.kstream.Produced) WindowedSerdes(org.apache.kafka.streams.kstream.WindowedSerdes) MockTime(kafka.utils.MockTime) Instant.ofEpochMilli(java.time.Instant.ofEpochMilli) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) IntegrationTestUtils.safeUniqueTestName(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.safeUniqueTestName) Serde(org.apache.kafka.common.serialization.Serde) After(org.junit.After) Duration(java.time.Duration) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Aggregator(org.apache.kafka.streams.kstream.Aggregator) TimeWindowedDeserializer(org.apache.kafka.streams.kstream.TimeWindowedDeserializer) MockMapper(org.apache.kafka.test.MockMapper) AfterClass(org.junit.AfterClass) SessionWindowedDeserializer(org.apache.kafka.streams.kstream.SessionWindowedDeserializer) TestUtils(org.apache.kafka.test.TestUtils) KeyValue(org.apache.kafka.streams.KeyValue) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Category(org.junit.experimental.categories.Category) QueryableStoreTypes(org.apache.kafka.streams.state.QueryableStoreTypes) IntegrationTestUtils(org.apache.kafka.streams.integration.utils.IntegrationTestUtils) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Materialized(org.apache.kafka.streams.kstream.Materialized) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) UnlimitedWindow(org.apache.kafka.streams.kstream.internals.UnlimitedWindow) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Duration.ofMillis(java.time.Duration.ofMillis) StreamsConfig(org.apache.kafka.streams.StreamsConfig) KGroupedStream(org.apache.kafka.streams.kstream.KGroupedStream) BeforeClass(org.junit.BeforeClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConsoleConsumer(kafka.tools.ConsoleConsumer) SessionWindows(org.apache.kafka.streams.kstream.SessionWindows) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) IntegrationTest(org.apache.kafka.test.IntegrationTest) HashMap(java.util.HashMap) KStream(org.apache.kafka.streams.kstream.KStream) HashSet(java.util.HashSet) Initializer(org.apache.kafka.streams.kstream.Initializer) EmbeddedKafkaCluster(org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster) Windowed(org.apache.kafka.streams.kstream.Windowed) TestName(org.junit.rules.TestName) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) Deserializer(org.apache.kafka.common.serialization.Deserializer) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) PrintStream(java.io.PrintStream) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) Properties(java.util.Properties) Consumed(org.apache.kafka.streams.kstream.Consumed) Transformer(org.apache.kafka.streams.kstream.Transformer) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) Grouped(org.apache.kafka.streams.kstream.Grouped) SlidingWindows(org.apache.kafka.streams.kstream.SlidingWindows) UnlimitedWindows(org.apache.kafka.streams.kstream.UnlimitedWindows) TimeUnit(java.util.concurrent.TimeUnit) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) Rule(org.junit.Rule) TimeWindows(org.apache.kafka.streams.kstream.TimeWindows) Reducer(org.apache.kafka.streams.kstream.Reducer) Duration.ofMinutes(java.time.Duration.ofMinutes) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Comparator(java.util.Comparator) ReadOnlySessionStore(org.apache.kafka.streams.state.ReadOnlySessionStore) Collections(java.util.Collections) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 34 with LongDeserializer

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

the class KStreamAggregationIntegrationTest method shouldCountHelper.

private void shouldCountHelper() throws Exception {
    startStreams();
    produceMessages(mockTime.milliseconds());
    final List<KeyValueTimestamp<String, Long>> results = receiveMessages(new StringDeserializer(), new LongDeserializer(), 10);
    results.sort(KStreamAggregationIntegrationTest::compare);
    assertThat(results, is(Arrays.asList(new KeyValueTimestamp("A", 1L, mockTime.milliseconds()), new KeyValueTimestamp("A", 2L, mockTime.milliseconds()), new KeyValueTimestamp("B", 1L, mockTime.milliseconds()), new KeyValueTimestamp("B", 2L, mockTime.milliseconds()), new KeyValueTimestamp("C", 1L, mockTime.milliseconds()), new KeyValueTimestamp("C", 2L, mockTime.milliseconds()), new KeyValueTimestamp("D", 1L, mockTime.milliseconds()), new KeyValueTimestamp("D", 2L, mockTime.milliseconds()), new KeyValueTimestamp("E", 1L, mockTime.milliseconds()), new KeyValueTimestamp("E", 2L, mockTime.milliseconds()))));
}
Also used : LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp)

Example 35 with LongDeserializer

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

the class KStreamRepartitionIntegrationTest method shouldInheritRepartitionTopicPartitionNumberFromUpstreamTopicWhenNumberOfPartitionsIsNotSpecified.

@Test
public void shouldInheritRepartitionTopicPartitionNumberFromUpstreamTopicWhenNumberOfPartitionsIsNotSpecified() throws Exception {
    final String repartitionName = "new-topic";
    final long timestamp = System.currentTimeMillis();
    sendEvents(timestamp, Arrays.asList(new KeyValue<>(1, "A"), new KeyValue<>(2, "B")));
    final StreamsBuilder builder = new StreamsBuilder();
    builder.stream(inputTopic, Consumed.with(Serdes.Integer(), Serdes.String())).repartition(Repartitioned.as(repartitionName)).groupByKey().count().toStream().to(outputTopic);
    startStreams(builder);
    validateReceivedMessages(new IntegerDeserializer(), new LongDeserializer(), Arrays.asList(new KeyValue<>(1, 1L), new KeyValue<>(2, 1L)));
    final String repartitionTopicName = toRepartitionTopicName(repartitionName);
    assertTrue(topicExists(repartitionTopicName));
    assertEquals(4, getNumberOfPartitionsForTopic(repartitionTopicName));
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) KeyValue(org.apache.kafka.streams.KeyValue) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Aggregations

LongDeserializer (org.apache.kafka.common.serialization.LongDeserializer)38 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)34 KeyValue (org.apache.kafka.streams.KeyValue)22 Test (org.junit.Test)22 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)20 List (java.util.List)19 Properties (java.util.Properties)18 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)16 HashMap (java.util.HashMap)14 Arrays (java.util.Arrays)13 Map (java.util.Map)13 Serdes (org.apache.kafka.common.serialization.Serdes)13 StreamsConfig (org.apache.kafka.streams.StreamsConfig)13 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)12 KStream (org.apache.kafka.streams.kstream.KStream)12 ArrayList (java.util.ArrayList)11 IntegerDeserializer (org.apache.kafka.common.serialization.IntegerDeserializer)11 Consumed (org.apache.kafka.streams.kstream.Consumed)10 Produced (org.apache.kafka.streams.kstream.Produced)10 Deserializer (org.apache.kafka.common.serialization.Deserializer)9