python消费kafka数据批量插入到es的方法

本文介绍了如何使用Python结合pykafka消费Kafka消息,并利用Elasticsearch进行批量数据插入。首先,配置信息存于logging.conf中,安装Elasticsearch依赖。接着,针对Kafka 0.8版本,使用get_simple_consumer消费,每个应用独占一个partition以避免重复消费。通过设置consumer_timeout_ms实现超时退出,并用while True循环保证持续消费。最后,数据积累到一定量后一次性批量插入到Elasticsearch。目前该方法正处于批量压测阶段。
摘要由CSDN通过智能技术生成

今天小编就为大家分享一篇python消费kafka数据批量插入到es的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
1、es的批量插入

这是为了方便后期配置的更改,把配置信息放在logging.conf中

用elasticsearch来实现批量操作,先安装依赖包,sudo pip install Elasticsearch2

from elasticsearch import Elasticsearch 
class ImportEsData:
 
  logging.config.fileConfig("logging.conf")
  logger = logging.getLogger("msg")
 
  def __init__(self,hosts,index,type):
    self.es = Elasticsearch(hosts=hosts.strip(',').split(','), timeout=5000)
    self.index = index
    self.type = type
 
 
  def set_date(self,data): 
    # 批量处理 
    # es.index(index="test-index",doc_type="test-type",id=42,body={"any":"data","timestamp":datetime.now()})
    self.es.index(index=self.index,doc_type=self.index,body=data)

2、使用pykafka消费kafka

1.因为kafka是0.8,pykafka不支持zk,只能用get_simple_

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值