概述
获取Elasticsearch客户端对象:
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import java.util.Arrays;
import java.util.function.Function;
public class ElasticUtil {
public static ElasticsearchClient getESObj(){
HttpHost[] ips = Arrays
.stream(PropertiesUtil.getPro("ip").split(";"))
.map(new Function<String, HttpHost>() {
@Override
public HttpHost apply(String s) {
return new HttpHost(s, Integer.valueOf(PropertiesUtil.getPro("port")));
}
}).toArray(HttpHost[]::new);
RestClient restClient = RestClient.builder(ips)
.setHttpClientConfigCallback(httpClientBuilder -> {
BasicCredentialsProvider bcp = new BasicCredentialsProvider();
bcp.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(PropertiesUtil.getPro("esUser"), PropertiesUtil.getPro("esPass")));
HttpAsyncClientBuilder httpAsyncClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(bcp);
return httpAsyncClientBuilder;
}).build();
RestClientTransport restClientTransport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient client = new ElasticsearchClient(restClientTransport);
return client;
}
}
依赖:
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>7.17.0</version>
</dependency>
<!-- <!– https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client –>-->
<!-- <dependency>-->
<!-- <groupId>org.elasticsearch.client</groupId>-->
<!-- <artifactId>elasticsearch-rest-high-level-client</artifactId>-->
<!-- <version>7.17.0</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.17.0</version>
</dependency>
从TransportClient对象追到RestHighLevelClient对象,发现都过期了,这个更迭有点快!
补充:新包Java删除数据操作
增改查操作EsSpark对象就可以直接完成,不赘述,没找到spark删除es数据的操作,这里是Java版本:
public void deleteData(String index,String id){
ElasticsearchClient client = ElasticUtil.getEsObj();
DeleteRequest deleteRequest = new DeleteRequest.Builder().index(index).id(id).build();
try {
client.delete(deleteRequest);
} catch (IOException e) {
e.printStackTrace();
}
}
补充:deleteByQuery
public void delete(String index, String fieldName, String fieldValue){
ElasticsearchClient client = ElasticUtils.getEsObj();
Query query = new QueryStringQuery.Builder()
.fields(fieldName)
.query(fieldValue)
.build()
._toQuery();
try {
client.deleteByQuery(new DeleteByQueryRequest.Builder().index(index).query(query).build());
} catch (IOException e) {
e.printStackTrace();
}
}
最后
以上就是任性小甜瓜为你收集整理的操作Elasticsearch(新版本)的全部内容,希望文章能够帮你解决操作Elasticsearch(新版本)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复