bulk批量增删改

1.bulk语法格式:
 注意: bulk api对json的语法,有严格的要求,每个json串不能换行,只能放在一行,多个类型的json串之间,必须换行

POST /_bulk   
{"delete":{"索引名称":"索引值","类型名称":"类型值","id名称":id值}}
{"create":{"索引名称":"索引值","类型名称":"类型值","id名称":id值}}
{实际数据存放的地方}
{"index":{"索引名称":"索引值","类型名称":"类型值","id名称":id值}}
{实际数据存放的地方}
{"update":{"索引名称":"索引值","类型名称":"类型值","id名称":id值}}
{"doc":{实际数据存放的地方}}

2.可操作的类型:
  2.1:delete:删除一个文档,只要一个json串就行了
  2.2:create: PUT/index/type/id,强制创建一个文档
  2.3:index:普通的PUT操作,可以是新建文档,也可以是全量替换文档
  2.4:update:执行partial update操作
3.代码:

POST /_bulk
{"delete":{"_index":"test_index","_type":"test_type","_id":3}}     删除id为3的数据
{"create":{"_index":"test_index","_type":"test_type","_id":20}}  创建一个id为20的文档
{"test_field":"test20"}   往文档里面加入数据(test20)
{"index":{"_index":"test_index","_type":"test_type","_id":10}}  全量替换 id为10的文档
{"test_field":"test10"}    替换文档内的全部数据
{"update":{"_index":"test_index","_type":"test_type","_id":1}}  修改id为1的文档
{"doc":{"test_field2":"partial update test1"}}   更新数据

注意:bulk操作中,任意一个操作失败,是不会影响其他的操作的,但是在返回结果里,会有一个异常日志

4.运行结果

{
  "took" : 333,
  "errors" : false,
  "items" : [
    {
      "delete" : {             删除
        "_index" : "test_index",     索引
        "_type" : "test_type",     类型
        "_id" : "3",          id
        "_version" : 4,         版本
        "result" : "deleted",     删除结果 
        "_shards" : {
          "total" : 2,
          "successful" : 1,	    删除成功
          "failed" : 0
        },
        "_seq_no" : 8,
        "_primary_term" : 3,
        "status" : 200
      }
    },
    {
      "create" : {      创建
        "_index" : "test_index", 索引
        "_type" : "test_type",  类型
        "_id" : "20",      id
        "_version" : 1,     版本
        "result" : "created",  创建结果
        "_shards" : {
          "total" : 2,
          "successful" : 1,   创建成功
          "failed" : 0
        },
        "_seq_no" : 7,
        "_primary_term" : 3,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "test_index",
        "_type" : "test_type",
        "_id" : "10",
        "_version" : 6,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 14,
        "_primary_term" : 3,
        "status" : 200
      }
    },
    {
      "update" : {
        "_index" : "test_index",
        "_type" : "test_type",
        "_id" : "1",
        "_version" : 3,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 5,
        "_primary_term" : 3,
        "status" : 200
      }
    }
  ]
}

图解:
在这里插入图片描述
验证:(这里使用批量查询验证)
 代码:

GET /test_index/test_type/_mget
{
  "ids":[3,20,10,1]
}
验证结果
{
  "docs" : [
    {
      "_index" : "test_index",
      "_type" : "test_type",
      "_id" : "3",
      "found" : false   已删除,显示false 表示不存在
    },
    {
      "_index" : "test_index",
      "_type" : "test_type",
      "_id" : "20",
      "_version" : 1,
      "found" : true,  创建成功
      "_source" : {
        "test_field" : "test20"   数据
      }
    },
    {
      "_index" : "test_index",
      "_type" : "test_type",
      "_id" : "10",
      "_version" : 6,    首先看版本号,已经不一样了,表示全量覆盖了
      "found" : true,    
      "_source" : {
        "test_field" : "test10"  数据已经改变了
      }
    },
    {
      "_index" : "test_index",
      "_type" : "test_type",
      "_id" : "1",
      "_version" : 3,
      "found" : true,
      "_source" : {
        "test_field1" : "test field1",
        "test_field2" : "partial update test1"       更新的数据
      }
    }
  ]
}

图解:
在这里插入图片描述
如果操作的是同一index和同一索引里面的数据

格式:
POST/index/type/_bulk
{"delete":{"id名称":id值}}
{"create":{"id名称":id值}}
{实际数据存放的地方}
{"index":{"id名称":id值}}
{实际数据存放的地方}
{"update":{"id名称":id值}}
{"doc":{实际数据存放的地方}} 

代码:
POST /test_index/test_type/_bulk
{"delete":{""_id":3}}
{"create":{"_id":20}}
{"test_field":"test20"}
{"index":{"_id":10}}
{"test_field":"test10"}
{"update":{"_id":1}}
{"doc":{"test_field2":"partial update test1"}}    也可以这样写

bulk size最佳的大小:
  bulk request会加载到内存里,如果太大的话,性能反而会下降,因此需要反复尝试一个最佳的bulk seize。
一般从10005000条数据开始,尝试逐条增加,大小最好在515MB之间

猜你喜欢

转载自blog.csdn.net/x15011238662/article/details/84835105
今日推荐