python3連接kafka模塊pykafka生產(chǎn)者簡單封裝代碼
更新時(shí)間:2019年12月23日 09:18:38 作者:清水漁漁
今天小編就為大家分享一篇python3連接kafka模塊pykafka生產(chǎn)者簡單封裝代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
1.1安裝模塊
pip install pykafka
1.2基本使用
# -* coding:utf8 *-
from pykafka import KafkaClient
host = 'IP:9092, IP:9092, IP:9092'
client = KafkaClient(hosts = host)
# 生產(chǎn)者
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):
"""
初始化實(shí)例
: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):
"""
設(shè)置topic
:param topic:
:return:
"""
self.__set_topic(topic)
def get_topics(self):
"""
獲取當(dāng)前所有topic
:return:
"""
return self.__client.topics
def get_topic(self):
"""
獲取當(dāng)前topic
:return:
"""
return self.__topic
def Producer(self):
"""
生產(chǎn)者對象
: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):
"""
發(fā)送數(shù)據(jù)
: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") #切換設(shè)置新的topic
K.get_topic() #獲取當(dāng)前設(shè)置的topic
#K.get_topics() #獲取所有topic
data = range(10000) #要發(fā)送的可迭代對象
K.send_data(data)
以上這篇python3連接kafka模塊pykafka生產(chǎn)者簡單封裝代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 在python環(huán)境下運(yùn)用kafka對數(shù)據(jù)進(jìn)行實(shí)時(shí)傳輸?shù)姆椒?/a>
- kafka-python批量發(fā)送數(shù)據(jù)的實(shí)例
- 對python操作kafka寫入json數(shù)據(jù)的簡單demo分享
- python3實(shí)現(xiàn)從kafka獲取數(shù)據(jù),并解析為json格式,寫入到mysql中
- python消費(fèi)kafka數(shù)據(jù)批量插入到es的方法
- python kafka 多線程消費(fèi)者&手動提交實(shí)例
- python 消費(fèi) kafka 數(shù)據(jù)教程
- python每5分鐘從kafka中提取數(shù)據(jù)的例子
- python操作kafka實(shí)踐的示例代碼
- 快速上手Python Kafka庫安裝攻略
相關(guān)文章
python統(tǒng)計(jì)一個(gè)文本中重復(fù)行數(shù)的方法
這篇文章主要介紹了python統(tǒng)計(jì)一個(gè)文本中重復(fù)行數(shù)的方法,涉及針對Python中dict對象的使用及相關(guān)本文的操作,具有一定的借鑒價(jià)值,需要的朋友可以參考下2014-11-11
python UDP(udp)協(xié)議發(fā)送和接收的實(shí)例
今天小編就為大家分享一篇python UDP(udp)協(xié)議發(fā)送和接收的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python Pandas實(shí)現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例
今天小編就為大家分享一篇Python Pandas實(shí)現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
舉例詳解Python中threading模塊的幾個(gè)常用方法
這篇文章主要介紹了舉例詳解Python中threading模塊的幾個(gè)常用方法,threading模塊用來創(chuàng)建和操作線程,是Python學(xué)習(xí)當(dāng)中的重要知識,需要的朋友可以參考下2015-06-06
python實(shí)現(xiàn)爬取百度圖片的方法示例
這篇文章主要介紹了python實(shí)現(xiàn)爬取百度圖片的方法,涉及Python基于requests、urllib等模塊的百度圖片抓取相關(guān)操作技巧,需要的朋友可以參考下2019-07-07

