如何将已创建的json格式字符串插入Elasticsearch Bulk

时间:2017-08-18 02:27:02

标签: python elasticsearch elasticsearch-bulk-api elasticsearch-bulk

在python脚本中,

我正在尝试 elasticsearch.helpers.bulk 来存储多条记录。

我将从另一个软件中获取一个json格式的字符串,我想将它附加在源代码中

我通过此answer

获得了helpers.bulk格式

我的部分代码:

def saveES(output,name):
    es = Elasticsearch([{'host':'localhost','port':9200}]) 
    output = output.split('\n')
    i=0
    datas=[]
    while i<len(output):
            data = {
                    "_index":"name",
                    "_type":"typed",
                    "_id":saveES.counter,
                    "_source":[[PROBLEM]]
            }
            i+=1
            saveES.counter+=1
            datas.append(data)

    helpers.bulk(es, datas)

我想在[[问题]]

中附加一个json格式的字符串

如何将其附加?我努力了,但输出不正确..

如果我使用:

"_source":{
"image_name":'"'+name+'",'+output[i]
}

并打印数据结果为:

{'_type': 'typed', '_id': 0, '_source': {'image_name': '"nginx","features": "os,disk,package", "emit_shortname": "f0b03efe94ec", "timestamp": "2017-08-18T17:25:46+0900", "docker_image_tag": "latest"'}, '_index': 'name'}

此结果显示合并为单个字符串。

但我希望:

{'_type': 'typed', '_id': 0, '_source': {'image_name': 'nginx','features': 'os,disk,package', 'emit_shortname': 'f0b03efe94ec', 'timestamp': '2017-08-18T17:25:46+0900', 'docker_image_tag': 'latest'}, '_index': 'name'}

1 个答案:

答案 0 :(得分:0)

您的代码中存在许多问题。

  1. 您覆盖循环中es = Elasticsearch([{'host':'localhost','port':9200}]) # You don't have to initialise this variable every time you are calling the function but only once. def save_es(output,es): # Peps8 convention output = output.split('\n') # you don't need a while loop. A comprehension loop will avoid a lot of trouble data = [ # Please without s in data { "_index": "name", "_type": "typed", "_id": index, "_source": { "image_name":"name" + name} } for index, name in enumerate(output) ] helpers.bulk(es, data) save_es(output, es) 的值
  2. 你不尊重任何规范(Pesp8和东西)
  3. 你现在而不是理解列表
  4. 您创建了2个无用的变量
  5. 您在函数中实例化了
  6. 这是您改进的代码

    {{1}}

    希望这有帮助。