全部产品
Search
文档中心

云原生多模数据库 Lindorm:Java Low Level REST Client

更新时间:Jun 26, 2024

Java Low Level REST Client是Elasticsearch官方提供的低级别REST客户端,其API不负责数据的编码与解码。Lindorm搜索引擎兼容Elasticsearch 7.10及更早版本的功能,如果您想要自定义请求和响应处理方式,可以通过Java Low Level REST Client连接并使用搜索引擎。

前提条件

  • 已安装Java环境,要求安装JDK 1.8及以上版本。

  • 已开通搜索引擎。如何开通,请参见开通指南

  • 已将客户端IP地址添加至Lindorm白名单,具体操作请参见设置白名单

操作步骤

  1. 安装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>
  2. 配置连接参数。

    //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

  3. 使用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}