Python aiokafka消费者
0、简单介绍
AIOKafkaConsumer是一个使用来自 Kafka 集群的记录的客户端。
官方文档:https://aiokafka.readthedocs.io/en/stable/consumer.html#consumer-usage
1、测试的版本说明
测试的Python版本是Python3.6
测试的aiokafka版本是0.7.0
2、普通消费者
import asyncio
import aiokafka
async def consumer_msg(loop):
consumer = aiokafka.AIOKafkaConsumer("my_topic", loop=loop, bootstrap_servers='192.168.1.3:9092')
await consumer.start()
try:
async for msg in consumer:
# ConsumerRecord(topic='my_topic',
# partition=0,
# offset=0,
# timestamp=1646450078237,
# timestamp_type=0,
# key=None,
# value=b'1',
# checksum=2657958227,
# serialized_key_size=-1,
# serialized_value_size=1)
print(msg)
print(msg.value)
finally:
print("退出消费")
await consumer.stop()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(consumer_msg(loop))
运行
3、用于自我调试使用(接收信息,接收到exit后退出)
import asyncio
import aiokafka
async def consumer_msg(loop, ip, topic):
consumer = aiokafka.AIOKafkaConsumer(topic, loop=loop, bootstrap_servers=f'{ip}:9092')
await consumer.start()
exit_str = 'exit'
print(f"开始消费, 发送{exit_str}到kafka退出消费!")
try:
async for msg in consumer:
# 信息包含如下
# ConsumerRecord(topic='my_topic',
# partition=0,
# offset=0,
# timestamp=1646450078237,
# timestamp_type=0,
# key=None,
# value=b'1',
# checksum=2657958227,
# serialized_key_size=-1,
# serialized_value_size=1)
print(msg)
value = msg.value.decode()
print(f"消费到的数据:{value}")
if value.lower() == 'exit':
print("****告辞****")
break
except Exception as e:
print(f"消费消息失败, 异常:{e}")
finally:
print("退出消费")
await consumer.stop()
def debug():
local_ip = "192.168.1.3"
topic = "my_topic"
loop = asyncio.get_event_loop()
loop.run_until_complete(consumer_msg(loop, local_ip, topic))
if __name__ == '__main__':
debug()
运行
生产者链接:https://www.cnblogs.com/rainbow-tan/p/15966440.html
docker kafka部署链接:https://www.cnblogs.com/rainbow-tan/p/15932030.html