【问题标题】:Trying to load json docs in elasticsearch尝试在弹性搜索中加载 json 文档
【发布时间】:2020-04-08 05:36:49
【问题描述】:

我正在尝试关注此answer,但在批量方法的操作参数中出现错误。 我可以使用 next 生成 json 对象数组,但是当我将它传递给 helpers.bulk 时出现错误。

这是我的代码:

from elasticsearch import Elasticsearch, helpers
import sys, json
import os

es = Elasticsearch("localhost:9200")
filename = "path_to_file.json"
def load_json(filename):
#     " Use a generator, no need to load all in memory"
print(filename)
with open(filename,'r') as open_file:
    yield json.load(open_file)`

helpers.bulk(es, load_json(filename), index='cllimaster_test', doc_type='clli_data_test')

错误:

【问题讨论】:

    标签: elasticsearch elasticsearch-py


    【解决方案1】:

    如果在list 而不是dict 上调用了.pop(),则会发生此python(非ES)错误。我还没有看到你的 json 文件,但这是你的 linted 代码,它工作得很好:

    from elasticsearch import Elasticsearch, helpers
    import sys
    import json
    import os
    
    es = Elasticsearch("localhost:9200")
    filename = "path_to_file.json"
    
    
    def load_json(filename):
        with open(filename, 'r') as open_file:
            yield json.load(open_file)
    
    
    helpers.bulk(es, load_json(filename), index='cllimaster_test',
                 doc_type='clli_data_test')
    
    print(es.count(index='cllimaster_test'))
    
    

    【讨论】:

    • 谢谢,原来我的 json 正在返回一个列表,通过在 json 对象数组的开头添加键来修复它。我现在有新的读取超时问题,如果无法解决,我可能会发布新问题。请注意:)
    • 我的 json 文件:{rows:[{obj1},{obj2},{obj3}]}。 Kibana 正在考虑将rows 作为字段并将我的对象存储为嵌套字段。这就是为什么我会读取超时,因为它将整个文件视为行中的单个对象。
    • yield json.load(open_file)['rows']那样提取行。
    • 我在 Notepad++ 中编辑过文件,但感谢您的解决方案。
    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 2015-06-01
    • 2015-12-18
    • 2022-08-12
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2016-08-19
    相关资源
    最近更新 更多