Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieve messages in a topic using kafka-python

I have written a python script using kafka-python library which writes and reads messages into kafka. I write messages without any problem; I can retrieve them using kafka console tools. But I can't read them using my python script. I have a for on my consumer which freezes on the first line of the iteration and never returns. Here's my code:

from kafka import KafkaConsumer

consumer = KafkaConsumer(
    "my-topic",
    bootstrap_servers="localhost:9092"),
    value_deserializer=lambda v: json.dumps(v).encode("utf-8")
)

for msg in consumer:
    print(type(msg))

The consumer is created and subscribed completely; I can see that my-topic is listed on the topic list of its _client property.

Any idea?

like image 668
Zeinab Abbasimazar Avatar asked Aug 31 '26 15:08

Zeinab Abbasimazar


1 Answers

By default, kafka python start from last offset, ie will only read new mesages. One approach is to read from beginning or the alternative approach is to keep polling topic in an infinite loop as shown in below code:

while True:
    try:
        records = consumer.poll(60 * 1000) # timeout in millis , here set to 1 min

        record_list = []
        for tp, consumer_records in records.items():
            for consumer_record in consumer_records:
                record_list.append(consumer_record.value)
        print(record_list) # record_list will be list of dictionaries

Edit

To read from the beginning, we need to add auto_offset_reset=earliest earlies while making consumer object

consumer = KafkaConsumer(
    "my-topic",
    bootstrap_servers="localhost:9092"),
    value_deserializer=lambda v: json.dumps(v).encode("utf-8"),
    auto_offset_reset='earliest')

Let me know if this helps!!

like image 74
Anand Sai Avatar answered Sep 02 '26 04:09

Anand Sai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!