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

开发环境: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 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", "Gerry0305"));

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("es-cn-mp90ww1j0001g5m8f.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值