Commit push demo

更新时间:
复制 MD 格式

Use the OpenSearch SDK for Java V3.1 to add, update, and delete documents in commit mode. In commit mode, you buffer one or more documents in a client-side queue and then submit the entire batch with a single commit call.

Use cases

  • Dynamically merge and commit data from multiple sources

  • Commit a single document in real time

  • Commit a small batch of documents at a time

Important

Commit mode only supports fields within the same table. You cannot commit fields from different tables in a single call.

Prerequisites

Before you begin, ensure that you have:

  • An OpenSearch application with a configured table

  • The application name (appName), table name (tableName), and OpenSearch API endpoint (host) for your region

  • An AccessKey ID and AccessKey secret from a Resource Access Management (RAM) user with the required OpenSearch permissions. For setup steps, see Create a RAM user and Access authorization rules

Important

The AccessKey pair of your Alibaba Cloud account has access to all API operations. Use a RAM user's AccessKey pair instead, and grant that RAM user the AliyunServiceRoleForOpenSearch role. Never store credentials directly in source code.

Set environment variables

Set your credentials as environment variables so the sample code can read them at runtime.

Linux and macOS

Replace <access_key_id> and <access_key_secret> with the RAM user's AccessKey ID and AccessKey secret, then run:

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 and add ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET with the corresponding values.

  2. Restart Windows for the changes to take effect.

How it works

Each operation follows the same three-step pattern:

  1. Encapsulate document data into a Map object.

  2. Call add(), update(), or remove() to stage the document in the client buffer.

  3. Call commit(appName, tableName) to submit all buffered documents to OpenSearch.

After a successful commit, check the OpenSearch console error logs to confirm the data was indexed without field-level errors (such as type conversion failures).

Add a document

Create a Map object with the document fields, stage it with add(), and submit it with commit().

package com.aliyun.opensearch;

import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Maps;
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 java.io.UnsupportedEncodingException;
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) {

        // Read credentials from environment variables
        String accesskey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String secret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

        // Print the file encoding and default charset for debugging
        System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
        System.out.println(String.format("defaultCharset: %s", java.nio.charset.Charset.defaultCharset().name()));

        // Use a random integer as the document's primary key
        Random rand = new Random();
        int value = rand.nextInt(Integer.MAX_VALUE);

        // Build the document as a Map object
        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);

        // Initialize the SDK client chain: OpenSearch -> OpenSearchClient -> DocumentClient
        OpenSearch openSearch1 = new OpenSearch(accesskey, secret, host);
        OpenSearchClient serviceClient1 = new OpenSearchClient(openSearch1);
        DocumentClient documentClient1 = new DocumentClient(serviceClient1);

        // Stage the document in the client buffer
        documentClient1.add(doc1);
        System.out.println(doc1.toString());

        try {
            // Submit all buffered documents. You can buffer multiple documents before calling commit.
            OpenSearchResult osr = documentClient1.commit(appName, tableName);
            checkCommitResult(osr);
        } catch (OpenSearchException | OpenSearchClientException e) {
            e.printStackTrace();
        }

        // Wait 10 seconds before querying -- OpenSearch indexes data asynchronously
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // Check whether the commit was accepted without transport-level errors.
    // A "true" result means the request was received successfully.
    // Field-level errors (such as type conversion failures) are reported separately
    // in the OpenSearch console error logs.
    private static void checkCommitResult(OpenSearchResult osr) {
        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());
        }
    }
}

Update a document

To update a document, build a new Map with the same primary key and the updated field values, then call update() followed by commit().

// Build the updated document -- must include the same primary key as the original
Map<String, Object> doc2 = Maps.newLinkedHashMap();
doc2.put("id", value); // same primary key as doc1

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);

// Stage the update in the client buffer
documentClient1.update(doc2);
System.out.println(doc2.toString());

try {
    OpenSearchResult osr = documentClient1.commit(appName, tableName);
    checkCommitResult(osr);
} catch (OpenSearchException | OpenSearchClientException e) {
    e.printStackTrace();
}

// Wait 10 seconds before querying
try {
    Thread.sleep(10000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

Delete a document

To delete a document, build a Map with only the primary key, then call remove() followed by commit().

// Only the primary key is required to identify the document for deletion
Map<String, Object> doc3 = Maps.newLinkedHashMap();
doc3.put("id", value);

// Stage the deletion in the client buffer
documentClient1.remove(doc3);
System.out.println(doc3.toString());

try {
    OpenSearchResult osr = documentClient1.commit(appName, tableName);
    checkCommitResult(osr);
} catch (OpenSearchException | OpenSearchClientException e) {
    e.printStackTrace();
}

// Wait at least 1 second (10 seconds recommended) before querying.
// Querying immediately after deletion may still return the deleted document.
try {
    Thread.sleep(10000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

Verify the result

After each commit, query OpenSearch to confirm the expected data is present or absent.

import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
import com.aliyun.opensearch.sdk.dependencies.org.json.JSONObject;
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;

// Initialize a separate client for search
OpenSearch openSearch2 = new OpenSearch(accesskey, secret, host);
OpenSearchClient serviceClient2 = new OpenSearchClient(openSearch2);
SearcherClient searcherClient2 = new SearcherClient(serviceClient2);

// Configure paging and response format
// Supported formats: XML, JSON. FULLJSON is not supported.
Config config = new Config(Lists.newArrayList(appName));
config.setStart(0);
config.setHits(30);
config.setSearchFormat(SearchFormat.JSON);

// Query by primary key
SearchParams searchParams = new SearchParams(config);
searchParams.setQuery("id:'" + value + "'");

try {
    SearchResult searchResult = searcherClient2.execute(searchParams);
    String result = searchResult.getResult();
    JSONObject obj = new JSONObject(result);
    System.out.println("Query result: " + obj.toString());
} catch (OpenSearchException | OpenSearchClientException e) {
    e.printStackTrace();
}

What's next