SDK quick start

Last Updated: Jun 23, 2017

In this section, you will learn how to perform basic operations with DTS SDK.

Initialize RegionContext

RegionContext is used to save and set security authentication information and network access mode. The following code is used to initialize RegionContext, set up security authentication credentials, and determine network access mode.

  1. import java.util.List;
  2. import com.aliyun.drc.clusterclient.RegionContext;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. public class MainClass
  6. {
  7. public static void main(String[] args) throws Exception {
  8. // Create RegionContext
  9. RegionContext context = new RegionContext();
  10. // Configure AccessKey and AccessKeySecret of the Alibaba Cloud account
  11. context.setAccessKey("<AccessKey>");
  12. context.setSecret("<AccessKeySecret>");
  13. // Whether the server running the SDK uses public network IP address to connect DTS subscription channel
  14. context.setUsePublicIp(true);
  15. // The following is other calling code...
  16. …………
  17. }
  18. }

To use the SDK, it is essential that you initialize RegionContext, configure security certifications, and complete other information of the connected subscription instance.

In the preceding code,

  • The setAccessKey interface sets the AccessKey of Alibaba Cloud accounts.

  • The setSecret interface sets the AccessKeySecret of Alibaba Cloud accounts.

  • AccessKey and AccessKeySecret are assigned to the user by Alibaba Cloud as an ID pair. They can be created at the user center of Alibaba Cloud.

  • The setUsePublicIp determines whether the SDK running server uses public network IP address to connect the subscription instance. In this determination, True means yes, and False means no.

Initialize ClusterClient

SDK uses ClusterClient class to connect the subscription instance, and receive change data. The following code is used to create a ClusterClient:

  1. import java.util.List;
  2. import com.aliyun.drc.clusterclient.ClusterClient;
  3. import com.aliyun.drc.clusterclient.DefaultClusterClient;
  4. import com.aliyun.drc.clusterclient.RegionContext;
  5. public class MainClass
  6. {
  7. public static void main(String[] args) throws Exception {
  8. // Create RegionContext
  9. RegionContext context = new RegionContext();
  10. context.setAccessKey("<AccessKey>");
  11. context.setSecret("<AccessKeySecret>");
  12. context.setUsePublicIp(true);
  13. // Create subscription consumers
  14. final ClusterClient client = new DefaultClusterClient(context);
  15. // The following is other calling code...
  16. ……………
  17. }
  18. }

Initialize Listener

SDK uses Listener class to consume change data.

Add listeners after the initialization of ClusterClient. The Listener defines the notification function to receive subscription data and consume data. The following code demonstrates the most basic consumption logic, and is used to print the subscription data to the screen.

  1. import com.aliyun.drc.clusterclient.ClusterClient;
  2. import com.aliyun.drc.clusterclient.ClusterListener;
  3. import com.aliyun.drc.clusterclient.DefaultClusterClient;
  4. import com.aliyun.drc.clusterclient.RegionContext;
  5. import com.aliyun.drc.clusterclient.message.ClusterMessage;
  6. public class MainClass
  7. {
  8. public static void main(String[] args) throws Exception {
  9. // Initialize a RegionContext object
  10. ...
  11. //Initialize ClusterClient object
  12. ………
  13. ClusterListener listener = new ClusterListener(){
  14. @Override
  15. public void notify(List<ClusterMessage> messages) throws Exception {
  16. for (ClusterMessage message : messages) {
  17. //Print the subscription change data
  18. System.out.println(message.getRecord() + ":" + message.getRecord().getTablename() + ":"
  19. + message.getRecord().getOpt());
  20. //After data consumption is completed, report ACK to DTS, and must call
  21. message.ackAsConsumed();
  22. }
  23. }
  24. }
  25. }

DTS saves the data consumption timestamp of SDK on the DTS server, which simplifies the SDK disaster recovery process.

The askAsConsumed () interface in the preceding sample code reports the position and timestamp of the latest data consumed by SDK to the DTS server. If SDK restarts after an unexpected downtime, it will automatically obtain the consumption time from the DTS server, and then restart from this time point to fix data duplication issues.

Starat ClusterClient

  1. import java.util.List;
  2. import com.aliyun.drc.clusterclient.ClusterClient;
  3. import com.aliyun.drc.clusterclient.ClusterListener;
  4. import com.aliyun.drc.clusterclient.DefaultClusterClient;
  5. import com.aliyun.drc.clusterclient.RegionContext;
  6. import com.aliyun.drc.clusterclient.message.ClusterMessage;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. public class MainClass
  10. {
  11. public static void main(String[] args) throws Exception {
  12. //Initialize RegionContext
  13. ...
  14. //Initialize ClusterClient
  15. ...
  16. //Initialize ClusterListener
  17. ...
  18. // Add listeners
  19. client.addConcurrentListener(listener);
  20. // Set requested subscription channel ID
  21. client.askForGUID("dts_rdsrjiei2u2afnb_DSF");
  22. // Start background thread. Note that here will not get blocked and the main thread cannot exit
  23. client.start();
  24. }

In the preceding code, the askForGUID interface sets the subscription instance ID requested by the client. The subscription channel ID is obtained from the DTS Console. Once the subscription instance ID is configured, this SDK can obtain the change data from this subscription instance.

You need to add a listener to a client before starting the client. When the client pulls change data from the subscription instance, it will start the data consumption by calling the notify method of the listener synchronously.

Thank you! We've received your feedback.