Sumanta Dutta
Sumanta Dutta

Reputation: 1

Not able to put data into elastic search index

Need help. I'm new to elasticsearch. I've created an account at bonsai.io to create an elastic search platform. I've been trying the below code base to connect but everytime I'm receiving error.

Code:

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();
    }
}

I've masked the username, password and host url within code. PFB the error I'm receiving:

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)

Not sure what I'm doing wrong. I already have an index created named "twitter". It will be great help if someone guide me about the issue. Thanks in advance

Upvotes: 0

Views: 2426

Answers (2)

ievgen
ievgen

Reputation: 1101

As a last effort you could try the following dirty hack to disable version validation and see if everything still works correctly.

    @SneakyThrows
    private void disableVersionValidation(@Nonnull RestHighLevelClient restHighLevelClient) {
        ListenableFuture<Optional<String>> mockVersionValidationFuture = new ListenableFuture<>();
        mockVersionValidationFuture.onResponse(Optional.empty());

        Field versionValidationFutureField = RestHighLevelClient.class.getDeclaredField("versionValidationFuture");
        versionValidationFutureField.setAccessible(true);
        versionValidationFutureField.set(restHighLevelClient, mockVersionValidationFuture);
    }

Upvotes: 1

Andreas C.
Andreas C.

Reputation: 51

It appear that you have two issues here.

First your logger is not configured correctly. That is why you are receiving

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

By adding the logger configuration in your classpath or using the following dependency should save you of this problem.

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

For the second one can you have to check the version of the RestHighLevelClient you are using. I assume that you might using the 7.xx one , which is alredy known to having some troubles with OSS flavors of ElasticSearch. Please try to downgrade your client version - see bonsai documentation as reference https://docs.bonsai.io/article/278-java

Upvotes: 4

Related Questions