• 26.bulk批量操作


    主要知识点

    1bulk语法

    2、bulk使用时的注意事项

    3、bulk size 对es性能的影响

       

    一、bulk语法

    每一个操作要两个json串(delete操作除外),每个json串占一行不能换行,语法如下:

    {"action": {"metadata"}}

    {"data"}

    具体写法如下:

    {"index": {"_index": "test_index", "_type", "test_type", "_id": "1"}}

    {"test_field1": "test1", "test_field2": "test2"}

       

    、用法举例

    POST /_bulk

    {"delete":{"_index":"test_index","_type":"test_type","_id":10}}

    {"create":{"_index":"test_index","_type":"test_type","_id":12}}

    {"test_field": "test field 12"}

    {"create":{"_index":"test_index","_type":"test_type","_id":2}}

    {"test_field": "recreate test field 2"}

    {"index":{"_index":"test_index","_type":"test_type","_id":2,"_retry_on_conflict":3}}

    {"test_field2": "reindex test field2"}

    {"update": {"_index":"test_index","_type":"test_type","_id":1}}

    {"doc":{"test_field1": "partial update test field2"}}

    执行结果如下:

    {

    "took": 189,

    "errors": true,

    "items": [

    {

    "delete": {

    "found": true,

    "_index": "test_index",

    "_type": "test_type",

    "_id": "10",

    "_version": 2,

    "result": "deleted",

    "_shards": {

    "total": 2,

    "successful": 1,

    "failed": 0

    },

    "status": 200

    }

    },

    {

    "create": {

    "_index": "test_index",

    "_type": "test_type",

    "_id": "14",

    "_version": 1,

    "result": "created",

    "_shards": {

    "total": 2,

    "successful": 1,

    "failed": 0

    },

    "created": true,

    "status": 201

    }

    },

    {

    "create": {

    "_index": "test_index",

    "_type": "test_type",

    "_id": "2",

    "status": 409,

    "error": {

    "type": "version_conflict_engine_exception",

    "reason": "[test_type][2]: version conflict, document already exists (current version [5])",

    "index_uuid": "d5YEp9EjTKevAC315oXfwA",

    "shard": "2",

    "index": "test_index"

    }

    }

    },

    {

    "index": {

    "_index": "test_index",

    "_type": "test_type",

    "_id": "2",

    "_version": 6,

    "result": "updated",

    "_shards": {

    "total": 2,

    "successful": 1,

    "failed": 0

    },

    "created": false,

    "status": 200

    }

    },

    {

    "update": {

    "_index": "test_index",

    "_type": "test_type",

    "_id": "1",

    "_version": 3,

    "result": "noop",

    "_shards": {

    "total": 2,

    "successful": 1,

    "failed": 0

    },

    "status": 200

    }

    }

    ]

    }

    可以看出,第一个deleter操作成功,第二个create操作成功,第三个create操作不成功(因为原_id=2document已存在)第四个index操作成功,做全量替换,第五个update操作成功,

       

    三、bulk可以执行的操作类型

    1delete:删除一个文档,只要1json串就可以了

    2createPUT /index/type/id/_create,强制创建

    3index:普通的put操作,可以是创建文档,也可以是全量替换文档

    4update:执行的partial update操作

       

    四、其他写法

       

    1)当所有的操作都作用于同一个index时:

    POST /test_index/_bulk

    { "delete": { "_type": "test_type", "_id": "3" }}

    { "create": { "_type": "test_type", "_id": "12" }}

    { "test_field": "test12" }

    { "index": { "_type": "test_type" }}

    { "test_field": "auto-generate id test" }

    { "index": { "_type": "test_type", "_id": "2" }}

    { "test_field": "replaced test2" }

    { "update": { "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }

    { "doc" : {"test_field2" : "bulk test1"} }

       

    2)当所有的操作都作用于同一个index下的同一个type时:

    POST /test_index/test_type/_bulk

    { "delete": { "_id": "3" }}

    { "create": { "_id": "12" }}

    { "test_field": "test12" }

    { "index": { }}

    { "test_field": "auto-generate id test" }

    { "index": { "_id": "2" }}

    { "test_field": "replaced test2" }

    { "update": { "_id": "1", "_retry_on_conflict" : 3} }

    { "doc" : {"test_field2" : "bulk test1"} }

       

    五、bulk size对性能的影响

    bulk request会加载到内存里,如果太大的话,性能反而会下降,因此需要反复尝试一个最佳的bulk size。一般从1000~5000条数据开始,尝试逐渐增加。另外,如果看大小的话,最好是在5~15MB之间。

       

    六、 bulk语法的注意事项

    1bulk apijson的语法有严格的要求,每个json串不能换行,只能放一行,同时一个json串和一个json串之间,必须有一个换行。

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

       

       

  • 相关阅读:
    微信Webapp开发的各种变态路由需求及解决办法!
    【Spring Security】七、RememberMe配置
    【Spring Security】七、RememberMe配置
    【Spring Security】七、RememberMe配置
    【Spring Security】七、RememberMe配置
    redis(6)lua脚本
    VS自带WCF测试客户端简单介绍
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/liuqianli/p/8463989.html
Copyright © 2020-2023  润新知