【问题标题】:elasticsearch bulk indexing using python使用python进行弹性搜索批量索引
【发布时间】:2013-10-16 19:21:57
【问题描述】:

我正在尝试使用 python pyes 模块将具有 6M 记录的 csv 文件索引到 elasticsearch,代码逐行读取记录并将其推送到 elasticsearch...知道如何将其作为批量发送吗?

import csv
from pyes import *
import sys

header = ['col1','col2','col3','col3', 'col4', 'col5', 'col6']

conn = ES('xx.xx.xx.xx:9200')

counter = 0

for row in reader:
    #print len(row)
    if counter >= 0:
        if counter == 0:
            pass
        else:
            colnum = 0
            data = {}
            for j in row:
                data[header[colnum]] = str(j)
                colnum += 1
            print data
            print counter
            conn.index(data,'accidents-index',"accidents-type",counter)
    else:
        break

    counter += 1

【问题讨论】:

标签: python linux elasticsearch


【解决方案1】:

pyelasticsearch 支持批量索引:

bulk_index(index, doc_type, docs, id_field='id', parent_field='_parent'[, other kwargs listed below])

例如,

cities = []
for line in f:
    fields = line.rstrip().split("\t")
    city = { "id" : fields[0], "city" : fields[1] }
    cities.append(cities)
    if len(cities) == 1000:
        es.bulk_index(es_index, "city", cities, id_field="id")
        cities = []
if len(cities) > 0:
    es.bulk_index(es_index, "city", cities, id_field="id")

【讨论】:

  • @krisdigitx 我不明白为什么这种方法不适合 6M 的数据。调整块中的文档数量以获得最佳性能,你很好。大量 1000 个文档是一个很好的起点。
  • 批量文档的数量限制是多少?如果我把它推到 10,000,批量可以处理吗?如果没有,它是否能够适应并将这 10,000 个分成块?
猜你喜欢
  • 2016-06-20
  • 1970-01-01
  • 2018-04-07
  • 2017-10-22
  • 1970-01-01
  • 2016-04-13
  • 2017-07-03
  • 2021-08-16
  • 2019-06-03
相关资源
最近更新 更多