python對kafka的基本操作

來源:互聯網
上載者:User

標籤:apr   手動   cer   rod   structs   res   struct   scribe   set   

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

"""生產者"""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()"""一個消費者消費一個topic"""def consume(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、分區資訊print consumer.beginning_offsets(consumer.assignment()) #擷取當前消費者可消費的位移量consumer.seek(TopicPartition(topic=self.topic, partition=0), 1)  #重設位移量,從第1個位移量消費    for message in consumer:        print ("%s:%d:%d: key=%s value=%s"         % (message.topic,message.partition,message.offset, message.key,message.value))"""一個消費者訂閱多個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)) #擷取當前主題的最新位移量for message in consumer:        print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,                                      message.offset, message.key,                                      message.value))"""消費者(手動拉取訊息)"""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對kafka的基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.