您现在的位置是:首页 > 文章详情

Java REST Client 访问阿里云5.5 Elasticsearch 实例实现

日期:2019-01-12点击:382

开发环境:InteliJ IDEA

操作系统 :macOS Mojave

Elasticsearch 版本:阿里云 5.5.3_with_X-Pack

客户端版本:REST Client 5.5.3


1. 预先创建好阿里云 ES 实例,开启公网地址访问白名单。

7267421bdcb9ac46228b2049053fe238f0e43294


2. 预先创建好 index 和 mapping(使用 Kibana Dev Tools 创建)

9e3d85435948be775a72e76af2f9c43b4dbaa08c


PUT index_test { "mappings": { "book": { "properties" : { "book_id" : { "type":"keyword" }, "name" : { "type":"text" } } } } }


3. 创建项目及 RestClient 类

47c4e4f3ce2e6281ebe1e7ba2ddbeaa94821cc29


4. pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>me.gary.es</groupId> <artifactId>rest-client-5</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/rest --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>rest</artifactId> <version>5.5.3</version> </dependency> </dependencies> </project>

此处的 Java REST Client Demo主要适用于阿里云ES 5.5.3版本,不兼容阿里云ES 6.3.2版本。

由于阿里云Elasticsearch实例使用5.5.3版本,所以需要您的JDK至少在 1.8 及以上版本。

Java REST Client版本需要与ES实例版本一致。


5. 构建 REST Client 对象进行访问

步骤一:

由于阿里云 ES 强制要求 elasticsearch-http-basic 认证,ES 官方给出的指导是:通过 builder 构造 RestClient 对象的同时,设定 builder 的回调接口 HttpClientConfigCallback。该回调接口仅有一个实现方法 customizeHttpClient(),参数接收一个 HttpAsyncClientBuilder 对象,设置该对象的验证信息(通过 CredentialProvider),然后返回该对象。

以此方法,来给 RestClient 设置验证信息,在后续向 ES 服务端请求时,带上验证信息。


步骤二:

通过 HttpEntity,拼接 JSON 请求,通过 restClient.performRequest() 发起请求。此例主要演示:创建一条索引文档并检索该文档。

import org.apache.http.HttpEntity; 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.entity.ContentType; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.apache.http.nio.entity.NStringEntity; import org.apache.http.util.EntityUtils; import org.elasticsearch.client.Response; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import java.io.IOException; import java.util.Collections; public class RestClientTest { public static void main(String[] args) { // 步骤一:创建 RestClient 对象 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("ES实例用户名", "ES实例密码")); RestClient restClient = RestClient.builder(new HttpHost("ES实例公网地址", 9200)) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }).build(); // 步骤二:发起请求 try { //index a document 往ES索引增加一条数据 HttpEntity entity = new NStringEntity("{\n\"book_id\":\"0001\",\n\"name\":\"Alice in Wonderland\"\n}", ContentType.APPLICATION_JSON); Response indexResponse = restClient.performRequest( "PUT", "/index_test/book/0001", Collections.<String, String>emptyMap(), entity); //search a document 检索ES数据 Response response = restClient.performRequest("GET", "/index_test/book/0001", Collections.singletonMap("pretty", "true")); System.out.println(EntityUtils.toString(response.getEntity())); } catch (IOException e) { e.printStackTrace(); } } }


65a1e59e6fecb84fbd6725dc8fe03bb9e30bd95c


该文档成功创建并检索得到,请求成功!


原文链接:https://yq.aliyun.com/articles/686692
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章