All Products
Search
Document Center

AnalyticDB:Write data by using the Client SDK

Last Updated:Mar 28, 2026

The AnalyticDB for PostgreSQL Client SDK provides a high-performance interface for writing data to AnalyticDB for PostgreSQL. It manages connection pooling, caching, and parallel processing internally, so you can focus on your data pipeline without managing infrastructure.

Note

The primary function of the AnalyticDB for PostgreSQL Client SDK is to efficiently write the data that you provide. The SDK does not read or process raw data.

When to use the Client SDK

Use the following table to choose the right tool for your use case:

ScenarioRecommended tool
High-throughput bulk writes (ETL pipelines, data sync)Client SDK (this page)
OLAP queries, DDL, and all other SQL operationsJDBC via getConnection()

The SDK outperforms direct COPY or INSERT for high-volume writes because its internal parallel processing batches and commits data efficiently. For SQL operations other than bulk writes, retrieve a JDBC connection from the SDK using getConnection() and use standard JDBC interfaces.

Prerequisites

Before you begin, ensure that you have:

  • An AnalyticDB for PostgreSQL instance with a connection address and port

  • A database user with write permissions on the target tables

  • Maven or a compatible Java build tool

Install the SDK

Maven

Add the following dependency to your pom.xml:

<dependency>
  <groupId>com.alibaba.cloud.analyticdb</groupId>
  <artifactId>adb4pgclient</artifactId>
  <version>1.0.16</version>
</dependency>

The SDK depends on the following packages. If version conflicts occur, check and resolve these versions:

Offline JAR

Download the offline JAR packages directly: adb4pgclient-1.0.16.jar and adb4pgclient-1.0.16-jar-with-dependencies.jar.

Quick start

The following example shows the complete write workflow: configure the connection, add rows to the buffer, and commit.

public class Adb4pgClientUsage {
    public void demo() {
        // 1. Configure the connection
        DatabaseConfig databaseConfig = new DatabaseConfig();
        databaseConfig.setHost("100.100.100.100");   // Connection address of the instance
        databaseConfig.setPort(8888);                // Default: 5432
        databaseConfig.setUser("your user name");
        databaseConfig.setPassword("your password");
        databaseConfig.setDatabase("your database name");

        // 2. Specify the target tables and columns
        // Call addTable separately for tables in different schemas.
        // Pass null for the schema to use the default schema 'public'.
        List<String> tables = new ArrayList<String>();
        tables.add("your table name 1");
        tables.add("your table name 2");
        databaseConfig.addTable(tables, "table schema name");

        // Specify columns per table. Use "*" to write to all columns.
        List<String> columns = new ArrayList<String>();
        columns.add("column1");
        columns.add("column2");
        databaseConfig.setColumns(columns, "your table name 1", "table schema name");
        databaseConfig.setColumns(Collections.singletonList("*"), "your table name 2", "table schema name");

        // 3. Set optional write behavior (all settings must be configured before creating the client)
        databaseConfig.setEmptyAsNull(false);      // Whether to treat empty strings as null. Default: false.
        databaseConfig.setInsertIgnore(true);      // Whether to ignore rows that cause primary key conflicts. Default: false (conflicts overwrite existing rows).
        databaseConfig.setRetryTimes(3);           // Retry count on commit exception. Default: 3.
        databaseConfig.setRetryIntervalTime(1000); // Retry interval in milliseconds. Default: 1000.

        // 4. Initialize the client. No DatabaseConfig changes take effect after this point.
        Adb4pgClient adbClient = new Adb4pgClient(databaseConfig);

        // 5. Add rows to the buffer using setColumn (creates a new Row per record)
        for (int i = 0; i < 10; i++) {
            Row row = new Row(columns.size());
            row.setColumn(0, i);                   // Column index must match the column order set above
            row.setColumn(1, "string value");
            // If the buffer exceeds commitSize, an autocommit triggers before the new row is added.
            // Handle AdbClientException with error code COMMIT_ERROR_DATA_LIST if autocommit fails.
            adbClient.addRow(row, "your table name 1", "table schema name");
        }

        // Use updateColumn when you want to reuse the same Row instance across records
        Row row = new Row();
        row.setColumn(0, 10);
        row.setColumn(1, "2018-01-01 08:00:00");
        adbClient.addRow(row, "your table name 1", "table schema name");
        row.updateColumn(0, 11);
        row.updateColumn(1, "2018-01-02 08:00:00");
        adbClient.addRow(row, "your table name 1", "table schema name");

        // Add rows in Map format (column name to value)
        Map<String, String> rowMap = new HashMap<String, String>();
        rowMap.put("t1", "12");
        rowMap.put("t2", "string value");
        adbClient.addMap(rowMap, "your table name 2", "table schema name");

        // 6. Commit the buffer. Data is written to AnalyticDB for PostgreSQL only after a successful commit.
        try {
            adbClient.commit();
        } catch (Exception e) {
            // Handle exception: inspect the error code and decide whether to retry or log and skip
        } finally {
            adbClient.stop(); // Releases thread pools. Throws an exception if uncommitted data remains.
        }
    }
}

API reference

DatabaseConfig class

DatabaseConfig holds all connection and write behavior settings. Configure all parameters before passing it to Adb4pgClient. Settings cannot be changed after the client is initialized.

Required parameters

MethodDescription
setHost(String adbHost)Connection address of the AnalyticDB for PostgreSQL instance.
setPort(int port)Port of the instance. Default: 5432.
setDatabase(String database)Name of the database to connect to.
setUser(String username)Username for the database connection.
setPassword(String pwd)Password for the database connection.
addTable(List<String> table, String schema)Tables to write to, grouped by schema. Call this method multiple times for tables in different schemas. Has no effect after Adb4pgClient is constructed.
setColumns(List<String> columns, String tableName, String schemaName)Columns to write per table. Use columnList.add("*") to write to all columns. Must be set for every table in the table list.

Optional parameters

MethodDefaultDescription
setInsertIgnore(boolean insertIgnore)falseWhen false, rows that cause primary key conflicts overwrite existing rows. Set to true to skip conflicting rows instead.
setEmptyAsNull(boolean emptyAsNull)falseWhether to treat empty strings as null. Applies to all configured tables.
setParallelNumber(int parallelNumber)4Number of concurrent write threads. Changing this is not recommended in most cases.
setLogger(Logger logger)Logger for the client. Use slf4j.Logger.
setRetryTimes(int retryTimes)3Number of retries on commit exception.
setRetryIntervalTime(long retryIntervalTime)1000 msInterval between retries, in milliseconds.
setCommitSize(long commitSize)10 MBBuffer size that triggers an autocommit, in bytes. Changing this is not recommended.
setEnablePreHash(boolean enablePreHash)Enable when the target table uses Beam storage and a deadlock occurs during writes. Improves write performance in this scenario.
setMetaDataSchedulerInterval(int metaDataSchedulerInterval)1 minInterval for metadata updates for the write table, in minutes.

Row class

Use Row to write data record by record.

MethodDescription
setColumn(int index, Object value)Sets a column value by index. Columns must be set in order. Row instances cannot be reused with this method — create a new Row for each record.
setColumnValues(List<Object> values)Writes an entire row from a list of values.
updateColumn(int index, Object value)Updates a column value by index. Row instances can be reused with this method — update the values in place and call addRow again.

Adb4pgClient class

Adb4pgClient is the main write interface. All write operations buffer data until commit() is called.

MethodDescription
addRow(Row row, String tableName, String schemaName) / addRows(List<Row> rows, String tableName, String schemaName)Adds one or more rows to the buffer. If the buffer exceeds commitSize, an autocommit triggers before the new data is added. If the autocommit fails, an AdbClientException with error code COMMIT_ERROR_DATA_LIST is thrown, containing the list of failed records.
addMap(Map<String, String> dataMap, String tableName, String schemaName) / addMaps(List<Map<String, String>> dataMaps, String tableName, String schemaName)Adds one or more rows in map format. Behaves the same as addRow for buffer management and autocommit.
commit()Commits all buffered data to AnalyticDB for PostgreSQL. Throws an exception with the failed statements if the commit fails.
TableInfo getTableInfo(String tableName, String schemaName)Returns the structure information of the specified table.
List<ColumnInfo> getColumnInfo(String tableName, String schemaName)Returns the list of columns for the specified table as ColumnInfo objects. Use columnInfo.isNullable() to check whether a column accepts null.
stop()Releases internal thread pools and resources. Throws an exception if uncommitted data exists in memory.
forceStop()Forcibly releases internal thread pools and resources. Any uncommitted data in memory is lost. Use only when a clean stop is not possible.
Connection getConnection() throws SQLExceptionRetrieves a JDBC Connection from the client's connection pool for non-COPY SQL operations. After use, release the associated ResultSet, Statement, and Connection objects.

ColumnInfo class

MethodDescription
boolean isNullable()Returns true if the column accepts null values.

Error codes

CodeNumberDescription
COMMIT_ERROR_DATA_LIST101Some records failed during commit. Use e.getErrData() to retrieve the failed records as List<String>. This error can occur during addMap(s), addRow(s), and commit. Handle it separately for each operation.
COMMIT_ERROR_OTHER102Other exceptions during commit.
ADD_DATA_ERROR103Exception while adding data.
CREATE_CONNECTION_ERROR104Exception while creating a connection.
CLOSE_CONNECTION_ERROR105Exception while closing a connection.
CONFIG_ERROR106Configuration error in DatabaseConfig.
STOP_ERROR107Error while stopping the instance.
OTHER999Default exception error code.

Usage notes

Thread safety

The SDK is not thread-safe. In a multi-threaded environment, each thread must maintain its own Adb4pgClient instance. Sharing a single client across threads creates write contention and turns the shared client into a performance bottleneck.

Commit and data durability

Data is written to AnalyticDB for PostgreSQL only after commit() succeeds. When the client throws an exception, inspect the error code to decide whether to retry the failed records or log and skip them.

Batch size

Avoid committing too frequently with small batches — the efficiency gains of batch writing are lost when batches are too small. Aim to add at least 10,000 records before calling commit(). Small batches increase the number of network round trips and reduce throughput.

Increasing the number of write threads does not always improve performance. Because applications typically buffer data in memory before committing, high thread counts can increase memory pressure and trigger more frequent garbage collection (GC). Monitor your application's GC status when tuning thread count.

Configuration lock

Finalize all DatabaseConfig settings before passing the config object to Adb4pgClient. No configuration changes take effect after the client is initialized.

SQL operations

The SDK is optimized for bulk writes (INSERT). For all other SQL operations — queries, DDL, updates, and deletes — use getConnection() to retrieve a JDBC connection and operate through standard JDBC interfaces.