pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] ivankelly commented on a change in pull request #3462: Added suppport for Kafka partitioner and explicit setting of partition on record
Date Thu, 31 Jan 2019 13:36:08 GMT
ivankelly commented on a change in pull request #3462: Added suppport for Kafka partitioner
and explicit setting of partition on record
URL: https://github.com/apache/pulsar/pull/3462#discussion_r252667984
 
 

 ##########
 File path: tests/pulsar-kafka-compat-client-test/src/test/java/org/apache/pulsar/tests/integration/compat/kafka/KafkaApiTest.java
 ##########
 @@ -300,6 +302,133 @@ public void testPartitions() throws Exception {
         consumers.forEach(Consumer::close);
     }
 
+    @Test
+    public void testExplicitPartitions() throws Exception {
+        String topic = "testExplicitPartitions";
+
+        // Create 8 partitions in topic
+        @Cleanup
+        PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(getHttpServiceUrl()).build();
+        admin.topics().createPartitionedTopic(topic, 8);
+
+        Properties producerProperties = new Properties();
+        producerProperties.put("bootstrap.servers", getPlainTextServiceUrl());
+        producerProperties.put("key.serializer", IntegerSerializer.class.getName());
+        producerProperties.put("value.serializer", StringSerializer.class.getName());
+
+        @Cleanup
+        Producer<Integer, String> producer = new KafkaProducer<>(producerProperties);
+
+        Properties props = new Properties();
+        props.put("bootstrap.servers", getPlainTextServiceUrl());
+        props.put("group.id", "my-subscription-name");
+        props.put("enable.auto.commit", "true");
+        props.put("key.deserializer", StringDeserializer.class.getName());
+        props.put("value.deserializer", StringDeserializer.class.getName());
+
+        // Create Kakfa consumer and verify all messages came from intended partition
+        @Cleanup
+        Consumer<String, String> consumer = new KafkaConsumer<>(props);
+        consumer.subscribe(Arrays.asList(topic));
+
+        int N = 8 * 3;
+
+        final int choosenPartition = 5;
+
+        for (int i = 0; i < N; i++) {
+            producer.send(new ProducerRecord<>(topic, choosenPartition, i, "hello-"
+ i));
+        }
+
+        producer.flush();
+
+        for (int i = 0; i < N;) {
+            ConsumerRecords<String, String> records = consumer.poll(100);
+            i += records.count();
+
+            records.forEach(record -> {
+                assertEquals(record.partition(), choosenPartition);
+            });
+        }
+
+        // No more messages for this consumer
+        ConsumerRecords<String, String> records = consumer.poll(100);
+        assertEquals(records.count(), 0);
+    }
+
+    private static class MyCustomPartitioner implements Partitioner {
+
+        static int USED_PARTITION = 3;
+
+        @Override
+        public void configure(Map<String, ?> arg0) {
+            // Do nothing
+        }
+
+        @Override
+        public void close() {
+            // Do nothing
+        }
+
+        @Override
+        public int partition(String arg0, Object arg1, byte[] arg2, Object arg3, byte[] arg4,
Cluster arg5) {
+            // Dummy implementation that always return same partition
+            return USED_PARTITION;
+        }
+
+    }
+
+    @Test
+    public void testCustomRouter() throws Exception {
+        String topic = "testExplicitPartitions";
+
+        // Create 8 partitions in topic
+        @Cleanup
+        PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(getHttpServiceUrl()).build();
+        admin.topics().createPartitionedTopic(topic, 8);
+
+        Properties producerProperties = new Properties();
+        producerProperties.put("bootstrap.servers", getPlainTextServiceUrl());
+        producerProperties.put("key.serializer", IntegerSerializer.class.getName());
+        producerProperties.put("value.serializer", StringSerializer.class.getName());
+        producerProperties.put("partitioner.class", MyCustomPartitioner.class.getName());
+
+        @Cleanup
+        Producer<Integer, String> producer = new KafkaProducer<>(producerProperties);
+
+        Properties props = new Properties();
+        props.put("bootstrap.servers", getPlainTextServiceUrl());
+        props.put("group.id", "my-subscription-name");
+        props.put("enable.auto.commit", "true");
+        props.put("key.deserializer", StringDeserializer.class.getName());
 
 Review comment:
   Shouldn't it be IntegerDeserializer?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message