且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-14 22:39:01

开发环境:InteliJ IDEA

操作系统 :macOS Mojave

Elasticsearch 版本:阿里云 6.3.2_with_X-Pack

客户端版本:REST Client 6.3.2


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

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


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

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

mappings: book

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

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


3. 创建项目及 RestClient 类

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


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”,示例运行成功。
High Level REST Client 访问阿里云6.3 Elasticsearch 实例实现