Elasticsearch之CURL命令的version控制


 

 

  普通關系型數據庫使用的是(悲觀並發控制(PCC))

    當我們在修改一個數據前先鎖定這一行,然后確保只有讀取到數據的這個線程可以修改這一行數據

  

  ES使用的是(樂觀並發控制(OCC))  

    ES不會阻止某一數據的訪問,然而,如果基礎數據在我們讀取和寫入的間隔中發生了變化,更新就會失敗,這時候就由程序來決定如何處理這個沖突。它可以重新讀取新數據來進行更新,又或者將這一情況直接反饋給用戶。

  ES如何實現版本控制(使用es內部版本號)

    1)首先得到想要修改的文檔,獲取版本version號

curl -XGET http://master:9200/zhouls/user/2

[hadoop@master elasticsearch-2.4.0]$ curl -XGET http://master:9200/zhouls/user/2
{"_index":"zhouls","_type":"user","_id":"2","_version":1,"found":true,"_source":{"name" : "john"  , "age" : 28}}[hadoop@master elasticsearch-2.4.0]$ 
[hadoop@master elasticsearch-2.4.0]$ 
[hadoop@master elasticsearch-2.4.0]$ 

 

 

 

 

 

 

 

 

 

 

    2)在執行更新操作的時候把版本號傳過去

curl -XPUT http://master:9200/zhouls/user/2?version=1  -d  '{"name" : "john1" , "age" : 29}'

 

[hadoop@master elasticsearch-2.4.0]$ curl -XPUT http://master:9200/zhouls/user/2?version=1  -d  '{"name" : "john1" , "age" : 29}'
{"_index":"zhouls","_type":"user","_id":"2","_version":2,"_shards":{"total":2,"successful":2,"failed":0},"created":false}[hadoop@master elasticsearch-2.4.0]$ 

 

 

 

 

 

 

 

 

 

 

 

 

curl -XPOST http://master:9200/zhouls/user/2/_update?version=2  -d  '{"doc" :{"age" : 30}}'

 

[hadoop@master elasticsearch-2.4.0]$ curl -XPOST http://master:9200/zhouls/user/2/_update?version=2  -d  '{"doc" :{"age" : 30}}'
{"_index":"zhouls","_type":"user","_id":"2","_version":3,"_shards":{"total":2,"successful":2,"failed":0}}[hadoop@master elasticsearch-2.4.0]$ 
[hadoop@master elasticsearch-2.4.0]$ 

 

 

 

 

 

 

 

 

 

 

 

   

 

 

 

 

   3)如果傳遞的版本號和待更新的文檔的版本號不一致,則會更新失敗。

  現在,待更新文檔的版本號是3。我分別傳1和穿4進入,都會報更新失敗。如下

[hadoop@master elasticsearch-2.4.0]$ curl -XPOST http://master:9200/zhouls/user/2/_update?version=2  -d  '{"doc" :{"age" : 30}}'
{"_index":"zhouls","_type":"user","_id":"2","_version":3,"_shards":{"total":2,"successful":2,"failed":0}}[hadoop@master elasticsearch-2.4.0]$ 
[hadoop@master elasticsearch-2.4.0]$ 
[hadoop@master elasticsearch-2.4.0]$ curl -XPOST http://master:9200/zhouls/user/2/_update?version=1  -d  '{"doc" :{"age" : 30}}'
{"error":{"root_cause":[{"type":"version_conflict_engine_exception","reason":"[user][2]: version conflict, current [3], provided [1]","index":"zhouls","shard":"2"}],"type":"version_conflict_engine_exception","reason":"[user][2]: version conflict, current [3], provided [1]","index":"zhouls","shard":"2"},"status":409}[hadoop@master elasticsearch-2.4.0]$ 
[hadoop@master elasticsearch-2.4.0]$ 
[hadoop@master elasticsearch-2.4.0]$ curl -XPOST http://master:9200/zhouls/user/2/_update?version=4  -d  '{"doc" :{"age" : 30}}'
{"error":{"root_cause":[{"type":"version_conflict_engine_exception","reason":"[user][2]: version conflict, current [3], provided [4]","index":"zhouls","shard":"2"}],"type":"version_conflict_engine_exception","reason":"[user][2]: version conflict, current [3], provided [4]","index":"zhouls","shard":"2"},"status":409}[hadoop@master elasticsearch-2.4.0]$ 
[hadoop@master elasticsearch-2.4.0]$ 
[hadoop@master elasticsearch-2.4.0]$ 

 

 

 

 

 

  更多,請見

Elasticsearch筆記三之版本控制和插件

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM