All Products
Search
Document Center

DataHub:Java SDK

Last Updated:Aug 25, 2026

Install SDK

Add the following dependency to your Maven project:

<dependency>
  <groupId>com.aliyun.datahub</groupId>
  <artifactId>aliyun-sdk-datahub</artifactId>
  <version>2.29.5</version>
</dependency>
  • JDK: Use version 1.8 or later.

  • If the API requires key authentication, configure the AccessKey ID and AccessKey Secret in your project. For more information, see Authentication.

Authentication

Background

An AccessKey (AK) is an access key provided by Alibaba Cloud used to authenticate OpenAPI calls. It consists of an AccessKey ID and an AccessKey Secret. You must keep both confidential.

Alibaba Cloud Credentials is a credential management tool for Alibaba Cloud developers. Configuring the default credentials provider chain lets you call Alibaba Cloud OpenAPI without hard-coding your AccessKey in plaintext. This helps secure your cloud resources.

Prerequisites

  • Obtain the AccessKey ID and AccessKey Secret for a RAM user. For instructions, see View the AccessKey information of a RAM user.

  • Important

    A leaked AccessKey for your primary account compromises all resources within it. To protect your account, create an AccessKey for a RAM user and avoid creating one for your primary account unless absolutely necessary.

    The AccessKey Secret for a RAM user is displayed only when the AccessKey is created and cannot be retrieved later. Save the AccessKey Secret securely immediately.

  • Install the Alibaba Cloud Credentials tool.

    To install with Maven (we recommend using the latest version of Credentials):

    <dependency>
     <groupId>com.aliyun</groupId>
     <artifactId>credentials-java</artifactId>
     <version>0.2.11</version>
    </dependency>
  • Install JDK 1.7 or later.

Configuration methods

This topic uses environment variables as an example. For more configuration methods, see Configure environment variables.

Important

When using a configuration file, ensure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are not set in your system. If they are set, they take precedence over the configuration file.

You can create a default access credential by setting the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. Your application will then automatically use these variables to authenticate API calls.

Configure environment variables

Set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • For Linux and macOS systems

    Run the following commands:

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>

    Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey Secret.

  • For Windows systems

    1. Create two new environment variables: ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. Set their values to your AccessKey ID and AccessKey Secret, respectively.

    2. Restart Windows.

Code example

    Client credentialClient = new Client();
    String accessKeyId = credentialClient.getAccessKeyId();
    String accessKeySecret = credentialClient.getAccessKeySecret();

Notes

  • For users upgrading from SDK v2.9: When you replace the setTimestampInms method, multiply the timestamp value by 1,000.

  • Under normal operating conditions, only the putRecords / putRecordsByShard and getRecords APIs are frequently called to read and write data. Other APIs, such as getTopic, getCursor, and listShard, are typically called only during initialization.

  • You can create one or more DatahubClient instances in your project. DatahubClient instances are thread-safe.

  • If you encounter classes with the same name in different packages, note that version 2.12 and later use classes from the com.aliyun.datahub.client package. Classes in other packages are maintained for backward compatibility with SDKs earlier than version 2.12. For example:

    // Version 2.12
    com.aliyun.datahub.client.model.RecordSchema
    // If your code was written with an SDK version earlier than 2.12 and you want to avoid code changes after upgrading, continue to use this type.
    com.aliyun.datahub.common.data.RecordSchema
  • If the error Parse body failed, Offset: 0 occurs, try setting the enableBinary parameter to false.