ElasticSearch _bulk批量插入报错

一、现象

今天在 ElasticSearch 批量插入时:

POST /customer/external/_bulk
{
  "index":{"_id":"1"}
}
{
  "name":"John"
}
{
  "index":{"_id":"2"}
}
{
  "name":"tom"
}

出现了这样的错误:

{
  "error": {
    "root_cause": [
      {
        "type": "json_e_o_f_exception",
        "reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@2eeea373; line: 1, column: 1])
 at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@2eeea373; line: 1, column: 3]"
      }
    ],
    "type": "json_e_o_f_exception",
    "reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@2eeea373; line: 1, column: 1])
 at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@2eeea373; line: 1, column: 3]"
  },
  "status": 500
}

二、原因

bulk api对json语法有严格的要求,每个json串不能换行,只能放到一行,同时一个json串和一个json串之间必须要换行,否则会出现如上错误

三、解决

POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"John"}
{"index":{"_id":"2"}}
{"name":"tom"}

插入成功:

原文地址:https://www.cnblogs.com/houchen/p/14152556.html