10.bulk操作
文章目錄
- 1. Bulk API 簡(jiǎn)介
- 2. 樂(lè)觀鎖使用
- 3. version
- 4. Routing
- 5. Waiting For Active Shards
- 6. Refresh
- 7. Update
1. Bulk API 簡(jiǎn)介
Bulk API,能夠在一個(gè)單一的API調(diào)用執(zhí)行多項(xiàng)index/delete操作。這可以大大提高索引速度。
該 REST API 是 /_bulk,它遵循多行JSON結(jié)構(gòu):
action_and_meta_data\n optional_source\n action_and_meta_data\n optional_source\n .... action_and_meta_data\n optional_source\n注意:數(shù)據(jù)的最終行必須以換行符結(jié)束\n。
可能的操作有 index,create,delete和 update 。 index 和 create必須在下一行緊跟著doc的source。并且create與index API中的op_type=create 有相同的語(yǔ)義(create在doc已經(jīng)存在的情況下會(huì)失敗,index是直接覆蓋)。delete不會(huì)使用緊接著的下一行的內(nèi)容,并與 delete API 中具有相同的語(yǔ)義。update 需要下一行內(nèi)容來(lái)識(shí)別partial doc,script 或者upsert 。
如果提供的文本文件輸入到 curl,必須使用 --data-binary的標(biāo)志,而不是 -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}}]}因?yàn)檫@種格式使用 \n 作為分隔符,請(qǐng)確保 JSON action 和 source 沒(méi)有被打印。這里是一個(gè)正確的 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"} }返回的結(jié)果是
{"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,當(dāng)在uri中指定了index,那么在請(qǐng)求體中沒(méi)有顯式指定index的都會(huì)使用這個(gè)index。
隊(duì)請(qǐng)求體格式的一些解釋:
{ "index" : { "_index" : "test", "_id" : "1" } } { "field1" : "value1" }為什么要采用這種請(qǐng)求格式呢,一行是index和id,一行是具體數(shù)據(jù),在實(shí)際過(guò)程中我們使用bulk write最多,因?yàn)槲覀儼l(fā)送bulk請(qǐng)求是到一個(gè)代理節(jié)點(diǎn)coordinate node,所以coordiante還要根據(jù)index-name和route規(guī)則進(jìn)行轉(zhuǎn)發(fā),coordinate并不需要解析下面一行doc的內(nèi)容,所以在coordinate node端只會(huì)parse 第一行的action_meta_data,這樣的處理就會(huì)更加高效。
使用此協(xié)議的客戶端庫(kù)應(yīng)嘗試并努力在客戶端執(zhí)行類似的操作,并盡可能減少緩存使用。
針對(duì) bulk action 的響應(yīng)是一個(gè)大的 Json 結(jié)構(gòu),包含著每個(gè) action 執(zhí)行后的結(jié)果。單一的 action 故障不會(huì)影響其余的操作。
在一個(gè)單獨(dú)的 bulk call 中沒(méi)有絕對(duì)正確的action數(shù)目。你應(yīng)該通過(guò)實(shí)驗(yàn)不同的設(shè)置來(lái)確定適合您的特定工作負(fù)載的最佳大小。
如果使用 HTTP API,請(qǐng)確保客戶端不發(fā)送 HTTP chunk,因?yàn)檫@會(huì)使得運(yùn)行速度降低。
2. 樂(lè)觀鎖使用
索引操作可以是有條件來(lái)控制的,可以控制僅在文檔的最后一次因?yàn)樾薷姆峙涞腳seq_no和_primary_term 參數(shù)和請(qǐng)求時(shí)傳進(jìn)來(lái)的if_seq_no和if_primary_term參數(shù)相等的情況下才能執(zhí)行操作。如果檢測(cè)到不匹配,則該操作將導(dǎo)致VersionConflictException和狀態(tài)碼409。有關(guān)更多詳細(xì)信息,請(qǐng)參見(jiàn)樂(lè)觀并發(fā)控制。
3. version
每個(gè) bulk 對(duì)象可以使用 version字段來(lái)指定版本。和index / delete 的version表現(xiàn)一樣。同時(shí)還支持version_type
4. Routing
每個(gè) bulk 對(duì)象使用 routing字段 來(lái)顯示指定 routing 值。和index / delete 的routing表現(xiàn)一樣。
5. Waiting For Active Shards
當(dāng)創(chuàng)建 bulk call 時(shí),您可以設(shè)置 wait_for_active_shards 參數(shù),在開(kāi)始 bulk 請(qǐng)求之前要求活躍分片副本的最低數(shù)量。請(qǐng)參見(jiàn)這里進(jìn)一步的詳細(xì)信息和使用示例。
6. Refresh
控制當(dāng)通過(guò)請(qǐng)求所做的更改是可見(jiàn)的。只有對(duì)應(yīng)搜到請(qǐng)求的shard會(huì)refresh
7. Update
當(dāng)使用 update操作,_retry_on_conflict可以用在每個(gè)action 當(dāng)紅,指定了在版本沖突的情況下可以進(jìn)行多少次被重試。
update action 的操作支持下列選項(xiàng):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}總結(jié)
- 上一篇: 09.multi-get api操作
- 下一篇: 11.reindex操作