0

需要帮忙。我是弹性搜索的新手。我在 bonsai.io 创建了一个帐户来创建一个弹性搜索平台。我一直在尝试下面的代码库进行连接,但每次我收到错误。

代码:

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.ActionListener;
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.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class ElasticSearchConsumer {

    public static RestHighLevelClient createClient(){
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(<username>, <password>));

        RestClientBuilder builder = RestClient.builder(
                        new HttpHost(<hostURL>, 443, "https"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                        return httpAsyncClientBuilder.setDefaultCredentialsProvider(credsProvider);
                    }
                });

        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;

    }

    public static void main(String[] args) throws IOException {

        Logger logger = LoggerFactory.getLogger(ElasticSearchConsumer.class.getName());

        RestHighLevelClient client = createClient();
        String testString = "{\"name\":\"Sumanta\", \"Nationality\":\"Indian\"}";
        IndexRequest indexRequest = new IndexRequest("twitter").source(testString, XContentType.JSON);


        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
        String id = response.getId();


        logger.info(id);


        //close the client
        client.close();
    }
}

我已经在代码中隐藏了用户名、密码和主机 URL。PFB 我收到的错误:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Exception in thread "main" ElasticsearchException[Invalid or missing build flavor [oss]]
    at org.elasticsearch.client.RestHighLevelClient.performClientRequest(RestHighLevelClient.java:2084)
    at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1732)
    at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1702)
    at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1672)
    at org.elasticsearch.client.RestHighLevelClient.index(RestHighLevelClient.java:1029)
    at com.github.simplekafka.tutorial3.ElasticSearchConsumer.main(ElasticSearchConsumer.java:59)

不知道我做错了什么。我已经创建了一个名为“twitter”的索引。如果有人指导我解决这个问题,那将是很大的帮助。提前致谢

4

1 回答 1

4

看来您在这里有两个问题。

首先,您的记录器配置不正确。这就是你收到的原因

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

通过在类路径中添加记录器配置或使用以下依赖项应该可以避免这个问题。

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.8.2</version>

对于第二个,您必须检查您正在使用的RestHighLevelClient的版本。我假设您可能会使用 7.xx 版本,众所周知,它在使用 ElasticSearch 的 OSS 风格时会遇到一些问题。请尝试降级您的客户端版本 - 请参阅盆景文档作为参考https://docs.bonsai.io/article/278-java

于 2021-08-23T19:29:30.620 回答