带有AWS ElasticSearch的Java RestHighLevelClient

时间:2018-07-13 16:48:09

标签: 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)                                                             
                                                                   ...........

1 个答案:

答案 0 :(得分:0)

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

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