使用JAVA Clients 连接ElasticSearch的不同场景

Maven依赖

<dependency>
  <groupId>co.elastic.clients</groupId>
  <artifactId>elasticsearch-java</artifactId>
  <version>8.0.1</version>
</dependency>

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.12.3</version>
</dependency>

<!-- Needed only if you use the spring-boot Maven plugin -->
<dependency> 
  <groupId>jakarta.json</groupId>
  <artifactId>jakarta.json-api</artifactId>
  <version>2.0.1</version>
</dependency>

测试代码

// 通用变量
public static ElasticsearchClient client_cur;
普通连接
/**
* 普通连接
*
* @throws IOException
*/
@Test
public void testSimpleConnect() throws IOException {
   RestClient restClient = RestClient.builder(new HttpHost("IP", 9200)).build();
   ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
   ElasticsearchClient client = new ElasticsearchClient(transport);
   InfoResponse response = client.info();
   Console.log(response.name());
   Assert.assertEquals("node-1", response.name());
}
使用用户名密码连接
/**
* 用户名密码连接
*
* @throws IOException
*/
@Test
public void testAuthConnect() throws IOException {
   final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
   credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pwd"));
   RestClientBuilder builder = RestClient.builder(new HttpHost("IP", 9200, "http"))
           .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
               @Override
               public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                   httpClientBuilder.disableAuthCaching();
                   return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
               }
           });
   RestClient restClient = builder.build();
   // Create the transport with a Jackson mapper
   ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
   // And create the API client
   ElasticsearchClient client = new ElasticsearchClient(transport);
   InfoResponse response = client.info();
   Console.log(response.name());
   Assert.assertEquals("node-1", response.name());
}
使用Token连接,在kibana配置文件中获取(仅限ES8.0以上版本可自动生成且免费)
/**
* 使用Token连接
 *
 * @throws IOException
 */
@Test
public void testTokenConnect() throws IOException {
    RestClientBuilder builder = RestClient.builder(new HttpHost("IP", 9200, "http"));
    Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization",
            "Bearer TVEAAWVsYXN0aWMva2liYW5hL1Vucm9sbC1wcm9jZXNzLXRva2VuLTE2NDUyNjg0MTY1NjM1SGo3WE9kOVJSdnF3cFU2S0N5bVYzUQ")};
    builder.setDefaultHeaders(defaultHeaders);
    RestClient restClient = builder.build();
    // Create the transport with a Jackson mapper
    ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
    // And create the API client
    ElasticsearchClient client = new ElasticsearchClient(transport);
    InfoResponse response = client.info();
    Console.log(response.name());
    Assert.assertEquals("node-1", response.name());
}
使用ApiKey连接,在kibana中使用相关命令生成key
/**
 * 使用ApiKey连接
 *
 * @throws IOException
 */
@Test
public void testApiKeyHttpConnect() throws IOException {
    String apiKeyEncoded = "XYZ2YXY0SUJBWXdOTAAESm42Q1I6c1dIZWs2Z2hUM0t6N3lPLXlSc05oZw==";
    RestClientBuilder builder = RestClient.builder(new HttpHost("IP", 9200, "http"));
    Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization",
            "ApiKey " + apiKeyEncoded)};
    builder.setDefaultHeaders(defaultHeaders);
    RestClient restClient = builder.build();
    // Create the transport with a Jackson mapper
    ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
    // And create the API client
    ElasticsearchClient client = new ElasticsearchClient(transport);
    InfoResponse response = client.info();
    Console.log(response.name());
    Assert.assertEquals("node-1", response.name());
}
使用ApiKey连接 使用TLS连接方式(https),在kibana中使用相关命令生成key
/**
* 使用ApiKey连接 使用TLS连接方式(https)
*
* @throws IOException
*/
@Test
public void testApiKeyHttpsConnect() throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, CertificateException {
    // 获取证书路径,此证书在Kibana中获取,8.0会自动生成
    FileSystemResource resource = new FileSystemResource("src/main/resources/certs/ca_1645268417123.crt");
    // System.out.println(resource.getFile().getAbsolutePath());
    Path caCertificatePath = Paths.get(resource.getFile().getAbsolutePath());
    CertificateFactory factory = CertificateFactory.getInstance("X.509");
    Certificate trustedCa = null;
    try (InputStream is = Files.newInputStream(caCertificatePath)) {
        trustedCa = factory.generateCertificate(is);
    } catch (CertificateException e) {
        e.printStackTrace();
    }
    KeyStore trustStore = KeyStore.getInstance("pkcs12");
    trustStore.load(null, null);
    trustStore.setCertificateEntry("ca", trustedCa);
    SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(trustStore, null);
    final SSLContext sslContext = sslContextBuilder.build();
    // 以上SSL设置完成,身份验证可以使用多种方式可以自由组合
    RestClientBuilder builder = RestClient.builder(new HttpHost("IP", 9200, "https"))
            .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                    return httpAsyncClientBuilder.setSSLContext(sslContext);
                }
            });
    String apiKeyEncoded = "XYZ2YXY0SUJBWXdOTAAESm42Q1I6c1dIZWs2Z2hUM0t6N3lPLXlSc05oZw==";
    Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization",
            "ApiKey " + apiKeyEncoded)};
    builder.setDefaultHeaders(defaultHeaders);
    RestClient restClient = builder.build();
    // Create the transport with a Jackson mapper
    ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
    // And create the API client
    ElasticsearchClient client = new ElasticsearchClient(transport);
    InfoResponse response = client.info();
    Console.log(response.name());
    Assert.assertEquals("node-1", response.name());
    client_cur = client;
}
使用
@Test
public void searchIndex() throws IOException {
	// Ecommerce 实体
    SearchResponse<Ecommerce> search = client_cur.search(s -> s
            .index("kibana_sample_data_ecommerce")
            .query(q -> q.term(t -> t.field("currency").value(v -> v.stringValue("EUR"))))
            .from(10).size(20), Ecommerce.class);
    for (Hit<Ecommerce> hit : search.hits().hits()) {
        System.out.println(JSONUtil.toJsonStr(hit.source()));
    }
}

@Test
public void createIndex() throws IOException {
    CreateIndexResponse createIndexResponse = client_cur.indices().create(q -> q.index("my-products")
            .aliases("foo", c -> c.isWriteIndex(true)));
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值