java - 如何使用 JAVA 高级 REST 客户端创建 Elasticsearch 索引?

标签 java elasticsearch resthighlevelclient

我正在浏览这些文档,以便从 Elastic 的高级 JAVA REST 客户端创建 Elasticsearch 索引。它似乎跳过了使用我的弹性云帐户进行身份验证的步骤。 有人可以指点我相关文档吗?

我启动了 Elasticsearch 实例并将端点 URL 复制到我的客户端代码中。

我最初遇到连接错误,现在没有了。仅身份验证错误。因此,我非常确定我正在连接正确的端点 URL,并且需要以某种方式进行身份验证 - 也许使用 header 。

现在,我看到这个错误:

Elasticsearch exception [type=security_exception, reason=action [indices:data/write/index] requires authentication]

我可以使用以下命令从 Postman 中查看 Elastic Search 部署的端点,没有任何问题: 获取 https://:@d97215aee2.us-east-1.aws.found.io:9243

我还可以使用 Postman 的此命令创建索引... 放置https://elastic:4YQIMXfoSZ9mXPgY1fj7T5BU@d97218f74f6d48489b355dd7d665aee2.us-east-1.aws.found.io:9243/ 。然而,我无法从 Java 代码中执行相同的操作。

这是我的 Java 代码的状态。这几乎就是这些教程页面中的代码。

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.4/java-rest-high-document-index.html

import java.io.IOException;

import javax.ws.rs.POST;
import javax.ws.rs.Path;

import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

@Path("/elasticsearch")
public class ElasticSearchService {

    @POST
    public void createElasticIndex() throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                    new HttpHost("d9<deleted a bunch of characters for privacy>7d665aee2.us-east-1.aws.found.io", 9243, "https")));


        IndexRequest request = new IndexRequest(
                "posts",
                "doc",
                "1");
        String jsonString = "{" +
                "\"user\":\"kimchy\"," +
                "\"postDate\":\"2013-01-30\"," +
                "\"message\":\"trying out Elasticsearch\"" +
                "}";
        request.source(jsonString, XContentType.JSON);

        client.close();
    }    
}

我还尝试按照本文的建议使用我们的用户名和密码更新 URL 地址:ElasticSearch authentication error with ElasticCloud?

基本上,我像这样更新了我的网址...

        RestClient.builder(
                new HttpHost(
                        "<my user name>:<my password>@d97218<hidden characters>d665aee2.us-east-1.aws.found.io",
                        9243, "https")));

这对我不起作用。我猜这个人没有使用新的 Elastic 高级 REST 客户端。 我收到此错误:

org.glassfish.jersey.server.internal.process.MappableException: java.io.IOException: :@d97265aee2.us-east-1.aws.found.io: invalid IPv6 address

最佳答案

在这里找到答案:enter link description here

更新后的代码有效:

import java.io.IOException;

import javax.ws.rs.POST;
import javax.ws.rs.Path;

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.ElasticsearchException;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;

@Path("/elasticsearch")
public class ElasticSearchService {
    private static final String ELASTIC_SEARCH_USER_NAME = <my elastic search username>;
    private static final String ELASTIC_SEARCH_PASSWORD = <my elastic search password>;
    private static final String ELASTIC_SEARCH_ENDPOINT_URL = <my elastic search endpoint url>
    private static final Integer ELASTIC_SEARCH_PORT = 9243;

    @POST
    public void createElasticIndex() throws IOException {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(ELASTIC_SEARCH_USER_NAME, ELASTIC_SEARCH_PASSWORD));

        RestClientBuilder builder = RestClient
                .builder(new HttpHost(
                        ELASTIC_SEARCH_ENDPOINT_URL,
                        ELASTIC_SEARCH_PORT, "https"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        RestHighLevelClient client = new RestHighLevelClient(builder);

        IndexRequest request = new IndexRequest(
                "contacts",
                "doc",
                "1");
        String jsonString = "{" +
                "\"user\":\"frank\"," +
                "\"postDate\":\"2020-03-02\"," +
                "\"message\":\"created this document from Java\"" +
                "}";
        request.source(jsonString, XContentType.JSON);

        try {
            IndexResponse response = client.index(request, RequestOptions.DEFAULT);
            System.out.println(response);

        } catch (ElasticsearchException e) {
            if (e.status() == RestStatus.CONFLICT) {
            }
        }

        client.close();
    }

}

此代码创建一个名为 contacts 的索引,并向该索引添加一个文档。

关于java - 如何使用 JAVA 高级 REST 客户端创建 Elasticsearch 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60480002/

相关文章:

java - URI 与 URL 与 URN

java - 我们应该使用什么客户端(java 或 rest)来为 Elasticsearch 中的数据建立索引?

java - 从ElasticSearch的索引中删除值为以下一项之一的多个文档-7.8版本

elasticsearch - mget 生成 : Unable to parse response body for Response

java - ElasticSearch使字段无法从Java搜索

Java 数组列表/RMI

java - 在子类和父类(super class)中具有相同的变量

java - jOOQ - DSL.dateAdd 与 Field<Timestamp>

spring - 使用Spring Data ElasticSearch 3.0.0.BUILD-SNAPSHOT和Elasticsearch 5.4.0的错误分页

python - 使用elasticsearch-dsl-py查询时间戳范围