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 Create 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 permissions to access DTS, and then use the AccessKey pair of the RAM user to call DTS SDK for Java. For more information, see Implement access control by using RAM.
  • 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 Install Alibaba Cloud SDK for Java.

Note If you 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
    • <RegonId>: 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 client.
    DefaultAcsClient client = new DefaultAcsClient(profile);
  4. Create an API request and set the request parameters.

    The following code takes CreateMigrationJob as an example.

    DescribeAccountsRequest request = new CreateMigrationJobRequest();
          request.setRegion("cn-hangzhou");
          request.setMigrationJobClass("large");
  5. Initiate the request and process the response or exceptions.
    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());
        }

    }
}
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

  • Debug API and generate SDK sample code.

    Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to search for API operations, call API operations, and dynamically generate SDK sample code.

  • For more information, see Use Alibaba Cloud SDK for Java.