kafka zookeeper

import json

from pykafka import KafkaClient
from pykafka.simpleconsumer import SimpleConsumer
import time

topic = 'qq'
data = {
"ddgd":"dgdfg",
"ddd":"fdg"
}


class kafkaConfig:

def __init__(self):

# 如果有chroot path 就放到端口号后面/kfk1
zookeeper_hosts = '10.200.224.13:2181,10.200.224.9:2181,10.200.224.2:2181'
try:
self.client = KafkaClient(zookeeper_hosts=zookeeper_hosts)
print('[conn success!]')
except Exception as e:
print('[exception conn]:%s,[msg]:%s' % (e, self.client))

def topic_lists(self):
try:
topic_lists = self.client.topics
print('[topic lists]:%s' % topic_lists)
except Exception as e:
print('[exception topic lists]:%s' % (e, topic_lists))
return topic_lists

# produce 同步消息send 单条消息 需要等待
def msg_sync_send(self, topic, content):
try:
topic = self.client.topics[topic]
with topic.get_sync_producer() as produce:
produce.produce(bytes(content, encoding='utf-8'), timestamp=round(time.time() * 1000))
print('[send sync success!]')
return "send sync success!"
except Exception as e:
print('[exception sync send]:%s' % e)

# produce 异步消息send 单条消息 高吞吐量
def msg_async_send(self, topic, content):
try:
topic = self.client.topics[topic]
with topic.get_producer() as produce:
produce.produce(bytes(content, encoding='utf-8'), timestamp=round(time.time() * 1000))
print('[send async success!]')
except Exception as e:
print('[exception async send]:%s' % e)


if __name__ == '__main__':
kafka = kafkaConfig()
kafka.msg_sync_send(topic=topic,
content=json.dumps(data))
posted @ 2020-03-30 10:33  千年妖狐  阅读(377)  评论(0编辑  收藏  举报