python使用kafka-python 操作Kafka

Pyhon操作Kafka数据库

1.Python需要安装pykafka模块

pip install pykafka

2.python 存入kafka消息

# -*- coding: utf-8 -*-

from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers=['127.0.0.1:9092'])

#此处ip可以是多个['0.0.0.1:9092','0.0.0.2:9092','0.0.0.3:9092' ]

for i in range(3):

msg = "msg %d" % i

print msg

producer.send('test', msg)

producer.close()

3.读取本地的所有 topic 信息

from pykafka import KafkaClient

client = KafkaClient(hosts="127.0.0.1:9092")

for topic in client.topics:

print topic

4.查看brokers信息

from pykafka import KafkaClient

client = KafkaClient(hosts="127.0.0.1:9092")

print client.brokers

for n in client.brokers:

host = client.brokers[n].host

port = client.brokers[n].port

id = client.brokers[n].id

print "host=%s | port=%s | broker.id=%s " %(host,port,id)

5.直接消费kafka

#encoding:utf8

from pykafka import KafkaClient

client = KafkaClient(hosts="127.0.0.1:9092")

topic = client.topics['test']

# 获取 consumer 消费者

consumer = topic.get_simple_consumer(consumer_group="test",reset_offset_on_start=True)

for message in consumer:

print message

if message is not None:

print ">>>>>>>>>>",message.offset

print ">>>>>>>>>>",message.value

6.从zookeeper消费

from pykafka import KafkaClient

client = KafkaClient(hosts="127.0.0.1:9092")

topic = client.topics['test']

balanced_consumer= topic.get_balanced_consumer(consumer_group='test',auto_commit_enable=True,reset_offset_on_start=True,zookeeper_connect='127.0.0.1:9092')

for message in balanced_consumer:

if message is not None:

print message.offset, message.value

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值