php中文网

使用 Java 高级 REST 客户端提升您的 Elasticsearch 体验 ()

php中文网

介绍

java high level rest client (7.x) 是一个与 elasticsearch 集群交互的强大工具,使服务器通信更容易访问和高效。在本指南中,我们将引导您完成在阿里云 elasticsearch 集群上使用高级 rest 客户端调用 elasticsearch java api 的步骤。

准备工作

第1步:创建elasticsearch集群

确保您的集群版本与您计划使用的 java high level rest client 版本相同或更新。有关分步说明,请参阅创建阿里云 elasticsearch 集群。

第 2 步:启用自动索引

在 yaml 配置文件中启用自动索引功能。详情请参见配置yml文件。

步骤3:配置ip地址白名单

通过配置ip地址白名单确保正常通信。如果您通过 internet 访问集群,请按照配置公共或私有 ip 地址白名单中的指南允许来自所需 ip 地址的请求。

第四步:安装jdk

安装 java 开发工具包 (jdk) 1.8 或更高版本。有关更多信息,请参阅安装 jdk。

第5步:创建java maven项目

将必要的依赖项添加到您的 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();
        }
    }
}
</string>

高并发配置

对于高并发场景,增加客户端连接数:

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 high level rest client 文档。

结论

使用java high level rest client可以确保与阿里云elasticsearch集群的高效交互。按照本指南充分利用您的 elasticsearch 设置。
准备好开始您的阿里云 elasticsearch 之旅了吗?探索我们量身定制的云解决方案和服务,将您的数据转变为视觉杰作。

单击此处开始 30 天免费试用

以上就是使用 Java 高级 REST 客户端提升您的 Elasticsearch 体验 ()的详细内容,更多请关注php中文网其它相关文章!