1. 程式人生 > >26.bulk批量操作

26.bulk批量操作

不成功 san ont class req 使用 mono _id ret

主要知識點

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會告訴異常日誌

26.bulk批量操作