-
-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