All Products
Search
Document Center

Demo code for pushing data

Last Updated: Sep 09, 2021

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, 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 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.

Scenarios

  • 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 = "The name of the OpenSearch application to which data is to be uploaded";
    private String tableName = "The name of the table to which data is to be uploaded";
    private String accesskey = "The AccessKey ID of your Alibaba Cloud account";
    private String secret = "The AccessKey secret of your Alibaba Cloud account";
    private 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()));

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

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

            String[] literal_arr = {"Add doc1 in push mode","Test adding doc1 in push mode"};
            doc1.put("literal_arr", literal_arr);

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

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

            String[] literal_arr = {"Add doc2 in push mode","Test adding 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 with 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", "13712343333");

            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 with 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 only to 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);// Add doc1.
            docsJsonArr.put(json2);// Add 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 one 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 and use the config clause to set the paging-related parameters or the 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);

            // 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 a filter condition.
            searchParams.setFilter("cate_id<=3");

            // Specify the 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();
            }

        }
}