Search in sources :

Example 96 with IntegerSerializer

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

the class CogroupedKStreamImplTest method shouldCoGroupStreamsWithDifferentInputTypes.

@Test
public void shouldCoGroupStreamsWithDifferentInputTypes() {
    final StreamsBuilder builder = new StreamsBuilder();
    final Consumed<String, Integer> integerConsumed = Consumed.with(Serdes.String(), Serdes.Integer());
    final KStream<String, String> stream1 = builder.stream("one", stringConsumed);
    final KStream<String, Integer> stream2 = builder.stream("two", integerConsumed);
    final KGroupedStream<String, String> grouped1 = stream1.groupByKey();
    final KGroupedStream<String, Integer> grouped2 = stream2.groupByKey();
    final KTable<String, Integer> customers = grouped1.cogroup(STRING_SUM_AGGREGATOR).cogroup(grouped2, SUM_AGGREGATOR).aggregate(SUM_INITIALIZER, Materialized.<String, Integer, KeyValueStore<Bytes, byte[]>>as("store1").withValueSerde(Serdes.Integer()));
    customers.toStream().to(OUTPUT);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, String> testInputTopic = driver.createInputTopic("one", new StringSerializer(), new StringSerializer());
        final TestInputTopic<String, Integer> testInputTopic2 = driver.createInputTopic("two", new StringSerializer(), new IntegerSerializer());
        final TestOutputTopic<String, Integer> testOutputTopic = driver.createOutputTopic(OUTPUT, new StringDeserializer(), new IntegerDeserializer());
        testInputTopic.pipeInput("k1", "1", 0L);
        testInputTopic.pipeInput("k2", "1", 1L);
        testInputTopic.pipeInput("k1", "1", 10L);
        testInputTopic.pipeInput("k2", "1", 100L);
        testInputTopic2.pipeInput("k2", 2, 100L);
        testInputTopic2.pipeInput("k2", 2, 200L);
        testInputTopic2.pipeInput("k1", 2, 1L);
        testInputTopic2.pipeInput("k2", 2, 500L);
        testInputTopic2.pipeInput("k1", 2, 500L);
        testInputTopic2.pipeInput("k2", 3, 500L);
        testInputTopic2.pipeInput("k3", 2, 500L);
        testInputTopic2.pipeInput("k2", 2, 100L);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", 1, 0);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", 1, 1);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", 2, 10);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", 2, 100);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", 4, 100);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", 6, 200);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", 4, 10);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", 8, 500);
        assertOutputKeyValueTimestamp(testOutputTopic, "k1", 6, 500);
        assertOutputKeyValueTimestamp(testOutputTopic, "k2", 11, 500);
        assertOutputKeyValueTimestamp(testOutputTopic, "k3", 2, 500);
    }
}
Also used : IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Example 97 with IntegerSerializer

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

the class KStreamFilterTest method testFilter.

@Test
public void testFilter() {
    final StreamsBuilder builder = new StreamsBuilder();
    final int[] expectedKeys = new int[] { 1, 2, 3, 4, 5, 6, 7 };
    final KStream<Integer, String> stream;
    final MockApiProcessorSupplier<Integer, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
    stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
    stream.filter(isMultipleOfThree).process(supplier);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<Integer, String> inputTopic = driver.createInputTopic(topicName, new IntegerSerializer(), new StringSerializer());
        for (final int expectedKey : expectedKeys) {
            inputTopic.pipeInput(expectedKey, "V" + expectedKey);
        }
    }
    assertEquals(2, supplier.theCapturedProcessor().processed().size());
}
Also used : MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Example 98 with IntegerSerializer

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

the class GlobalStateTaskTest method shouldThrowStreamsExceptionWhenKeyDeserializationFails.

@Test
public void shouldThrowStreamsExceptionWhenKeyDeserializationFails() {
    final byte[] key = new LongSerializer().serialize(topic2, 1L);
    final byte[] recordValue = new IntegerSerializer().serialize(topic2, 10);
    maybeDeserialize(globalStateTask, key, recordValue, true);
}
Also used : LongSerializer(org.apache.kafka.common.serialization.LongSerializer) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) Test(org.junit.Test)

Example 99 with IntegerSerializer

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

the class GlobalStateTaskTest method shouldProcessRecordsForOtherTopic.

@Test
public void shouldProcessRecordsForOtherTopic() {
    final byte[] integerBytes = new IntegerSerializer().serialize("foo", 1);
    globalStateTask.initialize();
    globalStateTask.update(record(topic2, 1, 1, integerBytes, integerBytes));
    assertEquals(1, sourceTwo.numReceived);
    assertEquals(0, sourceOne.numReceived);
}
Also used : IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) Test(org.junit.Test)

Example 100 with IntegerSerializer

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

the class GlobalStateTaskTest method shouldNotThrowStreamsExceptionWhenValueDeserializationFails.

@Test
public void shouldNotThrowStreamsExceptionWhenValueDeserializationFails() {
    final GlobalStateUpdateTask globalStateTask2 = new GlobalStateUpdateTask(logContext, topology, context, stateMgr, new LogAndContinueExceptionHandler());
    final byte[] key = new IntegerSerializer().serialize(topic2, 1);
    final byte[] recordValue = new LongSerializer().serialize(topic2, 10L);
    maybeDeserialize(globalStateTask2, key, recordValue, false);
}
Also used : LongSerializer(org.apache.kafka.common.serialization.LongSerializer) LogAndContinueExceptionHandler(org.apache.kafka.streams.errors.LogAndContinueExceptionHandler) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) Test(org.junit.Test)

Aggregations

IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)106 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)75 Test (org.junit.Test)74 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)72 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)58 MockApiProcessorSupplier (org.apache.kafka.test.MockApiProcessorSupplier)46 Properties (java.util.Properties)22 IntegerDeserializer (org.apache.kafka.common.serialization.IntegerDeserializer)16 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)13 HashSet (java.util.HashSet)11 Set (java.util.Set)11 KeyValue (org.apache.kafka.streams.KeyValue)10 LongSerializer (org.apache.kafka.common.serialization.LongSerializer)9 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)9 Serdes (org.apache.kafka.common.serialization.Serdes)8 TestInputTopic (org.apache.kafka.streams.TestInputTopic)8 Consumed (org.apache.kafka.streams.kstream.Consumed)8 KStream (org.apache.kafka.streams.kstream.KStream)8 StreamsTestUtils (org.apache.kafka.test.StreamsTestUtils)8 Duration (java.time.Duration)6