Python # Pykafka-producer and consume

Source: Internet
Author: User

###

Python version: 2.7.13

Pykafka version: 2.6.0

Note: Python 3.6. Version 2 will give an error.

Note: This is a production data via Pykafka module to Kafka

GitHub Address: Https://github.com/Parsely/pykafka

Pykafka doc:http://pykafka.readthedocs.io/en/latest/usage.html

producer.py

#-*-Coding:utf-8-*-from pykafka Import kafkaclientclient = Kafkaclient (hosts= "192.168.0.100:9092") # can accept multiple Clientprint client.topics# View All Topictopic = client.topics[' Test ']# Select a topicmessage = "test message Test Message" # When you have topic, You can create a producer to send messages that produce Kafka data, in string form with Topic.get_sync_producer () as producer: for    I in range (4):            Producer.produce (' test message ' + str (i * * 2))

###

consume.py

#-*-coding:utf-8-*- fromPykafkaImportkafkaclientclient= Kafkaclient (hosts='192.168.0.100:9092') Topic=client.topics['Test']balanced_consumer=Topic.get_balanced_consumer (Consumer_group='Test_kafka_group', Auto_commit_enable=False,#when set to false do not need to add consumer_group, directly connected to topic can be taken to the messagezookeeper_connect='192.168.0.100:2181'#Here you can connect multiple ZK) forMessageinchBalanced_consumer:#Print Message    ifMessage is  notNone:PrintMessage.offset, Message.value#print the number of offsets and values of the received message body

###

Python # Pykafka-producer and consume

Related Article

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.