我正在浏览这些文档,以从Elastic的高级JAVA REST客户端创建一个弹性搜索索引。似乎跳过了使用我的弹性云帐户进行身份验证的步骤。有人可以指出相关文档吗?
我启动了弹性搜索实例,并将端点URL复制到了我的客户代码中。
我最初有连接错误,现在没有。仅身份验证错误。因此,我很确定自己正在连接正确的端点URL,并且需要以某种方式进行身份验证-也许使用标头。
现在,我看到此错误:
Elasticsearch异常[类型= security_exception,原因=操作[索引:数据/写入/索引]需要身份验证]
我可以使用以下命令从Postman毫无问题地查看我的Elastic Search部署的端点:GET https://:@ d97215aee2.us-east-1.aws.found.io:9243
我还可以使用邮递员的此命令创建索引...放置https://elastic:4YQIMXfoSZ9mXPgY1fj7T5BU@d97218f74f6d48489b355dd7d665aee2.us-east-1.aws.found.io:9243/。但是,我无法通过Java代码执行相同的操作。
这里是我的Java代码的状态。这些教程页面中的代码差不多。
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:无效的IPv6地址
在这里找到答案: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
的索引,并将文档添加到该索引。