After creating a change tracking task, use the SDK demo provided by Data Transmission Service (DTS) to consume the resulting data changes. This topic explains how to use the SDK demo with distributed data sources such as PolarDB-X 1.0 and DMS LogicDB.
Prerequisites
-
JDK 1.8 is installed.
-
IntelliJ IDEA is installed.
Precautions
To consume change data as a RAM user, you must grant that user the AliyunDTSFullAccess permission and permissions to access the source objects. For more information, see Authorize a RAM user to manage DTS and Manage RAM user permissions.
Procedure
This topic demonstrates how to run the SDK demo to consume change data from a PolarDB-X 1.0 instance, using IntelliJ IDEA Community Edition 2020.1 for Windows as an example.
-
Create a change tracking instance. For more information, see Create a change tracking task for PolarDB-X 1.0.
-
Create one or more consumer groups. For more information, see Create a consumer group.
-
Download and decompress the SDK demo. For the download link, see SDK Demo Code.
ImportantWhen you consume change data, you must call the commit method of DefaultUserRecord to submit the consumer offset. Otherwise, you risk consuming data repeatedly.
-
Open the project in IntelliJ IDEA.
-
Open IntelliJ IDEA and click Open or Import.
-
In the dialog box, navigate to the directory where you decompressed the SDK demo, expand the folders, and then double-click the pom.xml file.
-
In the dialog box that appears, select Open as Project.
-
-
In IntelliJ IDEA, expand the folders. Based on the usage mode of the SDK client, select and double-click to open the corresponding Java file: DistributedDTSConsumerDemo.
aliyun-dts-subscribe-sdk-java-master [dts-new-subscr...] .idea src main test java com.aliyun.dts.subscribe.clients DBMapperTest DistributedDTSConsumerDemo DTSConsumerAssignDemo DTSConsumerSubscribeDemo UserMetaStore .gitignore dts-new-subscribe-sdk.iml LICENSE pom.xml README.md External Libraries Scratches and Consoles -
Set the required parameters in the Java file.
public static void main(String[] args) throws ClientException { // Configure the change tracking settings for a distributed data source, such as a PolarDB-X 1.0 instance. Set parameters such as the AccessKey pair, instance ID, task ID, and consumer group. String accessKeyId = "LTA***********99reZ"; String accessKeySecret = "****************"; String regionId = "cn-hangzhou"; String dtsInstanceId = "dtse5212sed162****"; String jobId = "l791216x16d****"; String sid = "dtsip412t13160****"; String userName = "xftest"; String password = "******"; String proxyUrl = "dts-cn-****.com:18001"; // The initial consumer offset, a Unix timestamp in seconds. e.g., 1566180200 for Mon Aug 19 10:03:21 CST 2019. String checkpoint = "1639620090"; // Convert physical database/table name to logical database/table name boolean mapping = true; // Set to true to force the client to start from the specified checkpoint. A checkpoint reset works only in ASSIGN mode. boolean isForceUseInitCheckpoint = false; ConsumerContext.ConsumerSubscribeMode subscribeMode = ConsumerContext.ConsumerSubscribeMode.ASSIGN; DistributedDTSConsumerDemo demo = new DistributedDTSConsumerDemo(userName, password, regionId, jobId, sid, dtsInstanceId, accessKeyId, accessKeySecret, subscribeMode, proxyUrl, checkpoint, isForceUseInitCheckpoint, mapping); demo.start(); }Parameter
Description
How to obtain
accessKeyId
The AccessKey ID.
For more information, see Obtain an AccessKey pair.
accessKeySecret
The AccessKey secret.
regionId
The region ID of the change tracking instance.
In the DTS console, click the ID of the target change tracking instance. On the Task Management page, find the region information. For example, if the region is China (Hangzhou), set this parameter to
cn-hangzhou. For a list of regions, see List of supported regions.dtsInstanceId
The ID of the change tracking instance.
In the DTS console, click the ID of the target change tracking instance. On the Task Management page, you can find the instance ID and task ID.
jobId
The ID of the change tracking task.
sid
The ID of the consumer group.
In the DTS console, click the ID of the target change tracking instance and then click Consume Data. You can find the SID and the Account of the consumer group.
NoteThe password for the consumer group account is specified when you create the consumer group.
userName
The account of the consumer group.
password
The password of the consumer group account.
proxyUrl
The endpoint and port of the change tracking instance.
Note-
To minimize network latency, use the internal endpoint if the ECS instance running the SDK client is in the same classic network or VPC as the change tracking instance.
-
We do not recommend using a public endpoint because of potential network instability.
In the DTS console, click the ID of the target change tracking instance. On the Task Management page, you can find the endpoint and port number.
checkpoint
The consumer offset, specified as a Unix timestamp in seconds. The SDK client starts consuming data from this point in time.
NoteThe consumer offset is used in the following scenarios:
-
If your application is interrupted, pass the last consumer offset to resume consumption without data loss.
-
When the client starts, you can pass a specific consumer offset to consume data on demand.
The consumer offset must be within the data range of the change tracking instance and must be a Unix timestamp.
NoteYou can use a search engine to find a Unix timestamp converter.
-
-
In the top menu bar of IntelliJ IDEA, choose to run the client.
NoteOn the first run, IntelliJ IDEA automatically installs the required dependencies, which may take some time.
-
The output shows that the client can consume data changes from the source instance.
-
The SDK client periodically displays consumption statistics, which include the total number and volume of data records sent and received, and the requests per second (RPS).
Table 1. Consumption statistics
Parameter
Description
outCountsThe total number of data records consumed by the SDK client.
outBytesThe total volume of data consumed by the SDK client, in bytes.
outRpsData consumption rate of the SDK client, in requests per second (RPS).
outBpsThe data consumption rate of the SDK client, in bits per second (bps).
countThis parameter is reserved.
inBytesThe total volume of data sent by the DTS server, in bytes.
DStoreRecordQueueThe current size of the data cache queue on the DTS server.
inCountsThe total number of data records sent by the DTS server.
inRpsThe data sending rate of the DTS server, in requests per second (RPS).
inBpsThe data sending rate of the DTS server, in bits per second (bps).
__dtThe timestamp when the SDK client receives the data, in milliseconds.
DefaultUserRecordQueueThe current size of the data cache queue after serialization.
-
-
Optional: To customize how consumed data is processed, you can modify the
buildRecordListener()method or use a custom class.public static Map<String, RecordListener> buildRecordListener() { // You can implement your own listener. RecordListener mysqlRecordPrintListener = new RecordListener() { @Override public void consume(DefaultUserRecord record) { OperationType operationType = record.getOperationType(); if (operationType.equals(OperationType.INSERT) || operationType.equals(OperationType.UPDATE) || operationType.equals(OperationType.DELETE) || operationType.equals(OperationType.HEARTBEAT)) { // Consume the record. RecordListener recordPrintListener = new DefaultRecordPrintListener(DbType.MySQL); recordPrintListener.consume(record); // The commit method pushes the checkpoint update. record.commit(""); } } }; return Collections.singletonMap("mysqlRecordPrinter", mysqlRecordPrintListener); }