将CSV索引到Python中的ElasticSearch [英] Index CSV to ElasticSearch in Python
本文介绍了将CSV索引到Python中的ElasticSearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
希望在不使用Logstash的情况下将CSV文件索引到ElasticSearch.
我正在使用elasticsearch-dsl
高级库.
Looking to index a CSV file to ElasticSearch, without using Logstash.
I am using the elasticsearch-dsl
high level library.
给出一个带有标题的CSV,例如:
Given a CSV with header for example:
name,address,url
adam,hills 32,http://rockit.com
jane,valleys 23,http://popit.com
按字段索引所有数据的最佳方法是什么?最终,我希望让每一行看起来像这样
What will be the best way to index all the data by the fields? Eventually I'm looking to get each row to look like this
{
"name": "adam",
"address": "hills 32",
"url": "http://rockit.com"
}
推荐答案
使用较低级别的elasticsearch-py
库,这种任务就更容易了:
This kind of task is easier with the lower-level elasticsearch-py
library:
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')
这篇关于将CSV索引到Python中的ElasticSearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文