在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'}
答案 0 :(得分:0)
您的代码中存在许多问题。
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)
的值这是您改进的代码
{{1}}
希望这有帮助。