kafka consumer to dynamically detect topics added

后端 未结 3 1421
庸人自扰
庸人自扰 2021-01-06 03:04

I\'m using KafkaConsumer to consume messages from Kafka server (topics)..

  • It works fine for topics created before starting Consumer code...

But

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 03:31

    Here is the solution it worked for me by using KafkaConsumer api. Here is the Java code for it.

    private static Consumer createConsumer(String topic) {
        final Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
                BOOTSTRAP_SERVERS);
        props.put(ConsumerConfig.GROUP_ID_CONFIG,
                "KafkaExampleConsumer");
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
                StringDeserializer.class.getName());
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
                StringDeserializer.class.getName());
        // Create the consumer using props.
        final Consumer consumer =
                new KafkaConsumer<>(props);
        // Subscribe to the topic.
        consumer.subscribe(Collections.singletonList(topic));
        return consumer;
    }
    
    public static void runConsumer(String topic) throws InterruptedException {
        final Consumer consumer = createConsumer(topic);
    
        ConsumerRecords records = consumer.poll(100);
        for (ConsumerRecord record : records)
            System.out.printf("hiiiii offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
        consumer.commitAsync();
        consumer.close();
        //System.out.println("DONE");
    }
    

    using this we can consume the message from dynamically created topics.

提交回复
热议问题