This topic describes how to use Data Transmission Service (DTS) SDK for Java and provides examples for your reference.

Prerequisites

  • An AccessKey pair is created. For more information, see Obtain an AccessKey pair.
    Note To protect the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user, grant the RAM user the permissions to access DTS, and then use the AccessKey pair of the RAM user to call DTS SDK for Java. For more information, see Control access to resources by using RAM users.
  • The SDK installation package of DTS is downloaded. For more information, see Download SDKs.

Install the SDK

You can install Alibaba Cloud SDK for Java by adding Maven dependencies or by importing JAR files. For more information, see Dependencies.

Note
  • You can view the Maven dependencies of DTS in SDK Center.
  • If you choose to download the SDK installation package by adding Maven dependencies, we recommend that you use the latest version of the SDK.

Procedure

  1. Set the region ID, AccessKey ID, and AccessKey secret.
    IClientProfile profile = DefaultProfile.getProfile("<RegionId>","<accessKeyId>","<accessSecret>");
    Note
    • <RegionId>: the region ID. For more information, see List of supported regions.
    • <accessKeyId>: the AccessKey ID of the RAM user.
    • <accessSecret>: the AccessKey secret of the RAM user.
  2. Optional:Set the endpoint.

    An endpoint is the API server address of an Alibaba Cloud service. Each service may have different endpoints in different regions. Each Alibaba Cloud SDK has a built-in endpoint addressing module. When you call an SDK to initiate a request to a service, the SDK automatically identifies the endpoint based on the region ID and service ID. The region ID and service ID are specified when you create the SDK client. Therefore, this step is optional.

    DefaultProfile.addEndpoint("<endpointName>","<RegionId>", "dts", "<domain>");
    Note
    • <endpointName>: the name of the endpoint.
    • <RegonId>: the region ID. For more information, see List of supported regions.
    • <domain>: the domain name. For more information, see Endpoints.
  3. Initiate the SDK client.
    DefaultAcsClient client = new DefaultAcsClient(profile);
  4. Create an API request and configure the required parameters.

    In the following example, the CreateMigrationJob operation is called.

    CreateMigrationJobResponse request = new CreateMigrationJobRequest();
          request.setRegion("cn-hangzhou");
          request.setMigrationJobClass("large");
  5. Initiate the API request and handle the response or exception.
    try {
        CreateMigrationJobResponse response = client.getAcsResponse(request);
        System.out.println(new Gson().toJson(response));
    } catch (ServerException e) {
        e.printStackTrace();
    } catch (ClientException e) {
        System.out.println("ErrCode:" + e.getErrCode());
        System.out.println("ErrMsg:" + e.getErrMsg());
        System.out.println("RequestId:" + e.getRequestId());
    }

Sample request

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.dts.model.v20180801.*;

public class CreateMigrationJob {

    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "********", "********");
        IAcsClient client = new DefaultAcsClient(profile);

        CreateMigrationJobRequest request = new CreateMigrationJobRequest();
        request.setRegionId("cn-hangzhou");
        request.setRegion("cn-hangzhou");
        request.setMigrationJobClass("large");

        try {
            CreateMigrationJobResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}

Sample response

{"MigrationJobId":"dts********","Success":true}

References