欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3連接kafka模塊pykafka生產(chǎn)者簡(jiǎn)單封裝代碼

 更新時(shí)間:2019年12月23日 09:18:38   作者:清水漁漁  
今天小編就為大家分享一篇python3連接kafka模塊pykafka生產(chǎn)者簡(jiǎn)單封裝代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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簡(jiǎn)單封裝

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)者對(duì)象
  :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:需要傳入的可迭代對(duì)象
  :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ā)送的可迭代對(duì)象
K.send_data(data)

以上這篇python3連接kafka模塊pykafka生產(chǎn)者簡(jiǎn)單封裝代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python統(tǒng)計(jì)一個(gè)文本中重復(fù)行數(shù)的方法

    python統(tǒng)計(jì)一個(gè)文本中重復(fù)行數(shù)的方法

    這篇文章主要介紹了python統(tǒng)計(jì)一個(gè)文本中重復(fù)行數(shù)的方法,涉及針對(duì)Python中dict對(duì)象的使用及相關(guān)本文的操作,具有一定的借鑒價(jià)值,需要的朋友可以參考下
    2014-11-11
  • python UDP(udp)協(xié)議發(fā)送和接收的實(shí)例

    python UDP(udp)協(xié)議發(fā)送和接收的實(shí)例

    今天小編就為大家分享一篇python UDP(udp)協(xié)議發(fā)送和接收的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python Pandas實(shí)現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例

    Python Pandas實(shí)現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例

    今天小編就為大家分享一篇Python Pandas實(shí)現(xiàn)數(shù)據(jù)分組求平均值并填充nan的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • 利用Python過濾相似文本的簡(jiǎn)單方法示例

    利用Python過濾相似文本的簡(jiǎn)單方法示例

    這篇文章主要給大家介紹了關(guān)于利用Python過濾相似文本的簡(jiǎn)單方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 淺談pycharm出現(xiàn)卡頓的解決方法

    淺談pycharm出現(xiàn)卡頓的解決方法

    今天小編就為大家分享一篇淺談pycharm出現(xiàn)卡頓的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python 中的@property的用法詳解

    python 中的@property的用法詳解

    這篇文章主要介紹了python @property的用法,簡(jiǎn)單地說就是一個(gè)類里面的方法一旦被@property裝飾,就可以像調(diào)用屬性一樣地去調(diào)用這個(gè)方法,它能夠簡(jiǎn)化調(diào)用者獲取數(shù)據(jù)的流程,感興趣的朋友跟隨小編一起看看吧
    2022-06-06
  • 教你如何利用python3爬蟲爬取漫畫島-非人哉漫畫

    教你如何利用python3爬蟲爬取漫畫島-非人哉漫畫

    本文給大家分享利用python3爬蟲爬取漫畫島-非人哉漫畫,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友跟隨小編一起學(xué)習(xí)下吧
    2021-07-07
  • 舉例詳解Python中threading模塊的幾個(gè)常用方法

    舉例詳解Python中threading模塊的幾個(gè)常用方法

    這篇文章主要介紹了舉例詳解Python中threading模塊的幾個(gè)常用方法,threading模塊用來創(chuàng)建和操作線程,是Python學(xué)習(xí)當(dāng)中的重要知識(shí),需要的朋友可以參考下
    2015-06-06
  • python實(shí)現(xiàn)文件批量重命名

    python實(shí)現(xiàn)文件批量重命名

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)文件批量重命名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • python實(shí)現(xiàn)爬取百度圖片的方法示例

    python實(shí)現(xiàn)爬取百度圖片的方法示例

    這篇文章主要介紹了python實(shí)現(xiàn)爬取百度圖片的方法,涉及Python基于requests、urllib等模塊的百度圖片抓取相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07

最新評(píng)論