Es 数组对象更新多进程冲突

本文介绍使用Elasticsearch进行文档更新的两种方法:一种是通过版本控制来实现乐观锁,确保并发环境下数据的一致性;另一种是利用post请求结合脚本的方式进行更新。文章还提供了具体的代码实例。

Es结构

{
  "_index": "my_index",
  "_type": "my_type",
  "_id": "1",
  "_version": 26,
  "found": true,
  "_source": {
    "group": "fans",
    "user": [
      {
        "first": "John",
        "last": "Smith"
      },
      {
        "first": "Alice",
        "last": "White"
      }
    ]
  }
}

 


}


===============================
1、利用version控制==
@Test
public void getOneById() {
Client client= elasticsearchTemplate.getClient();
GetResponse getResponse = elasticsearchTemplate.getClient()
.prepareGet() // 准备进行get操作,此时还有真正地执行get操作。(与直接get的区别)
.setIndex("my_index") // 要查询的
.setType("my_type")
.setId("1")
.get();
long l= getResponse.getVersion();
String sss= getResponse.getSourceAsString();
System.out.println("sss");
EsService esService= new EsService();

String a= getResponse.getSourceAsString();
Supper supper= JSONObject.parseObject(a,Supper.class);
System.out.println(supper.getUser().size());
Use use=new Use();
use.setLast("sbbbbvvs");
use.setFirst("ss");
supper.getUser().add(use);
insertSentimentById("my_index","my_type", supper,"1");
 /* 方法二利用 postscript
HttpEntity entity = new StringEntity(
"{\n" +
" \"script\" : {\n" +
" \"inline\": \"ctx._source.user+=new_tag\",\n" +
" \"params\" : {\n" +
" \"new_tag\" :[{\n" +
" \"first\":\"ddfdd\"\n" +
" ,\"last\":\"22f22\"\n" +
" },\n" +
" {\n" +
" \"first\":\"ddfdf\"\n" +
" ,\"last\":\"2f222\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
esService.putSync("my_index","my_type","1",entity);*/

}





public void insertSentimentById( String indexName,String type, Supper sentiment,String id ) {
//IndexQuery indexQuery = new IndexQueryBuilder().withIndexName(indexName).withType(type).withId(sentiment.getId().toString()).withObject(sentiment).build();
String json= JSONObject.toJSONString(sentiment);
long v=12;
boolean insertFlag= false;
while(true){
try {
elasticsearchTemplate.getClient().prepareIndex(indexName, type).setId(id).setVersion(v).setSource(json).execute().actionGet();
insertFlag=true;
}catch (Exception e){
System.out.println(e.getMessage());;
v=v+1;
}
if(insertFlag){
break;
}
logger.info("===="+v);
}
}
====================================================================================================================================
2、利用post+script

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>5.0.0-rc1</version>
</dependency>
import java.io.IOException;
import java.util.Collections;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.client.RestClient;
import org.springframework.stereotype.Service;

/**
* Created by Administrator on 2018/4/25.
*/
public class EsService {

private RestClient restClient = null;
public boolean putSync(String index, String type, String id, HttpEntity entity) {
restClient = RestClient.builder(new HttpHost("192.168.1.3", 9200, "http")).build();
Response indexResponse = null;
try {
indexResponse = restClient.performRequest(
"POST",
"/" + index + "/" + type + "/" + id + "/_update",
Collections.<String, String>emptyMap(),
entity);
} catch (IOException e) {
e.printStackTrace();
}
if (restClient != null) {
try {
restClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return (indexResponse != null);
}
}

==========所有更新和删除文档的请求都接受 version 参数,它可以允许在你的代码中增加乐观锁控制。

转载于:https://www.cnblogs.com/yangjingzhi/p/8947895.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值