Kafka Consumer does not receive messages

后端 未结 1 1402
野的像风
野的像风 2021-01-07 05:41

I am a newbie in Kafka. I read many instructions on the Internet to make a Kafka Producer and Kafka Consumer. I did the former successfully which can send messages to Kafka

相关标签:
1条回答
  • 2021-01-07 06:31

    I've encountered the same problem as you. After a long time try, here is the answer.

    There are two types of kafka new consumer api that you can choose one.

    cousumer.assign(...)

    consumer.subscribe(..)

    And use like:

        // set these properites or you should run consumer first than run producer
        props.put("enable.auto.commit", "false");
        props.put("auto.offset.reset", "earliest");
    
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
    
        boolean assign = false;
        if(assign) {
            TopicPartition tp = new TopicPartition(topic, 0);
            List<TopicPartition> tps = Arrays.asList(tp);
            consumer.assign(tps);
            consumer.seekToBeginning(tps);
        }else {
            consumer.subscribe(Arrays.asList(topic));
        }
    

    http://kafka.apache.org/documentation.html#newconsumerconfigs

    If you use old consumer api, it's almost the same about properties config. Remember to add the two following code if you want to see messages produced before consumer consumes:

    props.put("enable.auto.commit", "false");
    props.put("auto.offset.reset", "earliest");
    

    Hope this will help other people.

    0 讨论(0)
提交回复
热议问题