2016-11-15 97 views
1

我對ElasticSearch比較新,我注意到當你啓動elasticsearch Java Client時,它啓動了大量的線程(〜50)。ElasticSearch Java API異步書寫

我試圖利用這一點,購買找不到一種方式,以異步寫入(索引)ES。

官方API的建議是使用:

IndexResponse response = client.prepareIndex(indexName, documentName) 
      .setSource(mapper.writeValueAsString(data)) 
      .get(); 

即使我有這個運行在一個新的線程它仍然封鎖,因爲它等待響應,包含新創建的ID等等。

是否可以以異步方式寫入ES,而不必創建另外50個本地線程來匹配ES內部的50個本地線程?

回答

1

如果有人絆倒了這一點,解決方案是使用.execute(),它返回ListenableActionFuture<Response>

例如

client.prepareIndex(indexName, documentName) 
     .setSource(mapper.writeValueAsString(data)) 
     .execute();