gpt4 book ai didi

Java RestHighLevelClient 与 AWS ElasticSearch

转载 作者:行者123 更新时间:2023-12-02 11:00:58 25 4
gpt4 key购买 nike

我想知道这里是否有人使用 RestHighLevelClient 连接到 AWS ElasticSearch。不确定 AWS ElasticSearch 是否支持此功能。目前,每次尝试连接时,我都会收到 ConnectionClosedException。

这是我所拥有的:

public SearchResponse getQuery(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 4430, "http")));
SearchRequest searchRequest = new SearchRequest("msglog-dev-2018.05.21");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = null;
try{
searchResponse =client.search(searchRequest);
}catch(IOException e){
e.printStackTrace();
}
return searchResponse;
}

我得到的错误是

org.apache.http.ConnectionClosedException: Connection closed
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:347)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:261)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
...........

最佳答案

是的,我们将 RHLC 与 AWS 结合使用。这是一个示例,应该可以帮助您朝着正确的方向前进。这演示了直接调用(这可能与更多读者相关)——但可以通过调整连接参数(主机、端口、协议(protocol))的设置来轻松适应您的隧道需求。

private static final String HOST = "your-es-endpoint.es.amazonaws.com";
private static final int PORT = 443;
private static final String PROTOCOL = "https";

private static final RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(HOST, PORT, PROTOCOL)));

public static void getESDocs() {
try {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery()); // adjust search logic here
SearchRequest searchRequest = new SearchRequest("your-index-here");
searchRequest.source(sourceBuilder);
final SearchResponse searchResponse = client.search(searchRequest);

/* process results here... */

}
} catch (Exception e) {
/* handle connection/proc error here */
} finally {
client.close(); // example to release driver resource (see doc link)
}
}

此示例对版本 6.3.x (YMMV) 有效: API documentation

关于Java RestHighLevelClient 与 AWS ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51329657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com