Java Low Level REST Client是Elasticsearch官方提供的低層級REST用戶端,其API不負責資料的編碼與解碼。Lindorm搜尋引擎相容Elasticsearch 7.10及更早版本的功能,如果您想要自訂請求和響應處理方式,可以通過Java Low Level REST Client串連並使用搜尋引擎。
前提條件
操作步驟
安裝Low Level Rest Client。以Maven專案為例,在
pom.xml
檔案的dependencies
中添加依賴項。範例程式碼如下:<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.10.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency>
配置串連參數。
//Lindorm搜尋引擎的Elasticsearch相容地址 String search_url = "ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com"; int search_port = 30070; // 配置使用者名稱密碼 String username = "user"; String password = "test"; final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(search_url, search_port)); restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } });
參數說明
參數
說明
search_url
Lindorm搜尋引擎的Elasticsearch相容地址。如何擷取,請參見查看串連地址。
重要如果應用部署在ECS執行個體,建議您通過專用網路訪問Lindorm執行個體,可獲得更高的安全性和更低的網路延遲。
如果應用部署在本地,在通過公網串連Lindorm執行個體前,需在控制台開通公網地址。開通方式:在控制台的左側導覽列,選擇資料庫連接,單擊搜尋引擎頁簽,在頁簽右上方單擊開通公網地址。
通過專用網路訪問Lindorm執行個體,search_url請填寫Elasticsearch相容地址對應的專用網路地址。通過公網訪問Lindorm執行個體,search_url請填寫Elasticsearch相容地址對應的公網地址。
search_port
Lindorm搜尋引擎Elasticsearch相容的連接埠,固定為30070。
username
訪問搜尋引擎的使用者名稱和密碼。
預設使用者名和密碼的擷取方式:在控制台的左側導覽列,選擇資料庫連接,單擊搜尋引擎頁簽,在搜尋引擎頁簽可擷取。
password
使用Java Low Level REST Client對象訪問搜尋引擎。
範例程式碼分為以下幾部分:
建立搜尋索引:建立搜尋索引lindorm_index。
資料寫入:使用批量資料寫入的方式,寫入5000條文檔。
資料查詢:重新整理請求,強制寫入的資料可見。執行兩個請求,分別查詢索引中的全部文檔和ID為特定值的文檔。
資料刪除:刪除ID為test的文檔,並刪除索引lindorm_index。
try (RestClient restClient = restClientBuilder.build()) { String indexName = "lindorm_index"; // 建立索引 Request indexRequest = new Request("PUT", "/" + indexName); indexRequest.setJsonEntity("{" + " \"settings\":{" + " \"index.number_of_shards\": 1" + " }," + " \"mappings\":{" + " \"properties\":{" + " \"name\":{" + " \"type\":\"text\"" + " }" + " }" + " }" + "}"); Response response = restClient.performRequest(indexRequest); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("responseBody = " + responseBody); // 批量寫入資料 Random random = new Random(); Request bulkRequest = new Request("POST", "/_bulk"); StringBuilder bulkJsonBuilder = new StringBuilder(); for (int i = 0; i < 5000; i++) { // 請將field和value替換為實際業務欄位與值 bulkJsonBuilder.append("{\"index\":{\"_index\":\"").append(indexName).append("\",\"_id\":\"").append(i).append("\"}}").append("\n"); String value = random.nextInt() + ""; bulkJsonBuilder.append("{\"field1\":\"").append(value).append("\",\"field2\":\"").append(value).append("\"}").append("\n"); } bulkRequest.setJsonEntity(bulkJsonBuilder.toString()); response = restClient.performRequest(bulkRequest); // 發送重新整理請求,強制已寫資料可見 response = restClient.performRequest(new Request("POST", "/" + indexName + "/_refresh")); responseBody = EntityUtils.toString(response.getEntity()); System.out.println("responseBody = " + responseBody); // 查詢索引中所有資料,預設最多返回10條查詢結果 response = restClient.performRequest(new Request("GET", "/" + indexName + "/_search")); responseBody = EntityUtils.toString(response.getEntity()); System.out.println("responseBody = " + responseBody); // 查詢索引中文檔id為"0"的資料 response = restClient.performRequest(new Request("GET", "/" + indexName + "/_doc/0")); responseBody = EntityUtils.toString(response.getEntity()); System.out.println("responseBody = " + responseBody); // 刪除索引 response = restClient.performRequest(new Request("DELETE", "/" + indexName)); responseBody = EntityUtils.toString(response.getEntity()); System.out.println("responseBody = " + responseBody); } catch (Exception e) { System.out.println("msg: " + e.getMessage()); }
完整樣本
完整範例程式碼如下:
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.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import java.util.Random;
public class RestLClientTest {
public static void main(String[] args) {
// Lindorm搜尋引擎的Elasticsearch相容地址
String search_url = "ld-t4n5668xk31ui****-proxy-search-public.lindorm.rds.aliyuncs.com";
int search_port = 30070;
// 配置使用者名稱密碼
String username = "user";
String password = "test";
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(search_url, search_port));
restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
try (RestClient restClient = restClientBuilder.build()) {
String indexName = "lindorm_index";
// 建立索引
Request indexRequest = new Request("PUT", "/" + indexName);
indexRequest.setJsonEntity("{" +
" \"settings\":{" +
" \"index.number_of_shards\": 1" +
" }," +
" \"mappings\":{" +
" \"properties\":{" +
" \"name\":{" +
" \"type\":\"text\"" +
" }" +
" }" +
" }" +
"}");
Response response = restClient.performRequest(indexRequest);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("responseBody = " + responseBody);
// 批量寫入資料
Random random = new Random();
Request bulkRequest = new Request("POST", "/_bulk");
StringBuilder bulkJsonBuilder = new StringBuilder();
for (int i = 0; i < 5000; i++) {
// 請將field和value替換為實際業務欄位與值
bulkJsonBuilder.append("{\"index\":{\"_index\":\"").append(indexName).append("\",\"_id\":\"").append(i).append("\"}}").append("\n");
String value = random.nextInt() + "";
bulkJsonBuilder.append("{\"field1\":\"").append(value).append("\",\"field2\":\"").append(value).append("\"}").append("\n");
}
bulkRequest.setJsonEntity(bulkJsonBuilder.toString());
response = restClient.performRequest(bulkRequest);
// 發送重新整理請求,強制已寫資料可見
response = restClient.performRequest(new Request("POST", "/" + indexName + "/_refresh"));
responseBody = EntityUtils.toString(response.getEntity());
System.out.println("responseBody = " + responseBody);
// 查詢索引中所有資料,預設最多返回10條查詢結果
response = restClient.performRequest(new Request("GET", "/" + indexName + "/_search"));
responseBody = EntityUtils.toString(response.getEntity());
System.out.println("responseBody = " + responseBody);
// 查詢索引中文檔id為"0"的資料
response = restClient.performRequest(new Request("GET", "/" + indexName + "/_doc/0"));
responseBody = EntityUtils.toString(response.getEntity());
System.out.println("responseBody = " + responseBody);
// 刪除索引
response = restClient.performRequest(new Request("DELETE", "/" + indexName));
responseBody = EntityUtils.toString(response.getEntity());
System.out.println("responseBody = " + responseBody);
} catch (Exception e) {
System.out.println("msg: " + e.getMessage());
}
}
}
執行成功後將返回如下結果:
responseBody = {"acknowledged":true,"shards_acknowledged":true,"index":"lindorm_index"}
responseBody = {"_shards":{"total":1,"successful":1,"failed":0}}
responseBody = {"took":6,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":5000,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"lindorm_index","_id":"0","_score":1.0,"_source":{"field1":"2127821774","field2":"2127821774"}},{"_index":"lindorm_index","_id":"1","_score":1.0,"_source":{"field1":"1820331840","field2":"1820331840"}},{"_index":"lindorm_index","_id":"2","_score":1.0,"_source":{"field1":"-388903172","field2":"-388903172"}},{"_index":"lindorm_index","_id":"3","_score":1.0,"_source":{"field1":"-68048869","field2":"-68048869"}},{"_index":"lindorm_index","_id":"4","_score":1.0,"_source":{"field1":"1865955199","field2":"1865955199"}},{"_index":"lindorm_index","_id":"5","_score":1.0,"_source":{"field1":"1088273523","field2":"1088273523"}},{"_index":"lindorm_index","_id":"6","_score":1.0,"_source":{"field1":"-1529281106","field2":"-1529281106"}},{"_index":"lindorm_index","_id":"7","_score":1.0,"_source":{"field1":"-1185412160","field2":"-1185412160"}},{"_index":"lindorm_index","_id":"8","_score":1.0,"_source":{"field1":"-1243760053","field2":"-1243760053"}},{"_index":"lindorm_index","_id":"9","_score":1.0,"_source":{"field1":"1066058716","field2":"1066058716"}}]}}
responseBody = {"_index":"lindorm_index","_id":"0","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{"field1":"2127821774","field2":"2127821774"}}
responseBody = {"acknowledged":true}