Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 卡夫卡消费程序中的控制消息偏移量_Python_Apache Kafka_Kafka Consumer Api - Fatal编程技术网

Python 卡夫卡消费程序中的控制消息偏移量

Python 卡夫卡消费程序中的控制消息偏移量,python,apache-kafka,kafka-consumer-api,Python,Apache Kafka,Kafka Consumer Api,实现是用Python实现的。使用合流卡夫卡 我有一个消费者对象来轮询来自卡夫卡主题的消息。这些消息被其他大对象用于进一步处理,由于大小原因,我无法在每次消息处理后备份该对象 我定期转储对象,然后手动提交使用者。下面是我实现的示例代码 from confluent_kafka import Consumer, KafkaError, TopicPartition c = Consumer({ 'bootstrap.servers': 'myserver', 'group.id':

实现是用Python实现的。使用合流卡夫卡

我有一个消费者对象来轮询来自卡夫卡主题的消息。这些消息被其他大对象用于进一步处理,由于大小原因,我无法在每次消息处理后备份该对象

我定期转储对象,然后手动提交使用者。下面是我实现的示例代码

from confluent_kafka import Consumer, KafkaError, TopicPartition

c = Consumer({
    'bootstrap.servers': 'myserver',
    'group.id': 'mygroup',
    'default.topic.config': {'auto.offset.reset': 'smallest'},
    'enable.auto.commit': "false"
})
c.subscribe(['mytopic'])

offsets = {} 

for i in range(10):
    msg = c.poll()

    if msg.error():
        continue

    par = msg.partition()
    off = msg.offset() 
    offsets[p] = off 

c.commit(async=False) 

print(offsets) 
当我第二次运行这段代码时,我希望消息偏移量(如果来自同一分区)应该是下一个偏移量,即从打印的上一个偏移量+1

但补偿额提高了很多。还有几百个

我还尝试手动分配职位,如下所示:

lst_part = []

for par, off in offsets.items():
    lst_part.append(TopicPartition('mytopic', par, off))

c.assign(lst_part)

# then start polling messages
新轮询的消息不是指定的偏移量+1

c.commit(async=False)
将通过
poll()
调用从客户端向应用程序返回消息的所有已使用分区提交

如果您想要更细粒度地控制要提交的偏移量,您可以将显式的
[TopicPartition(..)]
列表传递给
commit()
(确保提交最后一条消息\u offset+1)或禁用
自动.offset.store
并显式调用
存储\u offset()
获取您希望为将来的
commit()
调用存储的消息/偏移量

请注意,它仅在master上可用,在confluent kafka python客户端的发布版本中尚不可用,但很快就会出现