日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

10.bulk操作

發布時間:2024/2/28 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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}

總結

以上是生活随笔為你收集整理的10.bulk操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。