All Products
Search
Document Center

OpenSearch:Demo code for committing data

Last Updated:Mar 31, 2025

Configure environment variables

This section describes how to configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your operating system.

Important
  • The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. We recommend that you use the AccessKey pair of a Resource Access Management (RAM) user to call API operations or perform routine O&M. For more information, see Create a RAM user.

  • For more information about how to create an AccessKey pair, see Create an AccessKey pair.

  • If you use the AccessKey pair of a RAM user, make sure that the Alibaba Cloud account has granted the permissions specified in the service-linked role AliyunServiceRoleForOpenSearch to the RAM user. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.

  • We recommend that you do not hard-code the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account is compromised.

  • Linux and macOS:

    In the following commands, replace <access_key_id> with the AccessKey ID of your RAM user and <access_key_secret> with the AccessKey secret of your RAM user. Then, run the commands.

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> 
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows:

    1. Create an environment variable file, add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the file, and then specify your AccessKey ID for ALIBABA_CLOUD_ACCESS_KEY_ID and your AccessKey secret for ALIBABA_CLOUD_ACCESS_KEY_SECRET.

    2. Restart Windows for the AccessKey pair to take effect.

Demo code for committing data by using OpenSearch SDK for Java V3.1

To upload data in commit mode, you can perform the following operations: Dynamically encapsulate the document data to be uploaded into Map objects, call the add method to upload the Map objects to the client buffer, and then call the commit method to submit the Map objects.

Scenarios

  • Dynamically merge and commit data

  • Commit a single document

  • Commit a small number of documents at a time

package com.aliyun.opensearch;

import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Maps;
import com.aliyun.opensearch.sdk.dependencies.org.json.JSONObject;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;
import com.aliyun.opensearch.sdk.generated.search.Config;
import com.aliyun.opensearch.sdk.generated.search.SearchFormat;
import com.aliyun.opensearch.sdk.generated.search.SearchParams;
import com.aliyun.opensearch.sdk.generated.search.general.SearchResult;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Random;

public class testCommitSearch {

  private static String appName = "The name of the OpenSearch application to which you want to commit data";
  private static String tableName = "The name of the table to which you want to commit data";
  private static String host = "Endpoint of the OpenSearch API in your region";

  public static void main(String[] args) {
    // Specify your AccessKey pair.
    // Obtain the AccessKey ID and AccessKey secret from environment variables. 
    // You must configure environment variables before you run this code. For more information, see the "Configure environment variables" section of this topic.
    String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
    String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

    // View documents and the default encoding format.
    System.out.println(
      String.format("file.encoding: %s", System.getProperty("file.encoding"))
    );
    System.out.println(
      String.format("defaultCharset: %s", Charset.defaultCharset().name())
    );

    // Generate a random value and use the value as the value of the primary key.
    Random rand = new Random();
    int value = rand.nextInt(Integer.MAX_VALUE);

    // Create a Map object named doc1 to store the data of the document to be uploaded.
    Map<String, Object> doc1 = Maps.newLinkedHashMap();
    doc1.put("id", value);

    String title_string = "Upload doc1 in commit mode"; // The string is encoded in UTF-8.
    byte[] bytes;
    try {
      bytes = title_string.getBytes("utf-8");
      String utf8_string = new String(bytes, "utf-8");
      doc1.put("name", utf8_string);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    doc1.put("phone", "1381111****");

    int[] int_arr = { 33, 44 };
    doc1.put("int_arr", int_arr);

    String[] literal_arr = {
      "Upload doc1 in commit mode",
      "Test uploading doc1 in commit mode]",
    };
    doc1.put("literal_arr", literal_arr);

    float[] float_arr = { (float) 1.1, (float) 1.2 };
    doc1.put("float_arr", float_arr);

    doc1.put("cate_id", 1);

    // Create an OpenSearch object.
    OpenSearch openSearch1 = new OpenSearch(accesskey, secret, host);

    // Use the OpenSearch object as a parameter to create an OpenSearchClient object.
    OpenSearchClient serviceClient1 = new OpenSearchClient(openSearch1);

    // Create a DocumentClient object that is used to upload and submit data.
    DocumentClient documentClient1 = new DocumentClient(serviceClient1);

    // Call the method to upload doc1 to the client buffer and set it as the new document.
    documentClient1.add(doc1);

    // Display the document.
    System.out.println(doc1.toString());

    try {
      // Call the commit method to submit the uploaded document. In this example, 10 seconds are reserved for the submit operation to be complete. You can also upload multiple documents to the client buffer and then submit the documents at a time.
      OpenSearchResult osr = documentClient1.commit(appName, tableName);

      // Whether the data is committed depends on whether an error occurs when you commit the data and whether an error occurs in the OpenSearch console.
      // After you commit the data, errors may occur in the application. The OpenSearch console records these errors in error logs, such as failures of field content conversion.
      if (osr.getResult().equalsIgnoreCase("true")) {
        System.out.println(
          "No error occurred when the data is committed! \n The request ID is "+
          osr.getTraceInfo().getRequestId()
        );
      } else {
        System.out.println("An error occurred when the data is committed!" + osr.getTraceInfo());
      }
    } catch (OpenSearchException e) {
      e.printStackTrace();
    } catch (OpenSearchClientException e) {
      e.printStackTrace();
    }

    try {
      Thread.sleep(10000);// Hibernate the thread for 10 seconds, after which you can view the added data in the OpenSearch console.
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // Create a Map object named doc2 to update doc1. Call the add method to perform this update and make sure that you specify the valid name and primary key value of doc1.
    Map<String, Object> doc2 = Maps.newLinkedHashMap();
    doc2.put("id", value);

    String title_string2 = "Update doc2 in commit mode";// utf-8
    byte[] bytes2;
    try {
      bytes2 = title_string2.getBytes("utf-8");
      String utf8_string2 = new String(bytes2, "utf-8");
      doc2.put("name", utf8_string2);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    doc2.put("phone", "1390000****");

    int[] int_arr2 = { 22, 22 };
    doc2.put("int_arr", int_arr2);

    String[] literal_arr2 = {"Update doc1 in commit mode","Update doc1 in commit mode"};
    doc2.put("literal_arr", literal_arr2);

    float[] float_arr2 = { (float) 1.1, (float) 1.2 };
    doc2.put("float_arr", float_arr2);

    doc2.put("cate_id", 1);

    // Call the add method to upload the Map object doc2 to the client buffer. This update operation can be executed normally, because the primary key is valid.
    documentClient1.update(doc2);

    // Display the document.
    System.out.println(doc2.toString());
    try {
      // Call the commit method to submit the updated document. In this example, only one document is submitted at a time and you can view the result after 10 seconds. You can also update multiple documents in the client buffer and then submit the documents at a time.
      OpenSearchResult osr = documentClient1.commit(appName, tableName);

      // Whether the data is committed depends on whether an error occurs when you commit the data and whether an error occurs in the OpenSearch console.
      // After you commit the data, errors may occur in the application. The OpenSearch console records these errors in error logs, such as failures of field content conversion.
      if (osr.getResult().equalsIgnoreCase("true")) {
        System.out.println(
          "No error occurred when the data is committed! \n The request ID is "+
          osr.getTraceInfo().getRequestId()
        );
      } else {
        System.out.println("An error occurred when the data is committed!" + osr.getTraceInfo());
      }
    } catch (OpenSearchException e) {
      e.printStackTrace();
    } catch (OpenSearchClientException e) {
      e.printStackTrace();
    }

    try {
      Thread.sleep(10000);// Hibernate the thread for 10 seconds, after which you can view the updated data in the OpenSearch console.
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // Create a Map object named doc3 to delete the document. To delete a document, you need to only specify the primary key value of the document.
    Map<String, Object> doc3 = Maps.newLinkedHashMap();
    doc3.put("id", value);

    // Upload the Map object doc3 to the client buffer to delete the document.
    documentClient1.remove(doc3);
    // Display the document.
    System.out.println(doc3.toString());

    try {
      // Call the commit method to submit the deleted document. In this example, 10 seconds are reserved for the submit operation to be complete. You can also delete multiple documents from the client buffer and then submit the deleted documents at a time.
      OpenSearchResult osr = documentClient1.commit(appName, tableName);

      // Whether the data is committed depends on whether an error occurs when you commit the data and whether an error occurs in the OpenSearch console.
      // After you commit the data, errors may occur in the application. The OpenSearch console records these errors in error logs, such as failures of field content conversion.
      if (osr.getResult().equalsIgnoreCase("true")) {
        System.out.println(
          "No error occurred when the data is committed! \n The request ID is "+
          osr.getTraceInfo().getRequestId()
        );
      } else {
        System.out.println("An error occurred when the data is committed!" + osr.getTraceInfo());
      }
    } catch (OpenSearchException e) {
      e.printStackTrace();
    } catch (OpenSearchClientException e) {
      e.printStackTrace();
    }

    try {
      Thread.sleep(10000);// Hibernate the thread for 10 seconds, after which you check whether the document is deleted. If you do not hibernate the thread and query data immediately after the deletion, the data to be deleted may be included in the search results. Hibernate the thread for at least one second.
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // Create an OpenSearch object.
    OpenSearch openSearch2 = new OpenSearch(accesskey, secret, host);

    // Use the OpenSearch object as a parameter to create an OpenSearchClient object.
    OpenSearchClient serviceClient2 = new OpenSearchClient(openSearch2);

    // Use the OpenSearchClient object as a parameter to create a SearcherClient object.
    SearcherClient searcherClient2 = new SearcherClient(serviceClient2);

    // Create a Config object to configure parameters of the config clause, such as the paging-related parameters and data format of returned results.
    Config config = new Config(Lists.newArrayList(appName));
    config.setStart(0);
    config.setHits(30);
    // Specify the data format of returned results. Supported formats are XML and JSON. The FULLJSON format is not supported. In this example, the data format is set to JSON.
    config.setSearchFormat(SearchFormat.JSON);

    SearchParams searchParams = new SearchParams(config);
    searchParams.setQuery("id:'" + value + "'");
    // Run the query and return the results.
    SearchResult searchResult;
    try {
      searchResult = searcherClient2.execute(searchParams);
      String result = searchResult.getResult();
      JSONObject obj = new JSONObject(result);

      // Display the search results.
      System.out.println("Query debugging results:" + obj.toString());
    } catch (OpenSearchException e) {
      e.printStackTrace();
    } catch (OpenSearchClientException e) {
      e.printStackTrace();
    }
  }
}

Important

You can commit data only to fields in the same table but cannot commit data across tables.