<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>
/**
* 用户名密码连接
*
* @throws IOException
*/@TestpublicvoidtestAuthConnect()throwsIOException{finalCredentialsProvider credentialsProvider =newBasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,newUsernamePasswordCredentials("user","pwd"));RestClientBuilder builder =RestClient.builder(newHttpHost("IP",9200,"http")).setHttpClientConfigCallback(newRestClientBuilder.HttpClientConfigCallback(){@OverridepublicHttpAsyncClientBuildercustomizeHttpClient(HttpAsyncClientBuilder httpClientBuilder){
httpClientBuilder.disableAuthCaching();return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);}});RestClient restClient = builder.build();// Create the transport with a Jackson mapperElasticsearchTransport transport =newRestClientTransport(restClient,newJacksonJsonpMapper());// And create the API clientElasticsearchClient client =newElasticsearchClient(transport);InfoResponse response = client.info();Console.log(response.name());Assert.assertEquals("node-1", response.name());}
使用Token连接,在kibana配置文件中获取(仅限ES8.0以上版本可自动生成且免费)
/**
* 使用Token连接
*
* @throws IOException
*/@TestpublicvoidtestTokenConnect()throwsIOException{RestClientBuilder builder =RestClient.builder(newHttpHost("IP",9200,"http"));Header[] defaultHeaders =newHeader[]{newBasicHeader("Authorization","Bearer TVEAAWVsYXN0aWMva2liYW5hL1Vucm9sbC1wcm9jZXNzLXRva2VuLTE2NDUyNjg0MTY1NjM1SGo3WE9kOVJSdnF3cFU2S0N5bVYzUQ")};
builder.setDefaultHeaders(defaultHeaders);RestClient restClient = builder.build();// Create the transport with a Jackson mapperElasticsearchTransport transport =newRestClientTransport(restClient,newJacksonJsonpMapper());// And create the API clientElasticsearchClient client =newElasticsearchClient(transport);InfoResponse response = client.info();Console.log(response.name());Assert.assertEquals("node-1", response.name());}
使用ApiKey连接,在kibana中使用相关命令生成key
/**
* 使用ApiKey连接
*
* @throws IOException
*/@TestpublicvoidtestApiKeyHttpConnect()throwsIOException{String apiKeyEncoded ="XYZ2YXY0SUJBWXdOTAAESm42Q1I6c1dIZWs2Z2hUM0t6N3lPLXlSc05oZw==";RestClientBuilder builder =RestClient.builder(newHttpHost("IP",9200,"http"));Header[] defaultHeaders =newHeader[]{newBasicHeader("Authorization","ApiKey "+ apiKeyEncoded)};
builder.setDefaultHeaders(defaultHeaders);RestClient restClient = builder.build();// Create the transport with a Jackson mapperElasticsearchTransport transport =newRestClientTransport(restClient,newJacksonJsonpMapper());// And create the API clientElasticsearchClient client =newElasticsearchClient(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
*/@TestpublicvoidtestApiKeyHttpsConnect()throwsIOException,KeyStoreException,NoSuchAlgorithmException,KeyManagementException,CertificateException{// 获取证书路径,此证书在Kibana中获取,8.0会自动生成FileSystemResource resource =newFileSystemResource("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);finalSSLContext sslContext = sslContextBuilder.build();// 以上SSL设置完成,身份验证可以使用多种方式可以自由组合RestClientBuilder builder =RestClient.builder(newHttpHost("IP",9200,"https")).setHttpClientConfigCallback(newRestClientBuilder.HttpClientConfigCallback(){@OverridepublicHttpAsyncClientBuildercustomizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder){return httpAsyncClientBuilder.setSSLContext(sslContext);}});String apiKeyEncoded ="XYZ2YXY0SUJBWXdOTAAESm42Q1I6c1dIZWs2Z2hUM0t6N3lPLXlSc05oZw==";Header[] defaultHeaders =newHeader[]{newBasicHeader("Authorization","ApiKey "+ apiKeyEncoded)};
builder.setDefaultHeaders(defaultHeaders);RestClient restClient = builder.build();// Create the transport with a Jackson mapperElasticsearchTransport transport =newRestClientTransport(restClient,newJacksonJsonpMapper());// And create the API clientElasticsearchClient client =newElasticsearchClient(transport);InfoResponse response = client.info();Console.log(response.name());Assert.assertEquals("node-1", response.name());
client_cur = client;}
使用
@TestpublicvoidsearchIndex()throwsIOException{// 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()));}}@TestpublicvoidcreateIndex()throwsIOException{CreateIndexResponse createIndexResponse = client_cur.indices().create(q -> q.index("my-products").aliases("foo", c -> c.isWriteIndex(true)));}