elasticsearch 使用bulk导入数据

#!/usr/bin/python
#coding=utf-8
#op type: index, create , delete, update
#www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch import helpers
es   = Elasticsearch()
es.indices.put_settings(index="index_0",body={"refresh_interval":-1,"number_of_replicas":0}) # for single node
file ="/home/allen/bbb.txt"
f=open(file,'r')
actions = []
for i in f:
    if i=="" or i=="\n" or i=="\r\n":
        continue
    action={
        "_op_type":"index",
        "_index":"index_0",
        "_type":"type_0",
        "_source":{
            "content":i
        }
    }
    actions.append(action)
helpers.bulk(es,actions)
f.close()
es.indices.put_settings(index="index_0",body={"refresh_interval":"1s"}) # change back to default

使用bulk之前最好设置一下某些参数,可以使用put_settings()函数
参考资料

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值