python 与kafka 交互

import time
from kafka import KafkaProducer, KafkaConsumer

if __name__ == "__main__":
    # 发送数据
    kafkaProducer = KafkaProducer(bootstrap_servers=["172.17.250.33:9092"])

    for i in range(0, 10):
        # 同步发送 topic 可写多个
        a = kafkaProducer.send(topic="test", value=f'{i}'.encode("UTF-8")).get()
        print(a.topic)
        print(a.offset)
        print(a.partition)
        # 异步发送
        kafkaProducer.send(topic="test", value=f'{i}'.encode("UTF-8"))
    time.sleep(0.1)

    # 消费数据 topic 可写多个
    kafkaConsumer = KafkaConsumer("test", bootstrap_servers=["172.17.250.33:9092"], group_id="g_01")
    for message in kafkaConsumer:
        topic = message.topic
        partition = message.partition
        offset = message.offset
        key = message.key
        value = message.value.decode("UTF-8")
        print(f'从{topic}的{partition}分片上,获取第{offset}偏移量为{key}--{value}')

你可能感兴趣的:(kafka,python,kafka,交互,分布式)