Elasticsearch-Bulk 批量 API
# 前言
前面已经学会了ES的CURD操作,接下来需要批量插入一些数据,来进行复杂一点的操作。
# 语法格式
POST customer/external/_bulk
{"action":{metadata}}
{request body}
{"action":{metadata}}
{request body}
1
2
3
4
5
6
2
3
4
5
6
# 简单实例
- 请求路径 POST请求
http://192.168.56.10:9200/customer/external/_bulk
1
- 请求体
{"index":{"_id":"1"}}
{"name":"ZhangSan"}
{"index":{"_id":"2"}}
{"name":"LiSi"}
1
2
3
4
2
3
4
- 请求体是TEXT,返回Status:406 Acceptable
{
"error": "Content-Type header [text/plain] is not supported",
"status": 406
}
1
2
3
4
2
3
4
- 请求体是JSON,返回Status:400 Bad Request
{
"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
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
发现无论请求体是TEXT还是JSON都无法成功,这时候,移步到Kibana中操作。
# 在Kibana发请求
去到Kibana中,找到Dev Tools
键入,点击运行
POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"ZhangSan"}
{"index":{"_id":"2"}}
{"name":"LiSi"}
1
2
3
4
5
2
3
4
5
- 返回
#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
"took" : 250, //花费时间/毫秒
"errors" : false, //没有发生报错
"items" : [ //每个数据都独立返回结果
{
"index" : { //index代表索引,保存
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "customer",
"_type" : "external",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
}
]
}
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
41
42
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
41
42
每一条记录都是独立的操作,上一条记录和下一条记录操作是不会相互影响的。
# 复杂实例
POST /_bulk
{"delete":{"_index":"website","_type":"blog","_id":"12"}}
{"create":{"_index":"website","_type":"blog","_id":"123"}}
{"title":"My First blog post"}
{"index":{"_index":"website","_type":"blog"}}
{"title":"My Second blog post"}
{"update":{"_index":"website","_type":"blog","_id":"123"}}
{"doc":{"tilte":"My Updated blog post"}}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
如上,有增删改查操作
- 返回
#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
"took" : 515,
"errors" : false,
"items" : [
{
"delete" : {
"_index" : "website",
"_type" : "blog",
"_id" : "12",
"_version" : 1,
"result" : "not_found",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 404
}
},
{
"create" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "website",
"_type" : "blog",
"_id" : "x27f-3UBveHmpGGJVTVA",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 201
}
},
{
"update" : {
"_index" : "website",
"_type" : "blog",
"_id" : "123",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1,
"status" : 200
}
}
]
}
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 插入样本数据
ES官方提供了一些版本数据,点击连接 (opens new window)
POST bank/account/_bulk
1
# 查看数据
插入成功后,现现在查看一下所有索引
GET http://192.168.56.10:9200/_cat/indices
1
返回
yellow open bank HWCvZjbYTR-4ZyOFsV7b8g 1 1 1000 0 428.5kb 428.5kb
yellow open website UHI0y9NBTJGx6VJP0E3aiA 1 1 2 2 9.2kb 9.2kb
green open .kibana_task_manager_1 B-MgVOetQy6RI_74OnemMA 1 0 2 0 49.2kb 49.2kb
green open .apm-agent-configuration x2dKfM9rSxOywtQ3VwUK5Q 1 0 0 0 283b 283b
green open .kibana_1 t62lfnEuR9euTWaMBQnx0g 1 0 9 0 32.7kb 32.7kb
yellow open customer ED1DrmWASWmeYZi_6Y1N-A 1 1 2 0 3.5kb 3.5kb
1
2
3
4
5
6
2
3
4
5
6
可以看到,bank索引下,有1000条数据,占用428.5kb
帮我改善此页面 (opens new window)
上次更新: 2020/12/17, 08:38:35