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
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.