一、現象
今天在 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])\n 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])\n 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"}
插入成功: