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

High Level REST Client 访问阿里云6.3 Elasticsearch 实例实现

日期:2019-01-14点击:491

开发环境:InteliJ IDEA

操作系统 :macOS Mojave

Elasticsearch 版本:阿里云 6.3.2_with_X-Pack

客户端版本:REST Client 6.3.2


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

7267421bdcb9ac46228b2049053fe238f0e43294


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

8d282de2736e3bc0800b2d57ab23f11f710c3813

mappings: book

properties: (book_id (keyword), name (text))

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


3. 创建项目及 RestClient 类

99cb144d9329839df7da39a352e483d273a4f220


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>high-level-rest-client-6</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.3.2</version> </dependency> </dependencies> </project>

JDK至少在 1.8及以上版本。

Java high level REST Client 版本建议不高于 ES 实例的版本,最理想的是和集群版本一致。


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

Java High Level REST Client 基于 Java Low Level REST client 实现的基础上,主要目标是为了暴露该 API 特定的方法。将 request 对象作为参数,返回一个 response 对象。该 API 可以同步或异步调用,同步调用方式立即返回一个 response 对象;而异步调用方式依赖于监听,该监听当有请求返回或是错误返回时通知到该方法继续执行。Java High Level REST Client 依赖于 Elasticsearch core 项目,接收的 request 对象和返回的 response 对象和 TransportClient 一样。

本例仅演示同步调用方式。

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.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class RestClientTest { public static void main(String[] args) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "******")); RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("es-cn-******.public.elasticsearch" + ".aliyuncs.com", 9200)) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } })); try { // 创建request Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("book_id", "0001"); jsonMap.put("name", "Pride and Prejudice"); IndexRequest indexRequest = new IndexRequest("index_test", "book", "0001") .source(jsonMap); // 同步执行 IndexResponse indexResponse = client.index(indexRequest); long version = indexResponse.getVersion(); System.out.println();

 client.close(); } catch (IOException ioException) { // 异常处理 } } }


该文档为第一次更新,成功返回版本号“1”,示例运行成功。
09234e0ab64c8a73e91f27a95613f015f723668e

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

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

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

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

文章评论

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

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章