Python Operation Kafka

Source: Internet
Author: User

#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数据, in string form
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

when #消费同一份kafka topic, it is recommended to use Get_balanced_consumer (), temporarily unable to use
#问题: 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

#通过文件, go to Kafka Brush data
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:

if not line:
continue
producer.produce (line)

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

TOPIC = client.topics["MyTest"]

#在consumer_timeout_ms内没有任何信息返回, the Accept message is interrupted
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 Operation Kafka

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.