kafka-python批量发送数据的实例
程序员文章站
2022-03-20 15:08:17
如下所示:
from kafka import kafkaclient
from kafka.producer import simpleproducer...
如下所示:
from kafka import kafkaclient from kafka.producer import simpleproducer
def send_data_2_kafka(datas): ''' 向kafka解析队列发送数据 ''' client = kafkaclient(hosts=kafkabroker.split(","), timeout=30) producer = simpleproducer(client, async=false) curcount = len(datas)/partnum for i in range(0, partnum): start = i*curcount if i != partnum - 1: end = (i+1)*curcount curdata = datas[start:end] producer.send_messages(topicname, *curdata) else: curdata = datas[start:] producer.send_messages(topicname, *curdata) producer.stop() client.close()
其中partnum为topic的partition的数目,这样保证批量发送的数据均匀的落在kafka的partition中。
以上这篇kafka-python批量发送数据的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。