'Set consumer offset

If I want get all messages from to start offset, I run this shell command:

/usr/share/kafka/bin/kafka-console-consumer.sh 
--bootstrap-server localhost:9092 
--topic 1-codicefiscale-21032022122736-i 
--partition 0 
--offset 5

How can I do the same with kafka-python library?

def demoListMessages(topicName):
    consumer = KafkaConsumer(topicName, 
                             bootstrap_servers="localhost:9092", 
                             auto_offset_reset='earliest',
                             consumer_timeout_ms=1000)

    for msg in consumer:
        print(msg.value)

demoListMessages("1-codicefiscale-21032022122736-i")


Solution 1:[1]

I had to set up partition. This snippet work for me.

def demoListPageMessages(topicName):
    consumer = KafkaConsumer(bootstrap_servers="localhost:9092",auto_offset_reset='earliest',consumer_timeout_ms=1000)
    tp = TopicPartition(topicName, 0)
    consumer.assign([tp])
    consumer.seek_to_beginning()
    consumer.seek(tp, 5)

    for msg in consumer:
        print(msg.value)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1