Python connects Kafka producers, consumer scripts

Source: Internet
Author: User

#-*-coding:utf-8-*-" "' use Kafka-python 1.3.3 Module # pip install kafka==1.3.5 # pip install kafka-python==1.3.5" "ImportSYSImport TimeImportJSON fromKafkaImportKafkaproducer fromKafkaImportKafkaconsumer fromKafka.errorsImportKafkaerrorkafaka_host="101.236.51.235"Kafaka_port= 9092Kafaka_topic="Test"classkafka_producer ():" "' Production module: Differentiate messages based on different key" "    def __init__(self, kafkahost,kafkaport, Kafkatopic, key): Self.kafkahost=kafkahost Self.kafkaport=Kafkaport self.kafkatopic=kafkatopic Self.key=KeyPrint("producer:h,p,t,k", Kafkahost,kafkaport,kafkatopic,key) bootstrap_servers='{Kafka_host}:{kafka_port}'. Format (kafka_host=Self.kafkahost, Kafka_port=self.kafkaport)Print("Boot SVR:", bootstrap_servers) self.producer= Kafkaproducer (Bootstrap_servers =bootstrap_servers)defsendjsondata (self, params):Try: Parmas_message= Json.dumps (params,ensure_ascii=False) Producer=Self.producerPrint(parmas_message) v= Parmas_message.encode ('Utf-8') K= Key.encode ('Utf-8')            Print("send msg: (K,V)", K,v) producer.send (self.kafkatopic, Key=k, value=v) producer.flush ()exceptKafkaerror as E:Print(e)classKafka_consumer ():" ""Consumption module: Consumption of topic messages through different GroupID" "    def __init__(self, kafkahost, Kafkaport, Kafkatopic, GroupID): Self.kafkahost=kafkahost Self.kafkaport=Kafkaport self.kafkatopic=kafkatopic self.groupid=GroupID Self.key=Key Self.consumer= Kafkaconsumer (self.kafkatopic, group_id =Self.groupid, Bootstrap_servers='{Kafka_host}:{kafka_port}'. Format (kafka_host=Self.kafkahost, Kafka_port=self.kafkaport))defConsume_data (self):Try:             forMessageinchSelf.consumer:yieldmessageexceptKeyboardinterrupt as E:Print(e)defMain (xtype, group, key):" ""Test consumer and producer" "    ifXtype = ="P":        #Production ModuleProducer =kafka_producer (Kafaka_host, Kafaka_port, Kafaka_topic, key)Print("===========> Producer:", producer) for_idinchRange (100): Params='{"MSG": "%s"}'%Str (_id) params=[{"msg0": _id},{"MSG1": _id}] Producer.sendjsondata (params) time.sleep (1)    ifXtype = ='C':        #Consumer ModuleConsumer =Kafka_consumer (Kafaka_host, Kafaka_port, Kafaka_topic, group)Print("===========> Consumer:", consumer) message=Consumer.consume_data () forMsginchmessage:Print('msg---------------->k,v', Msg.key,msg.value)Print('offset---------------->', Msg.offset)if __name__=='__main__': Xtype= Sys.argv[1] group= Sys.argv[2] Key= Sys.argv[3] Main (xtype, group, key)

How to use
Production messages

Python testkafka.py p G K
Consumer News

Python testkafka.py c G K

Python connects Kafka producers, consumer scripts

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.