Search in sources :

Example 21 with LongDeserializer

use of org.apache.kafka.common.serialization.LongDeserializer in project apache-kafka-on-k8s by banzaicloud.

the class KStreamAggregationIntegrationTest method shouldCountHelper.

private void shouldCountHelper() throws Exception {
    startStreams();
    produceMessages(mockTime.milliseconds());
    final List<KeyValue<String, Long>> results = receiveMessages(new StringDeserializer(), new LongDeserializer(), 10);
    Collections.sort(results, new Comparator<KeyValue<String, Long>>() {

        @Override
        public int compare(final KeyValue<String, Long> o1, final KeyValue<String, Long> o2) {
            return KStreamAggregationIntegrationTest.compare(o1, o2);
        }
    });
    assertThat(results, is(Arrays.asList(KeyValue.pair("A", 1L), KeyValue.pair("A", 2L), KeyValue.pair("B", 1L), KeyValue.pair("B", 2L), KeyValue.pair("C", 1L), KeyValue.pair("C", 2L), KeyValue.pair("D", 1L), KeyValue.pair("D", 2L), KeyValue.pair("E", 1L), KeyValue.pair("E", 2L))));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer)

Example 22 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 23 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 24 with LongDeserializer

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

the class KStreamRepartitionIntegrationTest method shouldGoThroughRebalancingCorrectly.

@Test
public void shouldGoThroughRebalancingCorrectly() throws Exception {
    final String repartitionName = "rebalancing-test";
    final long timestamp = System.currentTimeMillis();
    sendEvents(timestamp, Arrays.asList(new KeyValue<>(1, "A"), new KeyValue<>(2, "B")));
    final StreamsBuilder builder = new StreamsBuilder();
    final Repartitioned<String, String> repartitioned = Repartitioned.<String, String>as(repartitionName).withKeySerde(Serdes.String()).withValueSerde(Serdes.String()).withNumberOfPartitions(2);
    builder.stream(inputTopic, Consumed.with(Serdes.Integer(), Serdes.String())).selectKey((key, value) -> key.toString()).repartition(repartitioned).groupByKey().count().toStream().to(outputTopic);
    startStreams(builder);
    final Properties streamsToCloseConfigs = new Properties();
    streamsToCloseConfigs.putAll(streamsConfiguration);
    streamsToCloseConfigs.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getPath() + "-2");
    final KafkaStreams kafkaStreamsToClose = startStreams(builder, streamsToCloseConfigs);
    validateReceivedMessages(new StringDeserializer(), new LongDeserializer(), Arrays.asList(new KeyValue<>("1", 1L), new KeyValue<>("2", 1L)));
    kafkaStreamsToClose.close();
    sendEvents(timestamp, Arrays.asList(new KeyValue<>(1, "C"), new KeyValue<>(2, "D")));
    validateReceivedMessages(new StringDeserializer(), new LongDeserializer(), Arrays.asList(new KeyValue<>("1", 2L), new KeyValue<>("2", 2L)));
    final String repartitionTopicName = toRepartitionTopicName(repartitionName);
    assertTrue(topicExists(repartitionTopicName));
    assertEquals(2, getNumberOfPartitionsForTopic(repartitionTopicName));
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Arrays(java.util.Arrays) Repartitioned(org.apache.kafka.streams.kstream.Repartitioned) AdminClient(org.apache.kafka.clients.admin.AdminClient) Matcher(java.util.regex.Matcher) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) IntegrationTestUtils.safeUniqueTestName(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.safeUniqueTestName) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Duration(java.time.Duration) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Parameterized(org.junit.runners.Parameterized) AfterClass(org.junit.AfterClass) TestUtils(org.apache.kafka.test.TestUtils) Collection(java.util.Collection) KeyValue(org.apache.kafka.streams.KeyValue) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) State(org.apache.kafka.streams.KafkaStreams.State) Category(org.junit.experimental.categories.Category) Objects(java.util.Objects) IntegrationTestUtils(org.apache.kafka.streams.integration.utils.IntegrationTestUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Pattern(java.util.regex.Pattern) ERROR(org.apache.kafka.streams.KafkaStreams.State.ERROR) StreamsConfig(org.apache.kafka.streams.StreamsConfig) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) IntegrationTest(org.apache.kafka.test.IntegrationTest) KStream(org.apache.kafka.streams.kstream.KStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) JoinWindows(org.apache.kafka.streams.kstream.JoinWindows) EmbeddedKafkaCluster(org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster) TestName(org.junit.rules.TestName) Named(org.apache.kafka.streams.kstream.Named) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) Deserializer(org.apache.kafka.common.serialization.Deserializer) Before(org.junit.Before) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Properties(java.util.Properties) Consumed(org.apache.kafka.streams.kstream.Consumed) Parameter(org.junit.runners.Parameterized.Parameter) Assert.assertNotNull(org.junit.Assert.assertNotNull) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) RUNNING(org.apache.kafka.streams.KafkaStreams.State.RUNNING) Rule(org.junit.Rule) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) KafkaStreams(org.apache.kafka.streams.KafkaStreams) REBALANCING(org.apache.kafka.streams.KafkaStreams.State.REBALANCING) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) KafkaStreams(org.apache.kafka.streams.KafkaStreams) KeyValue(org.apache.kafka.streams.KeyValue) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Properties(java.util.Properties) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 25 with LongDeserializer

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

the class KStreamRepartitionIntegrationTest method shouldPerformKeySelectOperationWhenRepartitionOperationIsUsedWithKeySelector.

@Test
public void shouldPerformKeySelectOperationWhenRepartitionOperationIsUsedWithKeySelector() throws Exception {
    final String repartitionedName = "new-key";
    final long timestamp = System.currentTimeMillis();
    sendEvents(timestamp, Arrays.asList(new KeyValue<>(1, "A"), new KeyValue<>(2, "B")));
    final StreamsBuilder builder = new StreamsBuilder();
    final Repartitioned<String, String> repartitioned = Repartitioned.<String, String>as(repartitionedName).withKeySerde(Serdes.String());
    builder.stream(inputTopic, Consumed.with(Serdes.Integer(), Serdes.String())).selectKey((key, value) -> key.toString(), Named.as(repartitionedName)).repartition(repartitioned).groupByKey().count().toStream().to(outputTopic);
    startStreams(builder);
    validateReceivedMessages(new StringDeserializer(), new LongDeserializer(), Arrays.asList(new KeyValue<>("1", 1L), new KeyValue<>("2", 1L)));
    final String topology = builder.build().describe().toString();
    final String repartitionTopicName = toRepartitionTopicName(repartitionedName);
    assertTrue(topicExists(repartitionTopicName));
    assertEquals(1, countOccurrencesInTopology(topology, "Sink: .*" + repartitionedName + "-repartition.*"));
    assertEquals(1, countOccurrencesInTopology(topology, "<-- " + repartitionedName + "\n"));
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Arrays(java.util.Arrays) Repartitioned(org.apache.kafka.streams.kstream.Repartitioned) AdminClient(org.apache.kafka.clients.admin.AdminClient) Matcher(java.util.regex.Matcher) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) IntegrationTestUtils.safeUniqueTestName(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.safeUniqueTestName) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Duration(java.time.Duration) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Parameterized(org.junit.runners.Parameterized) AfterClass(org.junit.AfterClass) TestUtils(org.apache.kafka.test.TestUtils) Collection(java.util.Collection) KeyValue(org.apache.kafka.streams.KeyValue) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) State(org.apache.kafka.streams.KafkaStreams.State) Category(org.junit.experimental.categories.Category) Objects(java.util.Objects) IntegrationTestUtils(org.apache.kafka.streams.integration.utils.IntegrationTestUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Pattern(java.util.regex.Pattern) ERROR(org.apache.kafka.streams.KafkaStreams.State.ERROR) StreamsConfig(org.apache.kafka.streams.StreamsConfig) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) IntegrationTest(org.apache.kafka.test.IntegrationTest) KStream(org.apache.kafka.streams.kstream.KStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) JoinWindows(org.apache.kafka.streams.kstream.JoinWindows) EmbeddedKafkaCluster(org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster) TestName(org.junit.rules.TestName) Named(org.apache.kafka.streams.kstream.Named) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) Deserializer(org.apache.kafka.common.serialization.Deserializer) Before(org.junit.Before) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Properties(java.util.Properties) Consumed(org.apache.kafka.streams.kstream.Consumed) Parameter(org.junit.runners.Parameterized.Parameter) Assert.assertNotNull(org.junit.Assert.assertNotNull) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) RUNNING(org.apache.kafka.streams.KafkaStreams.State.RUNNING) Rule(org.junit.Rule) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) KafkaStreams(org.apache.kafka.streams.KafkaStreams) REBALANCING(org.apache.kafka.streams.KafkaStreams.State.REBALANCING) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) KeyValue(org.apache.kafka.streams.KeyValue) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Aggregations

LongDeserializer (org.apache.kafka.common.serialization.LongDeserializer)37 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)33 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