前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch 入门: _bulk 批量导入数据

Elasticsearch 入门: _bulk 批量导入数据

作者头像
王兵
发布2018-06-14 16:21:46
2.2K0
发布2018-06-14 16:21:46
举报
文章被收录于专栏:程序员笔记程序员笔记
批量导入数据

使用 Elasticsearch Bulk API /_bulk批量 update

步骤:
  1. 需求:我希望批量导入一个 movie type 的名词列表到 wordbank index 索引。
  2. 准备数据: 根据官方文档,Json 数据要准备成这个格式的: action_and_meta_data\n optional_source\n action_and_meta_data\n optional_source\n .... action_and_meta_data\n optional_source\n 其中 action 需要是 index, create, delete and update 中的一个。 接下来准备这样的数据: {"index": {"_index": "wordbank", "_type": "movie", "_id": 1}} {"doc": {"name": "权力的游戏"}} {"index": {"_index": "wordbank", "_type": "movie", "_id": 2}} {"doc": {"name": "熊出没"}}
  3. POST bulk curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' --data-binary @movie_names
  4. 批量 update 成功 {"took":50,"errors":false,"items":[{"index":{"_index":"wordbank","_type":"movie","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}},{"index":{"_index":"wordbank","_type":"movie","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}}]}
遇到过的坑:
  1. illegal_argument_exception: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"}],"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"},"status":400}
    • 原因:批量导入的 json 文件最后必须要以\n结尾,也就是需要一个空行。
    • 解决:在 json 文件末尾加多一个回车。
  2. header 问题: {"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}
    • 原因:Elasticsearch 6.x 之后 curl 的 content-type 更严格了。
    • 解决:在 curl 命令后多加一条 -H 'Content-Type: application/json'
  3. action_request_validation_exception: {"error":{"root_cause":[{"type":"action_request_validation_exception","reason":"Validation Failed: 1: script or doc is missing;2: script or doc is missing;"}],"type":"action_request_validation_exception","reason":"Validation Failed: 1: script or doc is missing;2: script or doc is missing;"},"status":400}
    • 原因:bulk update 时,更新的文本需要放到 "doc" 字典下,另外 update 在这里就只是 update,如果文档不存在会报错。
    • 解决:

    { "field1" : "value1", "field2" : "value2" } --> { "doc" : { "field1" : "value1", "field2" : "value2" } }

  4. 不要直接在 terminal 把 curl 的结果显示出来
    • 原因:因为 curl 返回的结果是个单行 json 当批量处理条目多的时候,这个单行的 json 很长。而且-s 也silent 模式是不会把这个结果去掉的,因为 -s 是 curl 的参数,会屏蔽掉 curl 的 log,但 Elasticsearch 的返回 json 是不会被屏蔽掉的。
    • 解决:把输出结果导到文件 curl -s 'http://example.com' > /dev/null
  5. 据说不要重复指定 index 和 type:来源,可能是我数据量比较小,2w条,差距不大。不过前者确实省文档空间。 推荐使用这种: POST /website/log/_bulk { "index": {}} { "event": "User logged in" } 而不是这种: POST /_bulk { "index": { "_index": "website" , "_type": "blog" , }} { "title": "Overriding the default type" }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.05.10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 批量导入数据
    • 步骤:
      • 遇到过的坑:
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档