ElasticSearch _update_by_query 批量更新超时

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/come_on_ha/article/details/79947891

ElasticSearch批量更新语句如下:

POST /products_survey/products_survey/_update_by_query

{"query":{"bool":{"must":[],"must_not":[]}},"script":{"source":"ctx._source.CHECKED='1'"}}

RestClient创建代码:      

 restClient = RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))

        .build();

项目中需要对ElasticSearch的整个type的数据进行更新,数据量10万,采用RestClient更新,在没有设置超时的情况下,报错如下:

java.io.IOException: listener timeout after waiting for [30000] ms

默认的监听超时,默认请求超过30秒就超时,因此在创建RestClient的时候需要设置超时时间:

restClient = RestClient.builder(
    new HttpHost(GSConstants.P_ES_HOST, GSConstants.P_ES_PORT, "http"))
        .setMaxRetryTimeoutMillis(5*60*1000)//超时时间5分钟

        .build();

发送请求后,还是报错,是SocketTimeout

java.net.SocketTimeoutException
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:375)

查看RestClient.builder方法的源码,创建RestClientBuilder对象,并返回RestClient,看到有一个HttpClientConfigCallback属性,可以通过setRequestConfigCallback方法进行设置

public RestClientBuilder setRequestConfigCallback(RequestConfigCallback requestConfigCallback) {
        Objects.requireNonNull(requestConfigCallback, "requestConfigCallback must not be null");
        this.requestConfigCallback = requestConfigCallback;
        return this;

    }

因此对RestClient的创建做如下修改:

            restClient = RestClient.builder(
            new HttpHost(GSConstants.P_ES_HOST, GSConstants.P_ES_PORT, "http"))
            .setMaxRetryTimeoutMillis(TIMEOUT)
            .setHttpClientConfigCallback(new HttpClientConfigCallback(){
@Override
public HttpAsyncClientBuilder customizeHttpClient(
HttpAsyncClientBuilder httpClientBuilder) {
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
               .setConnectTimeout(5*60*1000)//超时时间5分钟
               .setSocketTimeout(5*60*1000)//这就是Socket超时时间设置
               .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS);
httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
return httpClientBuilder; 
}

            }).build();


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页