All Products
Search
Document Center

OpenSearch:Demo code for pushing data

Last Updated:Aug 31, 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 pushing data by using OpenSearch SDK for Java V3.1

To upload data in push mode, you must first generate datasets in the valid format and upload the datasets to the client buffer. Then, call the push method to submit the datasets to the application at a time.

Usage notes

  • Datasets that can be pushed must be in the valid format. To view the valid format, log on to the OpenSearch console, go to the Instance Management page, choose More > Upload File in the Actions column for an application, and then download the sample file. You can use the sample file as a template to generate your datasets.

  • You can also generate datasets by using the JSONObject and JSONArray objects and call the push method to submit the datasets to the application at a time.

  • If the number of documents to be pushed at a time exceeds the limit, an error occurs and the push fails.

Scenario

  • Dynamically merge and push data

  • Push a single document

  • Generate datasets in advance and then push the datasets at a time

  • Push multiple 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.JSONArray;
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.document.Command;
import com.aliyun.opensearch.sdk.generated.document.DocumentConstants;
import com.aliyun.opensearch.sdk.generated.search.*;
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 testPushSearch2 {

    private String appName = "Name of the OpenSearch application that you want to manage";
    private String tableName = "Name of the table to which data is to be uploaded";
    private 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 the environment variables. You must configure the environment variables before you run the sample code.
      			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()));

            //-------------The following demo code shows how to push data.-----------------
            // Generate a random value and use the value as the value of the primary key.
            Random rand = new Random();
            int value1 = rand.nextInt(Integer.MAX_VALUE);
            int value2 = rand.nextInt(Integer.MAX_VALUE);

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

            String title_string = "Upload doc1 in push 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", "137****1111");

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

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

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

            doc1.put("cate_id", 1);

            // Standard applications do not support the UPDATE operation or partial updates of documents. You can perform the ADD operation to add documents or perform full updates of documents. Advanced applications support the UPDATE operation and partial updates of documents. 
            JSONObject json1 = new JSONObject();
            json1.put(DocumentConstants.DOC_KEY_CMD, Command.ADD.toString());
            json1.put(DocumentConstants.DOC_KEY_FIELDS, doc1);

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

            String title_string2 = "Upload doc2 in push 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", "137****2222");

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

            String[] literal_arr2 = {"Upload doc2 in push mode","Test uploading doc2 in push mode"};
            doc2.put("literal_arr", literal_arr2);

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

            doc2.put("cate_id", 1);

            // The ADD operation can be performed to add or update data. The UPDATE operation is not supported. If the specified document in the ADD operation has the same primary key value as an existing document, the existing document is updated. Otherwise, the specified document is added. In this example, the ADD operation is performed to add a document.
            JSONObject json2 = new JSONObject();
            json2.put(DocumentConstants.DOC_KEY_CMD, Command.ADD.toString());
            json2.put(DocumentConstants.DOC_KEY_FIELDS, doc2);

            // Create a Map object named doc3 to store the data of the third document to be uploaded. This document is used to update doc2.
            Map<String, Object> doc3 = Maps.newLinkedHashMap();
            doc3.put("id", value2);

            String title_string3 = "Update doc2 to doc3 in push mode";// UTF-8.
            byte[] bytes3;
            try {

                bytes3 = title_string3.getBytes("utf-8");
                String utf8_string3 = new String(bytes3, "utf-8");
                doc3.put("name", utf8_string3);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            doc3.put("phone", "137****3333");

            int[] int_arr3 = {33,33};
            doc3.put("int_arr", int_arr3);

            String[] literal_arr3 = {"Update doc2 to doc3 in push mode","Update doc2 to doc3 in push mode"};
            doc3.put("literal_arr", literal_arr3);

            float[] float_arr3 = {(float)3.3,(float)3.3};
            doc3.put("float_arr", float_arr3);

            doc3.put("cate_id", 1);

            // The ADD operation can be performed to add or update data. The UPDATE operation is not supported. If the specified document in the ADD operation has the same primary key value as an existing document, the existing document is updated. Otherwise, the specified document is added. In this example, the ADD operation is performed to update a document.
            JSONObject json3 = new JSONObject();
            json3.put(DocumentConstants.DOC_KEY_CMD, Command.ADD.toString());
            json3.put(DocumentConstants.DOC_KEY_FIELDS, doc3);

            // Create a Map object named doc4 to store the data of the fourth document to be uploaded. This document is used to delete doc1. To delete a document, you need to only specify the primary key value of the document.
            Map<String, Object> doc4 = Maps.newLinkedHashMap();
            doc4.put("id", value1);

            // Delete doc1.
            JSONObject json4 = new JSONObject();
            json4.put(DocumentConstants.DOC_KEY_CMD, Command.DELETE.toString());
            json4.put(DocumentConstants.DOC_KEY_FIELDS, doc4);

            JSONArray docsJsonArr = new JSONArray();
            docsJsonArr.put(json1);// Upload doc1.
            docsJsonArr.put(json2);// Upload doc2.
            docsJsonArr.put(json3);// Update doc2 to doc3.
            docsJsonArr.put(json4);// Delete doc1.
            String docsJson = docsJsonArr.toString();

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

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

            // Define a DocumentClient object, add a document in the form of a JSON string, and then submit the document.
            DocumentClient documentClient = new DocumentClient(serviceClient);

            try {
                // Call the push method to upload the data.
                OpenSearchResult osr = documentClient.push(docsJson, appName, tableName);

                // Whether the data is pushed depends on whether an error occurs when you push the data and whether an error occurs in the OpenSearch console.
                // After you push 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 pushed! \n The request ID is " + osr.getTraceInfo().getRequestId());

                }else{
                    System.out.println("An error occurred when the data is pushed! "+osr.getTraceInfo());
                }

            } catch (OpenSearchException e) {
                e.printStackTrace();
            } catch (OpenSearchClientException e) {
                e.printStackTrace();
            }

            try {
                Thread.sleep(1000);// Hibernate the thread for 1 second.
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //-------------Query the data in the pushed document.-----------------
            // Use the OpenSearchClient object as a parameter to create a SearcherClient object.
            SearcherClient searcherClient = new SearcherClient(serviceClient);

            // Create a Config object that is used to specify the paging-related parameters or the data format of returned results in the config clause.
            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);

            // Specify the fields to be returned in search results.
            config.setFetchFields(Lists.newArrayList("id", "name", "phone", "int_arr", "literal_arr", "float_arr", "cate_id"));

            // Create a SearchParams object.
            SearchParams searchParams = new SearchParams(config);

            // Specify the query clause. You can specify multiple keywords to perform a query based on multiple index fields. In this case, you must specify the index fields in one setQuery call. If you specify each index field in a separate setQuery call, the last clause overwrites the preceding clauses.
            searchParams.setQuery("id:'" + value1 + "'|'"+value2 +"'");

            // Specify filter conditions.
            searchParams.setFilter("cate_id<=3");

            // Specify a sorting condition.
            Sort sorter = new Sort();
            sorter.addToSortFields(new SortField("id", Order.DECREASE)); // Specify a field based on which documents are to be sorted, and a sorting method. In this example, documents are sorted based on the id field in descending order.
            sorter.addToSortFields(new SortField("RANK", Order.INCREASE)); // If multiple documents have the same value of the id field, sort the documents based on the RANK field in ascending order.

            // Add the Sort object as a query parameter.
            searchParams.setSort(sorter);

            // Run the query and return the results.
            SearchResult searchResult;
            try {
                searchResult = searcherClient.execute(searchParams);

                String result = searchResult.getResult();
                JSONObject obj = new JSONObject(result);

                // Display the search results.
                System.out.println(obj.toString());

            } catch (OpenSearchException e) {
                e.printStackTrace();
            } catch (OpenSearchClientException e) {
                e.printStackTrace();
            }

        }
}

Important

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