需要帮忙。我是弹性搜索的新手。我在 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”的索引。如果有人指导我解决这个问题,那将是很大的帮助。提前致谢