python kafka生产者和消费者的配置
生产者
producer = KafkaProducer(
bootstrap_servers=['localhost:9092'],
value_serializer=lambda m: json.dumps(m,ensure_ascii=False).encode()
)
kafka_result=json.dumps(send_data_result,ensure_ascii=False)
producer.send('ai.data', json.loads(kafka_result))
消费者
from kafka import KafkaConsumer, KafkaProducer
import json
# 定义 Topic 与 kafka_server 等变量
topic = "ai.data"
kafka_server = ['']
## 创建 Consumer 实例
consumer = KafkaConsumer(topic, bootstrap_servers=kafka_server)
print(consumer)
# 定义Kafka 消息处理逻辑
for msg in consumer:
print(msg.value)