from kafka import KafkaConsumer
from kafka import TopicPartition
TOPIC = "test_topic"
PARTITION = 0
consumer = KafkaConsumer(
group_id=TOPIC,
auto_offset_reset="earliest",
bootstrap_servers="localhost:9092",
request_timeout_ms=100000,
session_timeout_ms=99000,
max_poll_records=100,
)
topic_partition = TopicPartition(TOPIC, PARTITION)
# format: topic, partition
consumer.assign([topic_partition])
consumer.seek(topic_partition, 1660000)
# format: TopicPartition, offset. 1660000 is the offset been set.
for message in consumer:
# do something这只分配一个分区并设置该分区的偏移量,如果有多个分区,则需要为每个分区分配一个,然后设置偏移量。
aalmeida88的答案有时对我有用,在某些情况下,它确实有用,aalmeida88给了我一些想法去寻找,它似乎也是一种有用的方法。
您可能需要注意的另一件事