安装pykafka库
1.通过interpreter setting安装pykafka库
或者通过命令安装2.pip3 install pykafka
#!/usr/bin/python# -*- coding:utf-8 -*-from pykafka import KafkaClientclient = KafkaClient(hosts="10.XX0.XX0.XX4:9092") # 可接受多个client# 查看所有的topic# print(client.topics)topic = client.topics['test_78'] # 选择一个topicmessage = "test message2 test message2"with topic.get_sync_producer() as producer: producer.produce(bytes(message, encoding='utf8')) print(message)
二.消费kafka 的topic消息安装kafka库,安装方式如上--pykafka库
#!/usr/bin/python# -*- coding:utf-8 -*-from kafka import KafkaConsumerkafka_topic = "test_78878"kafka_topic_group = "test_kafka_group"kafka_bootstrap_servers = ['10.XX0.XX0.XX4:9092']consumer = KafkaConsumer(kafka_topic,group_id=kafka_topic_group,bootstrap_servers=kafka_bootstrap_servers)for message in consumer: print ("%s:key=%s value=%s" % (message.topic, message.key,message.value))