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]$
更多,请见
作者:大数据和人工智能躺过的坑
出处:http://www.cnblogs.com/zlslch/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏我一杯咖啡【物质支持】,也可以点击右下角的【好文要顶】按钮【精神支持】,因为这两种支持都是我继续写作,分享的最大动力!