Python connection Kafka distributed messaging System Instance Tutorial

Source: Internet
Author: User
Tags zookeeper in python

The recent project always with Java, I write a Python programmer, in the face of complex data structure of Java code into Python code, is indeed a big problem, sometimes more or less will leave a bit of a hole, it seems to have to look at the Java Foundation. This is not the beginning to let us connect Kafka again today. The company's Kafka and zookeeper do a cluster, connection more trouble, specific how to use, Java that side do the package I also can not see, so only through simple communication.

Begin

Begin to find the standard library of Python connection Kafka, Kafka-python and Pykafka the former are more mature libraries, the latter is an upgraded version of Samsa, online to the article in Python Connection and use Kafka Using Samsa to connect zookeeper and then use Kafka cluster is very satisfying my needs, in the case of Pykafka also see ZK support, and Kafka-python does not have ZK support, so chose Pykafka as a connection library

Conceptual issues

Kafaka and zookeeper cluster, the use of Samsa when the producers and consumers are connected to the zookeeper, but I with the peak cloud (large data Daniel, Yun-Wei, Pan knock Widows ┕ low ǎ?? The sheath of Mei Neon is a coincidence?? Waved?? Afaka server list, consumers only use zookeeper. This also solves me to see the Pykafka document, only the consumer only then connects the zookeeper confusion, therefore solves, directly according to the document to start.

Producers


>>> from Pykafka import kafkaclient
>>> client = kafkaclient (hosts= "192.168.1.1:9092, 192.168.1.2:9092") # acceptable multiple client that's the point.
>>> client.topics # View all topic
>>> topic = client.topics[' my.test '] # Choose a topic
>>> producer = Topic.get_producer ()
>>> producer.produce ([' Test message ' + str (i * * 2) for I in range (4)]) # plus a STR official example py2.7 run
But

Consumers


>>> Balanced_consumer = Topic.get_balanced_consumer (
consumer_group= ' Testgroup ',
Auto_commit_enable=true, # No need to add consumer_group when set to Flase
Zookeeper_connect= ' Myzkclusternode1.com:2181,myzkclusternode2.com:2181/myzkchroot ' # here is the connection to multiple ZK
)

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.