Java High Level REST Client (7.x) 是一個與 Elasticsearch 叢集互動的強大工具,使伺服器通訊更容易存取和高效。在本指南中,我們將引導您完成在阿里雲 Elasticsearch 叢集上使用進階 REST 用戶端呼叫 Elasticsearch Java API 的步驟。
確保您的叢集版本與您計劃使用的 Java High Level REST Client 版本相同或更高。有關逐步說明,請參閱建立阿里雲 Elasticsearch 叢集。
在 YAML 設定檔中啟用自動索引功能。詳情請參閱配置YML檔。
透過設定IP位址白名單確保正常通訊。如果您透過 Internet 存取集群,請依照設定公用或私人 IP 位址白名單中的指南允許來自所需 IP 位址的請求。
安裝 Java 開發工具包 (JDK) 1.8 或更高版本。有關詳細信息,請參閱安裝 JDK。
將必要的依賴項新增至您的 pom.xml 檔案中。將相依性中的版本號碼從 7.x 變更為您正在使用的進階 REST 用戶端的特定版本。
org.elasticsearch.client elasticsearch-rest-high-level-client 7.x org.apache.logging.log4j log4j-core 2.20.0 org.apache.logging.log4j log4j-api 2.20.0
以下是使用進階 REST 用戶端建立和刪除索引的範例。將佔位符 {} 替換為您的特定參數。
import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.*; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class RestClientExample { private static final RequestOptions COMMON_OPTIONS; static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); builder.setHttpAsyncResponseConsumerFactory( new HttpAsyncResponseConsumerFactory .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024)); COMMON_OPTIONS = builder.build(); } public static void main(String[] args) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("{Username}", "{Password}")); RestClientBuilder builder = RestClient.builder(new HttpHost("{Endpoint of the Elasticsearch cluster}", 9200, "http")) .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); RestHighLevelClient highClient = new RestHighLevelClient(builder); try { MapjsonMap = new HashMap(); jsonMap.put("{field_01}", "{value_01}"); jsonMap.put("{field_02}", "{value_02}"); IndexRequest indexRequest = new IndexRequest("{index_name}", "_doc", "{doc_id}").source(jsonMap); IndexResponse indexResponse = highClient.index(indexRequest, COMMON_OPTIONS); long version = indexResponse.getVersion(); System.out.println("Index document successfully! " version); DeleteRequest deleteRequest = new DeleteRequest("{index_name}", "_doc", "{doc_id}"); DeleteResponse deleteResponse = highClient.delete(deleteRequest, COMMON_OPTIONS); System.out.println("Delete document successfully! \n" deleteResponse.toString()); highClient.close(); } catch (IOException ioException) { ioException.printStackTrace(); } } }
針對高並發場景,增加客戶端連線數:
httpClientBuilder.setMaxConnTotal(500); httpClientBuilder.setMaxConnPerRoute(300);
範例程式碼片段:
String host = "127.0.0.1"; int port = 9200; String username = "elastic"; String password = "passwd"; final int max_conn_total = 500; final int max_conn_per_route = 300; RestHighLevelClient restHighLevelClient = new RestHighLevelClient( RestClient.builder(new HttpHost(host, port, "http")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { httpClientBuilder.setMaxConnTotal(max_conn_total); httpClientBuilder.setMaxConnPerRoute(max_conn_per_route); return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }) );
有關功能和配置的更多詳細信息,請參閱官方 Java High Level REST Client 文件。
使用 Java High Level REST Client 可確保與阿里雲 Elasticsearch 叢集高效互動。按照本指南充分利用您的 Elasticsearch 設定。
準備好開始阿里雲上的 Elasticsearch 之旅了嗎?探索我們量身訂製的雲端解決方案和服務,將您的數據轉變為視覺傑作。
點擊此處開始 30 天免費試用
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3