码迷,mamicode.com
首页 > 编程语言 > 详细

python&kafka集群取消息

时间:2020-06-24 21:46:18      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:click   new   boot   span   false   numbers   tool   cli   port   

安装

pip3 install setuptools

pip3 install pykafka

pip3 install kafka-python

 

单台kafka

import sys
from kafka import KafkaConsumer
from kafka.structs import TopicPartition
consumer = KafkaConsumer(
    enable_auto_commit=False,
    bootstrap_servers=["xxx-xx-x-sh-yyy-Eyy.yd.com:1234"])
consumer.subscribe(topics=(indata_str_abcde_click_new))

if sys.getdefaultencoding() != utf-8:
    reload(sys)
    sys.setdefaultencoding(utf-8)

for message in consumer:
    print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,
    message.offset, message.key,
    message.value.decode()))

kafka集群 

bootstrap_servers 参数使用‘‘,‘‘,‘‘隔开
import sys
from kafka import KafkaConsumer
from kafka.structs import TopicPartition
consumer = KafkaConsumer(
    enable_auto_commit=False,
    bootstrap_servers=["xxx-xx-x-sh-yyy-Eyy.yd.com:1234""xxx-xx-x-sh-yyy-Eyy.yd.com:1231""xxx-xx-x-sh-yyy-Eyy.yd.com:1232"])
consumer.subscribe(topics=(indata_str_abcde_click_new))

if sys.getdefaultencoding() != utf-8:
    reload(sys)
    sys.setdefaultencoding(utf-8)

for message in consumer:
    print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,
    message.offset, message.key,
    message.value.decode()))

可成功拿到消息

 


 

 

python&kafka集群取消息

标签:click   new   boot   span   false   numbers   tool   cli   port   

原文地址:https://www.cnblogs.com/jxba/p/13189886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!