Python's basic operations on Kafka

Source: Internet
Author: User

- -Coding:utf-8--

From Kafka import Kafkaproducer
From Kafka import Kafkaconsumer
From kafka.structs import topicpartition
Import time

Bootstrap_servers = []
Class Operatekafka:
def Init(self,bootstrap_servers,topic):
Self.bootstrap_servers = Bootstrap_servers
Self.topic = Topic

"" "Producer" "" Def Produce (self): producer = Kafkaproducer (bootstrap_servers=self.bootstrap_servers) for I in range (4): msg = "msg%d"%i producer.send (Self.topic,key=str (i), value=msg) Producer.close () "" "a consumer consumes a topic" "" Def Con Sume (self): #consumer = Kafkaconsumer (self.topic,auto_offset_reset= ' earliest ', group_id= "Testgroup", BOOTSTRAP_    servers=self.bootstrap_servers) consumer = Kafkaconsumer (self.topic,bootstrap_servers=self.bootstrap_servers) Print Consumer.partitions_for_topic (self.topic) #获取test主题的分区信息print consumer.topics () #获取主题列表print Consumer.subscription () #获取当前消费者订阅的主题print consumer.assignment () #获取当前消费者topic, partition information print Consumer.beginning_ Offsets (Consumer.assignment ()) #获取当前消费者可消费的偏移量consumer. Seek (Topicpartition (Topic=self.topic, partition=0), 1) # Resets the offset from the 1th offset to consume a for message in Consumer:print ("%s:%d:%d:key=%s value=%s"% (message.topic,message.par Tition,message.offset, Message.key,message.value)) "" "a consumer subscribes to multiple topic" "" Def Consume2(self): consumer = Kafkaconsumer (bootstrap_servers=[' 192.168.124.201:9092 ') consumer.subscribe (topics= (' TEST ', ' TEST2 ')) #订阅要消费的主题print consumer.topics () Print consumer.position (topicpartition (topic= ' TEST ', partition=0)) #                                      Gets the latest offset for the current topic for message in Consumer:print ("%s:%d:%d:key=%s value=%s"% (Message.topic, message.partition, Message.offset, Message.key, Message.value)) "" "Consumer (manual pull Cancel) "" "Def Consume3 (self): consumer = Kafkaconsumer (group_id=" MyGroup ", max_poll_records=3,bootstrap_servers=[' 192.168.124.201:9092 ']) consumer.subscribe (topics= (' TEST ', ' TEST2 ')) while true:message = Consumer.poll (timeout_ms=5 ) #从kafka获取消息 if Message:print message time.sleep (1)

def main ():
Bootstrap_servers = [' 192.168.124.201:9092 ']
TOPIC = "TEST"
Operatekafka = Operatekafka (bootstrap_servers,topic)
Operatekafka.produce ()
#operateKafka. Consume ()
#operateKafka. Consume2 ()
Operatekafka.consume3 ()
Main ()

Python's basic operations on Kafka

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.