All Products
Search
Document Center

Demo code for committing data

Last Updated: Sep 09, 2021

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.

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 data is to be uploaded";
    private static String tableName = "The name of the table to which data is to be uploaded";
    private static String accesskey = "The AccessKey ID of your Alibaba Cloud account";
    private static String secret = "The AccessKey secret of your Alibaba Cloud account";
    private static String host = "The endpoint of the OpenSearch API in your region";

    public static void main(String[] args) {

            // 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 = "Add 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", "13712345678");

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

            String[] literal_arr = {"Add doc1 in commit mode","Test adding 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);

            // Define a DocumentClient object that is used to add 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, 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 newly-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 to specify the 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", "13712342222");

            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 add operation is equivalent to an update, because the specified document name and primary key value are consistent with those of doc1.
            documentClient1.add(doc2);

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


            try {
                // Call the commit method to submit the updated document. In this example, 10 seconds are reserved for the submit operation to be complete. 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 newly-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 only to 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 returned in the search results. Hibernate the thread for at least one second.
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            //-------------Query document data after the deletion.-----------------
            // 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 return results.
            Config config = new Config(Lists.newArrayList(appName));
            config.setStart(0);
            config.setHits(30);
            // Specify the data format of return 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();
            }

        }
}