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