Saul's blog Saul's blog
首页
后端
分布式
前端
更多
分类
标签
归档
友情链接
关于
GitHub (opens new window)

Saul.J.Wu

立身之本,不在高低。
首页
后端
分布式
前端
更多
分类
标签
归档
友情链接
关于
GitHub (opens new window)
  • Java入门基础

  • Java核心基础

  • 设计模式

  • Web开发

  • SpringBoot

  • 微服务

  • Elasticsearch

    • ElasticSearch-简介
    • Elasticsearch-安装
    • Elasticsearch-_cat
    • Elasticsearch-新增数据
    • Elasticsearch-查询数据&乐观锁
    • Elasticsearch-更新文档
    • Elasticsearch-删除操作
    • Elasticsearch-Bulk 批量 API
      • 前言
      • 语法格式
      • 简单实例
      • 在Kibana发请求
      • 复杂实例
      • 插入样本数据
      • 查看数据
    • Elasticsearch-Search API
    • Elasticsearch-Query DSL
    • Elasticsearch-aggregations聚合分析
    • Elasticsearch-mapping映射
    • Elasticsearch-分词
    • SpringBoot整合Elasticsearch Rest Client
    • 商品上架与ES
  • 运维

  • 后端
  • Elasticsearch
SaulJWu
2020-11-24

Elasticsearch-Bulk 批量 API

# 前言

前面已经学会了ES的CURD操作,接下来需要批量插入一些数据,来进行复杂一点的操作。

# 语法格式

POST customer/external/_bulk

{"action":{metadata}}
{request body}
{"action":{metadata}}
{request body}
1
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
  • 请求体是TEXT,返回Status:406 Acceptable
{
    "error": "Content-Type header [text/plain] is not supported",
    "status": 406
}
1
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

发现无论请求体是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
  • 返回
#! 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

每一条记录都是独立的操作,上一条记录和下一条记录操作是不会相互影响的。

# 复杂实例

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

如上,有增删改查操作

  • 返回
#! 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

# 插入样本数据

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

可以看到,bank索引下,有1000条数据,占用428.5kb

帮我改善此页面 (opens new window)
#Elasticsearch#Bulk#批量操作#API
上次更新: 2020/12/17, 08:38:35
Elasticsearch-删除操作
Elasticsearch-Search API

← Elasticsearch-删除操作 Elasticsearch-Search API→

最近更新
01
zabbix学习笔记二
02-28
02
zabbix学习笔记一
02-10
03
Linux访问不了github
12-08
更多文章>
Theme by Vdoing | Copyright © 2020-2022 Saul.J.Wu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式