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

python操作kafka

时间:2015-11-18 16:11:58      阅读:594      评论:0      收藏:0      [点我收藏+]

标签:

#coding=utf-8
from pykafka import KafkaClient
import codecs
import logging
logging.basicConfig(level = logging.INFO)

client = KafkaClient(hosts = "172.16.82.163:9091")

#生产kafka数据,通过字符串形式
def produce_kafka_data(kafka_topic):
  with kafka_topic.get_sync_producer() as producer:
    for i in range(4):
      producer.produce(‘test message‘ + str(i ** 2))

#消费kafka数据
def consume_simple_kafka(kafka_topic, timeout):
  consumer = kafka_topic.get_simple_consumer(consumer_timeout_ms = timeout)
  for message in consumer:
    if message is not None:
      print message.offset, message.value

#消费同一份kafka topic时,建议使用 get_balanced_consumer(),暂时不能使用
#问题:kazoo.handlers.threading.KazooTimeoutError: Connection time-out
def consume_kafka(kafka_topic, zkhost):
  balanced_consumer = kafka_topic.get_balanced_consumer(
  consumer_group = "testgroup",
  auto_commit_enable = False,
  zookeeper_connect = zkhost,
  #zookeeper = zkhost,
  zookeeper_connection_timeout_ms = 6000,
  consumer_timeout_ms = 10000,
  )
  for message in balanced_consumer:
    if message is not None:
      print message.offset, message.value

#通过文件,往kafka刷数据
def produce_kafka_file(filename, kafka_topic):
  with kafka_topic.get_sync_producer() as producer:
    with codecs.open(filename, "r", "utf8") as rf:
      for line in rf:
        line = line.strip()
        if not line:
          continue
      producer.produce(line)

#===========================================================

topic = client.topics["mytest"]

#在consumer_timeout_ms内没有任何信息返回,则中断接受消息
cosumer = topic.get_simple_consumer(consumer_timeout_ms = 10000)
cnt = 0
for message in cosumer:
  if message is not None:
    print message.offset, message.value
  cnt += 1
print cnt

 

python操作kafka

标签:

原文地址:http://www.cnblogs.com/lg1990/p/4974684.html

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