Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.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_Confluent Platform - Fatal编程技术网

Python 如何为我的使用者获取所有分区的当前偏移量?

Python 如何为我的使用者获取所有分区的当前偏移量?,python,apache-kafka,kafka-consumer-api,confluent-platform,Python,Apache Kafka,Kafka Consumer Api,Confluent Platform,我试图获得每个可用分区的当前偏移量。根据文档,我应该这样做,所以我试着这样做: 消费者=消费者({ 'bootstrap.servers':config.bootstrap\u服务器, “group.id”:config.CONSUMER\u组, “enable.auto.commit”:False, }) #获取所有主题 topics=consumer.list_topics() #获取所有分区 分区=[] topics.topics.items()中的meta作为名称: 对于meta.par

我试图获得每个可用分区的当前偏移量。根据文档,我应该这样做,所以我试着这样做:

消费者=消费者({
'bootstrap.servers':config.bootstrap\u服务器,
“group.id”:config.CONSUMER\u组,
“enable.auto.commit”:False,
})
#获取所有主题
topics=consumer.list_topics()
#获取所有分区
分区=[]
topics.topics.items()中的meta作为名称:
对于meta.partitions.keys()中的分区\ id:
part=TopicPartition(名称、分区\u id)
partitions.append(部分)
#获取所有偏移量
x=使用者位置(分区)
但是,
x
中生成的分区中的所有偏移量仍然是
-1001


如果我使用镜头或其他工具进行检查,我可以看到此结果不正确,我正在查找的消费者组已消费消息并将其提交给Kafka。

尝试添加
消费者.subscribe()
消费者.assign()
函数

consumer = Consumer({
    'bootstrap.servers': config.BOOTSTRAP_SERVERS,
    'group.id': config.CONSUMER_GROUP,
    'enable.auto.commit': False,
})

# get all partitions
partitions = []
for name, meta  in topics.topics.items():
    for partition_id in meta.partitions.keys():
        part = TopicPartition(name, partition_id)
        partitions.append(part)

consumer.assign(partitions)
committed = consumer.committed(tp)

last_offset = consumer.position(tp)
print("topic: %s partition: %s committed: %s last: %s lag: %s" % (TOPIC, p, committed, last_offset))

作为参考,这是行之有效的解决方案:

消费者=消费者({
'bootstrap.servers':config.bootstrap\u服务器,
“group.id”:config.CONSUMER\u组,
“enable.auto.commit”:False,
})
#获取所有主题
topics=consumer.list_topics()
#获取所有分区
分区=[]
topics.topics.items()中的meta作为名称:
对于meta.partitions.keys()中的分区\ id:
part=TopicPartition(名称、分区\u id)
partitions.append(部分)
#获取最后提交的偏移量
partitions=consumer.committed(分区)

显然,
consumer.position
不能像广告那样工作,但是
consumer.committed
返回存储的偏移量,即使消费者当前没有订阅主题/分区。

committed和position之间有什么区别?根据单据,他们都应该返回上一次提交的偏移量。position将返回上一次使用的偏移量+1。因此,出于某种原因,只有提交的有效,position不会返回正确的偏移量。Committed也可以在没有consumer.assign.position的情况下工作,是否仍然存在-1001的特定位置错误?在上述示例中,所有偏移的位置返回-1001