For*_*hit 4 json elasticsearch
我必须在弹性中插入一个json数组.链接中接受的答案建议在每个json条目之前插入标题行.答案是2年,市场上有更好的解决方案吗?我需要手动编辑我的json文件吗?
有没有办法在elasticsearch服务器中导入json文件(包含100个文档).
[
{
"id":9,
"status":"This is cool."
},
...
]
Run Code Online (Sandbox Code Playgroud)
好的,那么使用简单的shell脚本就可以做到非常简单(见下文).我们的想法是不必手动编辑您的文件,但让Python执行此操作并创建另一个文件,其格式符合_bulk端点所需的格式.它执行以下操作:
_bulk端点._bulk使用简单的curl命令将步骤2中创建的文件发送到端点bulk.sh:
#!/bin/sh
# 0. Some constants to re-define to match your environment
ES_HOST=localhost:9200
JSON_FILE_IN=/path/to/your/file.json
JSON_FILE_OUT=/path/to/your/bulk.json
# 1. Python code to transform your JSON file
PYTHON="import json,sys;
out = open('$JSON_FILE_OUT', 'w');
with open('$JSON_FILE_IN') as json_in:
docs = json.loads(json_in.read());
for doc in docs:
out.write('%s\n' % json.dumps({'index': {}}));
out.write('%s\n' % json.dumps(doc, indent=0).replace('\n', ''));
"
# 2. run the Python script from step 1
python -c "$PYTHON"
# 3. use the output file from step 2 in the curl command
curl -s -XPOST $ES_HOST/index/type/_bulk --data-binary @$JSON_FILE_OUT
Run Code Online (Sandbox Code Playgroud)
你需要:
bulk.sh文件中并chmod它(即chmod u+x bulk.sh)./bulk.sh| 归档时间: |
|
| 查看次数: |
7869 次 |
| 最近记录: |