Elasticsearch Java Low Level REST Client(執行請求)

執行請求

一旦建立了RestClient,就能夠經過調用performRequestperformRequestAsync來發送請求,performRequest是同步的,將阻塞調用線程並在請求成功時返回Response,若是失敗則拋出異常。performRequestAsync是異步的,它接受一個ResponseListener參數,它在請求成功時調用Response,若是失敗則調用Exceptionjson

這是同步的:segmentfault

Request request = new Request(
    "GET",  
    "/");   
Response response = restClient.performRequest(request);
  • 第一個參數:HTTP方法(GETPOSTHEAD等)。
  • 第二個參數:服務器上的端點。

這是異步的:服務器

Request request = new Request(
    "GET",  
    "/");   
restClient.performRequestAsync(request, new ResponseListener() {
    @Override
    public void onSuccess(Response response) {
        
    }

    @Override
    public void onFailure(Exception exception) {
        
    }
});
  • onSuccess方法:處理響應。
  • onFailure:處理失敗。

你能夠將請求參數添加到請求對象:app

request.addParameter("pretty", "true");

你能夠將請求的body設置爲任何HttpEntity異步

request.setEntity(new NStringEntity(
        "{\"json\":\"text\"}",
        ContentType.APPLICATION_JSON));
HttpEntity指定的 ContentType很重要,由於它將用於設置 Content-Type header,以便Elasticsearch能夠正確解析內容。

你還能夠將其設置爲String,默認爲ContentTypeapplication/jsonide

request.setJsonEntity("{\"json\":\"text\"}");

RequestOptions

RequestOptions類保存應在同一應用程序中的多個請求之間共享的部分請求,你能夠建立單例實例並在全部請求之間共享它:post

private static final RequestOptions COMMON_OPTIONS;
static {
    RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
    builder.addHeader("Authorization", "Bearer " + TOKEN); 
    builder.setHttpAsyncResponseConsumerFactory(           
        new HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
    COMMON_OPTIONS = builder.build();
}
  • builder.addHeader:添加全部請求所需的任何header。
  • builder.setHttpAsyncResponseConsumerFactory:自定義響應消費者。

addHeader用於受權或在Elasticsearch前使用代理所需的header,無需設置Content-Type header,由於客戶端將自動從附加到請求的HttpEntity設置該header。ui

你能夠設置NodeSelector來控制哪些節點將接收請求。線程

NodeSelector.NOT_MASTER_ONLY是一個不錯的選擇。代理

你還能夠自定義用於緩衝異步響應的響應消費者,默認消費者將在JVM堆上緩衝最多100MB的響應,若是響應較大,則請求將失敗。例如,若是你在堆約束環境(如上面的例子)中運行,則能夠下降可能有用的最大大小。

建立單例後,你能夠在發出請求時使用它:

request.setOptions(COMMON_OPTIONS);

你還能夠根據請求自定義這些選項,例如,這會添加額外的header:

RequestOptions.Builder options = COMMON_OPTIONS.toBuilder();
options.addHeader("cats", "knock things off of other things");
request.setOptions(options);

多個並行異步操做

客戶端很樂意並行執行許多操做,如下示例並行索引許多文檔,在現實世界的場景中,你可能但願使用_bulk API,但示例是做例證的。

final CountDownLatch latch = new CountDownLatch(documents.length);
for (int i = 0; i < documents.length; i++) {
    Request request = new Request("PUT", "/posts/doc/" + i);
    //let's assume that the documents are stored in an HttpEntity array
    request.setEntity(documents[i]);
    restClient.performRequestAsync(
            request,
            new ResponseListener() {
                @Override
                public void onSuccess(Response response) {
                    
                    latch.countDown();
                }

                @Override
                public void onFailure(Exception exception) {
                    
                    latch.countDown();
                }
            }
    );
}
latch.await();
  • onSuccess:處理返回的響應。
  • onFailure:因爲通訊錯誤或帶有指示錯誤的狀態碼的響應,處理返回的異常。

上一篇:初始化

下一篇:讀取響應

相關文章
相關標籤/搜索