在elasticsearch中插入多个文档

在elasticsearch中插入多个文档,第1张

在elasticsearch中插入多个文档

好的,那么您可以使用简单的Shell脚本来完成一些非常简单的 *** 作(请参见下文)。这个想法是不必手动编辑文件,而是让Python进行编辑并创建另一个文件格式符合

_bulk
端点期望的文件。它执行以下 *** 作:

  1. 首先,我们声明一个小的Python脚本,该脚本读取您的JSON文件并创建一个具有所需文件格式的新脚本以发送到
    _bulk
    端点。
  2. 然后,我们运行该Python脚本并存储批量文件
  3. 最后,我们
    _bulk
    使用简单的curl命令将在第2步中创建的文件发送到端点
  4. 到这里,您现在有了一个包含文档的新ES索引

bulk.sh:

#!/bin/sh# 0. Some constants to re-define to match your environmentES_HOST=localhost:9200JSON_FILE_IN=/path/to/your/file.jsonJSON_FILE_OUT=/path/to/your/bulk.json# 1. Python pre to transform your JSON filePYTHON="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('%sn' % json.dumps({'index': {}}));        out.write('%sn' % json.dumps(doc, indent=0).replace('n', ''));"# 2. run the Python script from step 1python -c "$PYTHON"# 3. use the output file from step 2 in the curl commandcurl -s -XPOST $ES_HOST/index/type/_bulk --data-binary @$JSON_FILE_OUT

你需要:

  1. 将以上脚本保存在
    bulk.sh
    文件中并对其进行chmod处理(即
    chmod u+x bulk.sh
  2. 修改ordre顶部(步骤0)的三个变量以匹配您的环境
  3. 使用运行它
    ./bulk.sh


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5001320.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存