Java RestHighLevelClient 与 AWS ElasticSearch

标签 java amazon-web-services spring-boot elasticsearch

我想知道这里是否有人使用 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/

相关文章:

java - 如何为 CORS 指定响应 header ?

java - Wordnet 相似度 4 Java (WS4J)

Java 小程序菜单

java - 使 JPA 持久化上下文识别批量操作所做的更改

amazon-web-services - AWS 区域的确切含义以及为业务选择合适的区域

mysql - 在 AWS RDS 上混合使用 MyISAM 和 InnoDB 的问题

java - java 和 .net 应用程序之间的身份验证

amazon-web-services - 从 AWS Lambda 调用 aws-cli

java - Spring Boot - 创建名称为 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration' 的 bean 时出错

java - RabbitMQ java 客户端停止消费消息