[ES7版本系列(四)] Java连接ElasticSearch向索引中(bulk)批量更新数据

1.引入依赖,这里使用的是es的7以上的版本,使用elasticsearch-rest-high-level-client 高级别API来创建客户端

  <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
    <dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>elasticsearch</artifactId>
      <version>7.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.elasticsearch.client</groupId>
      <artifactId>elasticsearch-rest-high-level-client</artifactId>
      <version>7.3.1</version>
    </dependency>

2.批量请求更新的Demo

package cn.sse;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ElasticSearchBulkOpreation {

	public static void main(String[] args) throws IOException {
		//当es用用户名和密码连接时
		//初始化ES操作客户端
		final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY,
				new UsernamePasswordCredentials("elastic", "123456"));  //es账号密码(默认用户名为elastic)
		RestHighLevelClient client =new RestHighLevelClient(
				RestClient.builder(new HttpHost("192.168.6.1",9200,"http")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
					public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
						httpClientBuilder.disableAuthCaching();
						return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
					}
				})
		);

		当es无密码时可以直接使用下面这个创建客户端
		//RestHighLevelClient client =  new RestHighLevelClient(RestClient.builder(new HttpHost(
		//		"192.168.6.1",9200,"http"
		//)));


		//创建批量请求
		BulkRequest bulkRequest = new BulkRequest();
		//bulkRequest.add(new IndexRequest("sse_test").id("7").source(XContentType.JSON,"qq","哆啦A梦","weixin","123456"));
		Map<String,Object> map = new HashMap<>();
		map.put("qq","123456789");
		map.put("weixin","123456789");
		map.put("phone","123456789");
		Map<String,Object> map1 = new HashMap<>();
		map1.put("qq","1234");
		map1.put("weixin","123456789");
		map1.put("phone","1234");
		bulkRequest.add(new IndexRequest("sse_test").id("9").source(map));
		bulkRequest.add(new UpdateRequest("sse_test","9").doc(map1).upsert());
		client.bulk(bulkRequest, RequestOptions.DEFAULT);
		System.out.println("结束");
		
	}
}

3.使用kibana连接es查看时,输入GET /sse_test/_doc/9即可查看更新后的数据

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值