我有一个 .json.gz
文件,我希望将其加载到 Elasticsearch 中。
我的第一次尝试涉及使用 json
模块将 JSON 转换为字典列表。
import gzip
import json
from pprint import pprint
from elasticsearch import Elasticsearch
nodes_f = gzip.open("nodes.json.gz")
nodes = json.load(nodes_f)
字典示例:
pprint(nodes[0])
{u'index': 1,
u'point': [508163.122, 195316.627],
u'tax': u'fehwj39099'}
使用 Elasticsearch :
es = Elasticsearch()
data = es.bulk(index="index",body=nodes)
但是,这会返回:
elasticsearch.exceptions.RequestError: TransportError(400, u'illegal_argument_exception', u'Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_STRING]')
除此之外,我希望能够找到给定 point
查询的 tax
,以防这对我应该如何使用 elasticsearch 索引数据产生影响.
最佳答案
Alfe 为我指明了正确的方向,但我无法让他的代码正常工作。
我找到了两个解决方案:
逐行使用 for 循环:
es = elasticsearch.Elasticsearch()
for node in nodes:
_id = node['index']
es.index(index='nodes',doc_type='external',id=_id,body=node)
批量使用helper
:
actions = [
{
"_index" : "nodes_bulk",
"_type" : "external",
"_id" : str(node['index']),
"_source" : node
}
for node in nodes
]
helpers.bulk(es,actions)
对于 343724
个字典的列表,批量处理的速度大约是 22
倍。
关于python - 使用 python 将字典或 JSON 文件列表导入 Elasticsearch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42623636/