All Products
Search
Document Center

OpenSearch:Demo code for committing data

Last Updated:Aug 14, 2023

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

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

  • For 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 required permissions are granted to the AliyunServiceRoleForOpenSearch role by using your Alibaba Cloud account. For more information, see AliyunServiceRoleForOpenSearch and Access authorization rules.

  • We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure.

  • Linux and macOS

    Run the following commands. Replace <access_key_id> and <access_key_secret> with the AccessKey ID and AccessKey secret of the RAM user that you use.

    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 set the environment variables to your AccessKey ID and AccessKey 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 must first dynamically encapsulate the document data to be uploaded into Map objects. Then, call the add method to upload the Map objects to the client buffer. Finally, call the commit method to submit the Map objects.

Scenario

  • 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 = "Name of the OpenSearch application for which you want to commit data";
    private static String tableName = "Name of the table to which data is to be uploaded";
    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 the environment variables before you run the sample 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");
      			
            // Obtain the file encoding format and 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";// 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 add method to upload the document to the client buffer.
            documentClient1.add(doc1);

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

            try {

                // Call the commit method to submit the added document. In this example, only one document is submitted at a time and you can view the result after 10 seconds. 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 new 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 doc1 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, only one document is submitted at a time and you can view the result after 10 seconds. 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 can 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 returned 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 and use the config clause to configure parameters 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

When you commit data, you can only commit fields in the same table and cannot commit fields in different tables.