小编典典

将CSV索引到Python中的ElasticSearch

elasticsearch

希望不使用Logstash将CSV文件索引到ElasticSearch。我正在使用elasticsearch-dsl高级库。

给定带有标题的CSV例如:

name,address,url
adam,hills 32,http://rockit.com
jane,valleys 23,http://popit.com

按字段索引所有数据的最佳方法是什么?最终我希望让每一行看起来像这样

{
"name": "adam",
"address": "hills 32",
"url":  "http://rockit.com"
}

阅读 344

收藏
2020-06-22

共1个答案

小编典典

使用较低级的elasticsearch-py库,这种任务比较容易:

from elasticsearch import helpers, Elasticsearch
import csv

es = Elasticsearch()

with open('/tmp/x.csv') as f:
    reader = csv.DictReader(f)
    helpers.bulk(es, reader, index='my-index', doc_type='my-type')
2020-06-22