Data push

更新时间:
复制 MD 格式

Use the OpenSearch Retrieval Engine Edition JDBC SDK to push documents to your instance in real time. The SDK supports two operations: uploading and deleting documents.

Prerequisites

Before you begin, ensure that you have:

  • An OpenSearch Retrieval Engine Edition instance

  • The API endpoint, username, and password from the API Endpoint section of the Instance Details page

  • Added the com.aliyun.ha3engine.jdbc driver to your project

Establish a connection

All examples share the same connection setup. Configure the driver with your instance credentials:

import java.sql.*;
import java.util.Properties;

private static Connection connection;

@Before
public void initConnection() throws ClassNotFoundException, SQLException {
    String name = "com.aliyun.ha3engine.jdbc.Ha3Driver";
    Properties props = new Properties();
    // API endpoint — find this in the API Endpoint section of the Instance Details page
    props.setProperty("serviceName", "ha-cn-i7*****605.public.ha.aliyuncs.com");
    // Username — find this in the API Endpoint section of the Instance Details page
    props.setProperty("username", "<your-username>");
    // Password — set this in the API Endpoint section of the Instance Details page
    props.setProperty("password", "<your-password>");
    props.setProperty("enableDynamicParams", "true");
    props.setProperty("enableDetailLog", "true");

    Class.forName(name);
    connection = DriverManager.getConnection("jdbc:ha3://", props);
}
PropertyDescription
serviceNameAPI endpoint of the instance
usernameUsername for authentication
passwordPassword for authentication
enableDynamicParamsSet to true to enable dynamic parameters
enableDetailLogSet to true to enable detail logging

Upload documents

Use INSERT statements to add documents to your index.

Method 1: Statement

The first field in the INSERT statement must be the primary key field.

import java.sql.*;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;

public class Push {

    private static Connection connection;

    @Before
    public void initConnection() throws ClassNotFoundException, SQLException {
        // See "Establish a connection" above
    }

    @Test
    public void push1() throws SQLException {
        // First field in the SQL statement must be the primary key field
        Statement statement = connection.createStatement();
        int count = statement.executeUpdate(
            "insert into `test1` (`id`,`title`,`body`,`count`,`price`) values\n" +
            "    (1, 'Test title 1', 'Test text', 23, 28.55),\n" +
            "    (2, 'Test title 2', 'Test text', 15, 26.77);"
        );
        System.out.println("insert count: " + count);
    }
}

Method 2: PreparedStatement

Use PreparedStatement with parameterized values. Pass a pk param via setObject(0, params) to explicitly specify the primary key field. If you omit this parameter, the first field in the SQL statement is used as the primary key.

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;

public class Push {

    private static Connection connection;

    @Before
    public void initConnection() throws ClassNotFoundException, SQLException {
        // See "Establish a connection" above
    }

    @Test
    public void push2() throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement(
            "insert into test1 (`id`,`title`,`body`,`count`,`price`) values\n" +
            "    (3, ?, 'Test text', 37, 26.55),\n" +
            "    (4, ?, 'Test text', 6, 26.55);"
        );
        preparedStatement.setString(1, "Test title 3");
        preparedStatement.setString(2, "Test title 4");

        // Explicitly specify the primary key field
        Map<String, Object> params = new HashMap<>();
        params.put("pk", "id");
        preparedStatement.setObject(0, params);

        int count = preparedStatement.executeUpdate();
        System.out.println("insert count: " + count);
    }
}

Delete documents

Use DELETE statements to remove documents by primary key. You can only filter on the primary key field using id = or id in syntax.

Filtering on non-primary-key fields in a DELETE statement is not supported.

Method 1: Statement with id =

import java.sql.*;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;

public class Delete {

    private static Connection connection;

    @Before
    public void initConnection() throws ClassNotFoundException, SQLException {
        // See "Establish a connection" above
    }

    @Test
    public void delete1() throws SQLException {
        Statement statement = connection.createStatement();
        int count = statement.executeUpdate("delete from `test1` where `id` = 4");
        System.out.println("delete count: " + count);
    }
}

Method 2: Statement with id in

    @Test
    public void delete2() throws SQLException {
        Statement statement = connection.createStatement();
        int count = statement.executeUpdate("delete from `test1` where `id` in (2,3)");
        System.out.println("delete count: " + count);
    }

Method 3: PreparedStatement with id =

    @Test
    public void delete3() throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement(
            "delete from `test1` where `id` = 1");
        int count = preparedStatement.executeUpdate();
        System.out.println("delete count: " + count);
    }

Method 4: PreparedStatement with id in

Use ? placeholders to pass dynamic IDs:

    @Test
    public void delete4() throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement(
            "delete from `test1` where `id` in (2, ?, 4)");
        preparedStatement.setInt(1, 3);
        int count = preparedStatement.executeUpdate();
        System.out.println("delete count: " + count);
    }