怎么在python3中连接kafka模块?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
1.1安装模块
pip install pykafka
1.2基本使用
# -* coding:utf8 *-
from pykafka import KafkaClient
host = 'IP:9092, IP:9092, IP:9092'
client = KafkaClient(hosts = host)
# 生产者
topicdocu = client.topics['my-topic']
producer = topicdocu.get_producer()
for i in range(100):
print i
producer.produce('test message ' + str(i ** 2))
producer.stop()
1.3简单封装
class KafkaProduct():
def __init__(self,hosts,topic):
"""
初始化实例
:param hosts: 连接地址
:param topic:
"""
self.__client = KafkaClient(hosts=hosts)
self.__topic = self.__client.topics[topic.encode()]
def __set_topic(self, topic):
self.__topic = self.__client.topics[topic.encode()]
def set_topic(self, topic):
"""
设置topic
:param topic:
:return:
"""
self.__set_topic(topic)
def get_topics(self):
"""
获取当前所有topic
:return:
"""
return self.__client.topics
def get_topic(self):
"""
获取当前topic
:return:
"""
return self.__topic
def Producer(self):
"""
生产者对象
:return:
"""
with self.__topic.get_producer(delivery_reports=True) as producer:
next_data = ''
while True:
if next_data:
producer.produce(str(next_data).encode())
next_data = yield True
def send_data(self,datas):
"""
发送数据
:param datas:需要传入的可迭代对象
:return:
"""
c = self.Producer()
next(c)
for i in datas:
c.send(i)
if __name__ == '__main__':
hosts = "1.2.3.4:9999,2.3.4.5:9090" #连接hosts
topic = "test_523"
K = KafkaProduct(hosts=hosts, topic=topic) #
#K.set_topic("test") #切换设置新的topic
K.get_topic() #获取当前设置的topic
#K.get_topics() #获取所有topic
data = range(10000) #要发送的可迭代对象
K.send_data(data)
关于怎么在python3中连接kafka模块问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。