Search in sources :

Example 36 with IntegerSerializer

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

the class KTableSourceTest method testKTable.

@Test
public void testKTable() {
    final StreamsBuilder builder = new StreamsBuilder();
    final String topic1 = "topic1";
    final KTable<String, Integer> table1 = builder.table(topic1, Consumed.with(Serdes.String(), Serdes.Integer()));
    final MockApiProcessorSupplier<String, Integer, Void, Void> supplier = new MockApiProcessorSupplier<>();
    table1.toStream().process(supplier);
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, Integer> inputTopic = driver.createInputTopic(topic1, new StringSerializer(), new IntegerSerializer());
        inputTopic.pipeInput("A", 1, 10L);
        inputTopic.pipeInput("B", 2, 11L);
        inputTopic.pipeInput("C", 3, 12L);
        inputTopic.pipeInput("D", 4, 13L);
        inputTopic.pipeInput("A", null, 14L);
        inputTopic.pipeInput("B", null, 15L);
    }
    assertEquals(asList(new KeyValueTimestamp<>("A", 1, 10L), new KeyValueTimestamp<>("B", 2, 11L), new KeyValueTimestamp<>("C", 3, 12L), new KeyValueTimestamp<>("D", 4, 13L), new KeyValueTimestamp<>("A", null, 14L), new KeyValueTimestamp<>("B", null, 15L)), supplier.theCapturedProcessor().processed());
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockApiProcessorSupplier(org.apache.kafka.test.MockApiProcessorSupplier) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) KeyValueTimestamp(org.apache.kafka.streams.KeyValueTimestamp) Test(org.junit.Test)

Example 37 with IntegerSerializer

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

the class KTableSourceTest method testKTableSourceEmitOnChange.

// we have disabled KIP-557 until KAFKA-12508 can be properly addressed
@Ignore
@Test
public void testKTableSourceEmitOnChange() {
    final StreamsBuilder builder = new StreamsBuilder();
    final String topic1 = "topic1";
    builder.table(topic1, Consumed.with(Serdes.String(), Serdes.Integer()), Materialized.as("store")).toStream().to("output");
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<String, Integer> inputTopic = driver.createInputTopic(topic1, new StringSerializer(), new IntegerSerializer());
        final TestOutputTopic<String, Integer> outputTopic = driver.createOutputTopic("output", new StringDeserializer(), new IntegerDeserializer());
        inputTopic.pipeInput("A", 1, 10L);
        inputTopic.pipeInput("B", 2, 11L);
        inputTopic.pipeInput("A", 1, 12L);
        inputTopic.pipeInput("B", 3, 13L);
        // this record should be kept since this is out of order, so the timestamp
        // should be updated in this scenario
        inputTopic.pipeInput("A", 1, 9L);
        assertEquals(1.0, getMetricByName(driver.metrics(), "idempotent-update-skip-total", "stream-processor-node-metrics").metricValue());
        assertEquals(asList(new TestRecord<>("A", 1, Instant.ofEpochMilli(10L)), new TestRecord<>("B", 2, Instant.ofEpochMilli(11L)), new TestRecord<>("B", 3, Instant.ofEpochMilli(13L)), new TestRecord<>("A", 1, Instant.ofEpochMilli(9L))), outputTopic.readRecordsToList());
    }
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) IntegerDeserializer(org.apache.kafka.common.serialization.IntegerDeserializer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 38 with IntegerSerializer

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

the class GlobalStateTaskTest method shouldProcessRecordsForOtherTopic.

@Test
public void shouldProcessRecordsForOtherTopic() {
    final byte[] integerBytes = new IntegerSerializer().serialize("foo", 1);
    globalStateTask.initialize();
    globalStateTask.update(new ConsumerRecord<>(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 39 with IntegerSerializer

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

the class GlobalStateTaskTest method shouldThrowStreamsExceptionWhenKeyDeserializationFails.

@Test
public void shouldThrowStreamsExceptionWhenKeyDeserializationFails() throws Exception {
    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 40 with IntegerSerializer

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

the class GlobalStateTaskTest method shouldNotThrowStreamsExceptionWhenKeyDeserializationFailsWithSkipHandler.

@Test
public void shouldNotThrowStreamsExceptionWhenKeyDeserializationFailsWithSkipHandler() throws Exception {
    final GlobalStateUpdateTask globalStateTask2 = new GlobalStateUpdateTask(topology, context, stateMgr, new LogAndContinueExceptionHandler(), logContext);
    final byte[] key = new LongSerializer().serialize(topic2, 1L);
    final byte[] recordValue = new IntegerSerializer().serialize(topic2, 10);
    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)86 Test (org.junit.Test)65 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)64 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)61 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)53 MockApiProcessorSupplier (org.apache.kafka.test.MockApiProcessorSupplier)42 Properties (java.util.Properties)17 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)13 HashSet (java.util.HashSet)11 Set (java.util.Set)11 LongSerializer (org.apache.kafka.common.serialization.LongSerializer)8 Serdes (org.apache.kafka.common.serialization.Serdes)8 KeyValue (org.apache.kafka.streams.KeyValue)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)5 Instant (java.time.Instant)5 ArrayList (java.util.ArrayList)5