Search in sources :

Example 1 with TimeWindowedSerializer

use of org.apache.kafka.streams.kstream.TimeWindowedSerializer in project kafka by apache.

the class WindowedStreamPartitionerTest method testCopartitioning.

@Test
public void testCopartitioning() {
    final Random rand = new Random();
    final DefaultPartitioner defaultPartitioner = new DefaultPartitioner();
    final WindowedSerializer<Integer> timeWindowedSerializer = new TimeWindowedSerializer<>(intSerializer);
    final WindowedStreamPartitioner<Integer, String> streamPartitioner = new WindowedStreamPartitioner<>(timeWindowedSerializer);
    for (int k = 0; k < 10; k++) {
        final Integer key = rand.nextInt();
        final byte[] keyBytes = intSerializer.serialize(topicName, key);
        final String value = key.toString();
        final byte[] valueBytes = stringSerializer.serialize(topicName, value);
        final Integer expected = defaultPartitioner.partition("topic", key, keyBytes, value, valueBytes, cluster);
        for (int w = 1; w < 10; w++) {
            final TimeWindow window = new TimeWindow(10 * w, 20 * w);
            final Windowed<Integer> windowedKey = new Windowed<>(key, window);
            final Integer actual = streamPartitioner.partition(topicName, windowedKey, value, infos.size());
            assertEquals(expected, actual);
        }
    }
    defaultPartitioner.close();
}
Also used : TimeWindowedSerializer(org.apache.kafka.streams.kstream.TimeWindowedSerializer) DefaultPartitioner(org.apache.kafka.clients.producer.internals.DefaultPartitioner) Windowed(org.apache.kafka.streams.kstream.Windowed) Random(java.util.Random) Test(org.junit.Test)

Example 2 with TimeWindowedSerializer

use of org.apache.kafka.streams.kstream.TimeWindowedSerializer in project apache-kafka-on-k8s by banzaicloud.

the class WindowedStreamPartitionerTest method testCopartitioning.

@Test
public void testCopartitioning() {
    final Random rand = new Random();
    final DefaultPartitioner defaultPartitioner = new DefaultPartitioner();
    final WindowedSerializer<Integer> timeWindowedSerializer = new TimeWindowedSerializer<>(intSerializer);
    final WindowedStreamPartitioner<Integer, String> streamPartitioner = new WindowedStreamPartitioner<>(topicName, timeWindowedSerializer);
    for (int k = 0; k < 10; k++) {
        Integer key = rand.nextInt();
        byte[] keyBytes = intSerializer.serialize(topicName, key);
        String value = key.toString();
        byte[] valueBytes = stringSerializer.serialize(topicName, value);
        Integer expected = defaultPartitioner.partition("topic", key, keyBytes, value, valueBytes, cluster);
        for (int w = 1; w < 10; w++) {
            TimeWindow window = new TimeWindow(10 * w, 20 * w);
            Windowed<Integer> windowedKey = new Windowed<>(key, window);
            Integer actual = streamPartitioner.partition(windowedKey, value, infos.size());
            assertEquals(expected, actual);
        }
    }
    defaultPartitioner.close();
}
Also used : TimeWindowedSerializer(org.apache.kafka.streams.kstream.TimeWindowedSerializer) DefaultPartitioner(org.apache.kafka.clients.producer.internals.DefaultPartitioner) Windowed(org.apache.kafka.streams.kstream.Windowed) Random(java.util.Random) Test(org.junit.Test)

Aggregations

Random (java.util.Random)2 DefaultPartitioner (org.apache.kafka.clients.producer.internals.DefaultPartitioner)2 TimeWindowedSerializer (org.apache.kafka.streams.kstream.TimeWindowedSerializer)2 Windowed (org.apache.kafka.streams.kstream.Windowed)2 Test (org.junit.Test)2