打赏

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笔记三之版本控制和插件

 

posted @ 2017-07-01 12:14  大数据和AI躺过的坑  阅读(4825)  评论(0编辑  收藏  举报