我在 Kibana 的开发工具页面中使用 Query DSL 给索引批量添加数据时,出现了 json_e_o_f_exception 异常。

重现步骤如下:

创建索引 DSL:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
PUT user_info_v202210120517219 
{
"settings": {
"refresh_interval": "1s",
"number_of_replicas": 1,
"number_of_shards": 5
},
"mappings": {
"properties": {
"user_id": {
"type": "integer"
},
"phone": {
"type": "keyword"
},
"create_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"user_type_id": {
"type": "integer"
},
"user_type_name": {
"type": "keyword"
}
}
}
}

批量添加数据 DSL:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
PUT user_info_v202210120517219/_bulk
{
"index": {"_id": "2022001"}
}
{
"user_id": 2022001,
"phone": "17612345678",
"create_time": "2022-08-12 22:54:09",
"user_type_id": 2,
"user_type_name": "SVIP"
}
{
"index": {"_id": "2022002"}
}
{
"user_id": 2022002,
"phone": "17712345678",
"create_time": "2022-08-13 09:14:10",
"user_type_id": 1,
"user_type_name": "VIP"
}

但结果返回了 500 错误,具体返回内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"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@2501da6e; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@2501da6e; line: 2, 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@2501da6e; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@2501da6e; line: 2, column: 3]"
},
"status": 500
}

批量添加数据 DSL 有什么问题吗?


批量添加数据 DSL 里数据格式有问题, _bulk 生成文档需要 JSON 格式,并且不能有换行符。改成如下就可以了:

1
2
3
4
5
PUT user_info_v202210120517219/_bulk
{"index": {"_id": "2022001"}}
{"user_id":2022001,"phone":"17612345678","create_time":"2022-08-12 22:54:09","user_type_id":2,"user_type_name":"SVIP"}
{"index": {"_id": "2022002"}}
{"user_id":2022002,"phone":"17712345678","create_time":"2022-08-13 09:14:10","user_type_id":1,"user_type_name":"VIP"}

返回结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
"took" : 3,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "user_info_v202210120517219",
"_type" : "_doc",
"_id" : "2022001",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "user_info_v202210120517219",
"_type" : "_doc",
"_id" : "2022002",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
}
]
}

(END)