3

Im trying to consume data from a topic by setting the offset but get assertion error -

from kafka import KafkaConsumer

consumer = KafkaConsumer('foobar1',
                         bootstrap_servers=['localhost:9092'])
print 'process started'
print consumer.partitions_for_topic('foobar1')
print 'done'
consumer.seek(0,10)

for message in consumer:
    print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,
                                          message.offset, message.key,
                                          message.value))
print 'process ended'

Error:-

Traceback (most recent call last):
  File "/Users/pn/Documents/jobs/ccdn/kafka_consumer_1.py", line 21, in <module>
    consumer.seek(0,10)
  File "/Users/pn/.virtualenvs/vpsq/lib/python2.7/site-packages/kafka/consumer/group.py", line 549, in seek
    assert partition in self._subscription.assigned_partitions(), 'Unassigned partition'
AssertionError: Unassigned partition
user1050619
  • 19,822
  • 85
  • 237
  • 413

3 Answers3

3

Here is an example to solve the problem:

from kafka import KafkaConsumer, TopicPartition

con = KafkaConsumer(bootstrap_servers = my_bootstrapservers)
tp = TopicPartition(my_topic, 0)
con.assign([tp])
con.seek_to_beginning()
con.seek(tp, 1000000)

Reference: kafka consumer seek is not working: AssertionError: Unassigned partition

DennisLi
  • 3,915
  • 6
  • 30
  • 66
1

You have to call consumer.assign() with a list of TopicPartitions before calling seek. Also note that first argument for seek is also a TopicPartition. See KafkaConsumer API

Luciano Afranllie
  • 4,053
  • 25
  • 23
0

In my case with Kafka 0.9 and kafka-python, partition assignment is happened during for message in consumer. So, the seek opration should after the iteration. I reset my group's offset by the following code:

import kafka

ps = []
for i in xrange(topic_partition_number):
    ps.append(kafka.TopicPartition(topic, i))

consumer = kafka.KafkaConsumer(topic, bootstrap_servers=address, group_id=group)
for msg in consumer:
    print msg
    consumer.seek_to_beginning(*ps)
    consumer.commit()
    break
secfree
  • 4,357
  • 2
  • 28
  • 36