10.bulk操作
文章目錄
- 1. Bulk API 簡介
- 2. 樂觀鎖使用
- 3. version
- 4. Routing
- 5. Waiting For Active Shards
- 6. Refresh
- 7. Update
1. Bulk API 簡介
Bulk API,能夠在一個單一的API調用執行多項index/delete操作。這可以大大提高索引速度。
該 REST API 是 /_bulk,它遵循多行JSON結構:
action_and_meta_data\n optional_source\n action_and_meta_data\n optional_source\n .... action_and_meta_data\n optional_source\n注意:數據的最終行必須以換行符結束\n。
可能的操作有 index,create,delete和 update 。 index 和 create必須在下一行緊跟著doc的source。并且create與index API中的op_type=create 有相同的語義(create在doc已經存在的情況下會失敗,index是直接覆蓋)。delete不會使用緊接著的下一行的內容,并與 delete API 中具有相同的語義。update 需要下一行內容來識別partial doc,script 或者upsert 。
如果提供的文本文件輸入到 curl,必須使用 --data-binary的標志,而不是 -d。后者不保留換行符。例:
$ cat requests { "index" : { "_index" : "test", "_id" : "1" } } { "field1" : "value1" } $ curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@requests"; echo {"took":7, "errors": false, "items":[{"index":{"_index":"test","_type":"_doc","_id":"1","_version":1,"result":"created","forced_refresh":false}}]}因為這種格式使用 \n 作為分隔符,請確保 JSON action 和 source 沒有被打印。這里是一個正確的 bulk 命令使用的例子:
POST _bulk { "index" : { "_index" : "test", "_id" : "1" } } { "field1" : "value1" } { "delete" : { "_index" : "test", "_id" : "2" } } { "create" : { "_index" : "test", "_id" : "3" } } { "field1" : "value3" } { "update" : {"_id" : "1", "_index" : "test"} } { "doc" : {"field2" : "value2"} }返回的結果是
{"took": 30,"errors": false,"items": [{"index": {"_index": "test","_type": "_doc","_id": "1","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"status": 201,"_seq_no" : 0,"_primary_term": 1}},{"delete": {"_index": "test","_type": "_doc","_id": "2","_version": 1,"result": "not_found","_shards": {"total": 2,"successful": 1,"failed": 0},"status": 404,"_seq_no" : 1,"_primary_term" : 2}},{"create": {"_index": "test","_type": "_doc","_id": "3","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"status": 201,"_seq_no" : 2,"_primary_term" : 3}},{"update": {"_index": "test","_type": "_doc","_id": "1","_version": 2,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"status": 200,"_seq_no" : 3,"_primary_term" : 4}}] }api的uri可以是/_bulk,/{index}/_bulk,當在uri中指定了index,那么在請求體中沒有顯式指定index的都會使用這個index。
隊請求體格式的一些解釋:
{ "index" : { "_index" : "test", "_id" : "1" } } { "field1" : "value1" }為什么要采用這種請求格式呢,一行是index和id,一行是具體數據,在實際過程中我們使用bulk write最多,因為我們發送bulk請求是到一個代理節點coordinate node,所以coordiante還要根據index-name和route規則進行轉發,coordinate并不需要解析下面一行doc的內容,所以在coordinate node端只會parse 第一行的action_meta_data,這樣的處理就會更加高效。
使用此協議的客戶端庫應嘗試并努力在客戶端執行類似的操作,并盡可能減少緩存使用。
針對 bulk action 的響應是一個大的 Json 結構,包含著每個 action 執行后的結果。單一的 action 故障不會影響其余的操作。
在一個單獨的 bulk call 中沒有絕對正確的action數目。你應該通過實驗不同的設置來確定適合您的特定工作負載的最佳大小。
如果使用 HTTP API,請確保客戶端不發送 HTTP chunk,因為這會使得運行速度降低。
2. 樂觀鎖使用
索引操作可以是有條件來控制的,可以控制僅在文檔的最后一次因為修改分配的_seq_no和_primary_term 參數和請求時傳進來的if_seq_no和if_primary_term參數相等的情況下才能執行操作。如果檢測到不匹配,則該操作將導致VersionConflictException和狀態碼409。有關更多詳細信息,請參見樂觀并發控制。
3. version
每個 bulk 對象可以使用 version字段來指定版本。和index / delete 的version表現一樣。同時還支持version_type
4. Routing
每個 bulk 對象使用 routing字段 來顯示指定 routing 值。和index / delete 的routing表現一樣。
5. Waiting For Active Shards
當創建 bulk call 時,您可以設置 wait_for_active_shards 參數,在開始 bulk 請求之前要求活躍分片副本的最低數量。請參見這里進一步的詳細信息和使用示例。
6. Refresh
控制當通過請求所做的更改是可見的。只有對應搜到請求的shard會refresh
7. Update
當使用 update操作,_retry_on_conflict可以用在每個action 當紅,指定了在版本沖突的情況下可以進行多少次被重試。
update action 的操作支持下列選項:doc(部分文檔)upsert,doc_as_upsert,script,params(腳本), lang(腳本)和_source。
POST _bulk { "update" : {"_id" : "1", "_index" : "index1", "retry_on_conflict" : 3} } { "doc" : {"field" : "value"} } { "update" : { "_id" : "0", "_index" : "index1", "retry_on_conflict" : 3} } { "script" : { "source": "ctx._source.counter += params.param1", "lang" : "painless", "params" : {"param1" : 1}}, "upsert" : {"counter" : 1}} { "update" : {"_id" : "2", "_index" : "index1", "retry_on_conflict" : 3} } { "doc" : {"field" : "value"}, "doc_as_upsert" : true } { "update" : {"_id" : "3", "_index" : "index1", "_source" : true} } { "doc" : {"field" : "value"} } { "update" : {"_id" : "4", "_index" : "index1"} } { "doc" : {"field" : "value"}, "_source": true}總結
- 上一篇: 09.multi-get api操作
- 下一篇: 11.reindex操作