Java High Level REST Client (7.x) 是一个与 Elasticsearch 集群交互的强大工具,使服务器通信更加容易访问和高效。在本指南中,我们将引导您完成在阿里云 Elasticsearch 集群上使用高级 REST 客户端调用 Elasticsearch Java API 的步骤。
确保您的集群版本与您计划使用的 Java High Level REST Client 版本相同或更高。有关分步说明,请参阅创建阿里云 Elasticsearch 集群。
在 YAML 配置文件中启用自动索引功能。详情请参见配置YML文件。
通过配置IP地址白名单确保正常通信。如果您通过 Internet 访问集群,请按照配置公共或私有 IP 地址白名单中的指南允许来自所需 IP 地址的请求。
安装 Java 开发工具包 (JDK) 1.8 或更高版本。有关更多信息,请参阅安装 JDK。
将必要的依赖项添加到您的 pom.xml 文件中。将依赖项中的版本号从 7.x 更改为您正在使用的高级 REST 客户端的特定版本。
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.x</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.20.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.20.0</version> </dependency>
下面是使用高级 REST 客户端创建和删除索引的示例。将占位符 {} 替换为您的具体参数。
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.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.*; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class RestClientExample { private static final RequestOptions COMMON_OPTIONS; static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); builder.setHttpAsyncResponseConsumerFactory( new HttpAsyncResponseConsumerFactory .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024)); COMMON_OPTIONS = builder.build(); } public static void main(String[] args) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("{Username}", "{Password}")); RestClientBuilder builder = RestClient.builder(new HttpHost("{Endpoint of the Elasticsearch cluster}", 9200, "http")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); RestHighLevelClient highClient = new RestHighLevelClient(builder); try { Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("{field_01}", "{value_01}"); jsonMap.put("{field_02}", "{value_02}"); IndexRequest indexRequest = new IndexRequest("{index_name}", "_doc", "{doc_id}").source(jsonMap); IndexResponse indexResponse = highClient.index(indexRequest, COMMON_OPTIONS); long version = indexResponse.getVersion(); System.out.println("Index document successfully! " + version); DeleteRequest deleteRequest = new DeleteRequest("{index_name}", "_doc", "{doc_id}"); DeleteResponse deleteResponse = highClient.delete(deleteRequest, COMMON_OPTIONS); System.out.println("Delete document successfully! \n" + deleteResponse.toString()); highClient.close(); } catch (IOException ioException) { ioException.printStackTrace(); } } }
针对高并发场景,增加客户端连接数:
httpClientBuilder.setMaxConnTotal(500); httpClientBuilder.setMaxConnPerRoute(300);
示例代码片段:
String host = "127.0.0.1"; int port = 9200; String username = "elastic"; String password = "passwd"; final int max_conn_total = 500; final int max_conn_per_route = 300; RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder(new HttpHost(host, port, "http")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { httpClientBuilder.setMaxConnTotal(max_conn_total); httpClientBuilder.setMaxConnPerRoute(max_conn_per_route); return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }) );
有关功能和配置的更多详细信息,请参阅官方 Java 高级 REST 客户端文档。
使用 Java High Level REST Client 可确保与阿里云 Elasticsearch 集群高效交互。按照本指南充分利用您的 Elasticsearch 设置。
准备好开始阿里云上的 Elasticsearch 之旅了吗?探索我们量身定制的云解决方案和服务,将您的数据转变为视觉杰作。
单击此处开始 30 天免费试用
以上是使用 Java 高级 REST 客户端提升您的 Elasticsearch 体验 ()的详细内容。更多信息请关注PHP中文网其他相关文章!