user18480960
user18480960

Reputation:

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")

Upvotes: 0

Views: 487

Answers (1)

user18480960
user18480960

Reputation:

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)

Upvotes: 1

Related Questions