import kafka
boot_servers = "xxx.xxx.xxx.xxx"
topic_name = "topic_test_1"
def getTopicInfo(boot_servers,topic_name):
'''
@return: partition_no begin_offset end_offset
'''
##超时设置10秒
mc=kafka.KafkaConsumer(topic_name,bootstrap_servers=boot_servers,request_timeout_ms=10000)
ptnlst=mc.partitions_for_topic(topic_name)
if not ptnlst:
print("%sthere is no topic named '%s' in this cluster%s"%('!'*10,topic_name,'!'*10))
raise ValueError
res=[]
for pn in ptnlst:
tps=kafka.TopicPartition(topic_name,pn)
boft=mc.beginning_offsets([tps])
eoft=mc.end_offsets([tps])
#python3
if sys.version_info.major >=3:
res.append((pn,list(boft.values())[0],list(eoft.values())[0]))
else:
#python2
res.append((pn,boft.values()[0],eoft.values()[0]))
if not res:
print("%sthere is no topic named '%s' in this cluster%s"%('!'*10,topic_name,'!'*10))
raise ValueError
return res
if __name__ == '__main__':
getTopicInfo(boot_servers,topic_name)
用python获取kafka起始以及结束断点位置
最新推荐文章于 2023-08-25 10:23:40 发布