读取topic数据存储到文件内

基于python3.6

from pykafka import KafkaClient
import logging

logging.basicConfig(level=logging.INFO)
def scantopic(mytopic,filename,host): tp = mytopic.encode('utf-8') client = KafkaClient(hosts=host) topic = client.topics[tp] consumer = topic.get_simple_consumer() for record in consumer: if record is not None: valuestr = record.value.decode() with open(filename,'a',encoding='utf-8') as f: f.write(valuestr+' ') print(valuestr) if __name__=="__main__": scantopic("topic",r"filepath",'10.xx.xx.:9092,10.x.xx.x:9092')
原文地址:https://www.cnblogs.com/lccyb/p/9700749.html