tencent cloud

Feedback

SDK Upgrade

Last updated: 2023-11-21 10:40:36
    Note
    The data subscription feature of the legacy version ‍has been deactivated. If you need to create a subscription task for data consumption, use the data subscription feature (Kafka edition). For more information, see Data Subscription (Kafka Edition) and Data Subscription Guide (MySQL).
    Data subscription APIs on v2.0 will become unavailable. Data subscription SDK 2.8.0 and earlier versions use API 2.0 for authentication, and thus will be affected after API 2.0 is deprecated.
    Follow the steps below to upgrade the applications you have consumed through the SDK in time to avoid the impact of API 2.0 deprecation.
    Note
    We recommend that you call the SDK by using the sub-account key and environment variables to improve the SDK security. For more information, see Access Key. When authorizing sub-accounts, follow the principle of minimum permission to avoid leaking resources other than the destination buckets and objects.
    If you must use a permanent key, we recommend that you follow the principle of minimum permission to limit the permission scope of the permanent key.

    Step 1. Check the SDK version

    Check the version of the used SDK you have downloaded from Data Subscription SDK. Check the SDK version as follows:
    Check the package name of the SDK you have used, which contains the version information, such as binlogsdk-2.8.0-jar-with-dependencies.jar.
    Check the value of SDK_VERSION. If the SDK package has been renamed, run the unzip command to decompress the package and check the value of SDK_VERSION in the tencentSubscribe.properties file.
    If the data subscription SDK version is 2.8.0 or earlier, proceed to step 2.

    Step 2. Upgrade the SDK

    1. Download the latest SDK from Data Subscription SDK and replace SDK 2.8.0 and earlier versions with it.
    2. After the replacement, refer to the following code modifications to add a line of code to set region for the subscription channel.
    package com.qcloud.biz;
    import com.qcloud.dts.context.NetworkEnv;
    import com.qcloud.dts.context.SubscribeContext;
    import com.qcloud.dts.message.ClusterMessage;
    import com.qcloud.dts.message.DataMessage;
    import com.qcloud.dts.subscribe.ClusterListener;
    import com.qcloud.dts.subscribe.DefaultSubscribeClient;
    import com.qcloud.dts.subscribe.SubscribeClient;
    import java.util.List;
    public class Main {
    public static void main(String[] args) throws Exception {
    
    SubscribeContext context=new SubscribeContext();
    // User `secretId` and `secretKey`. We recommend that you use a sub-account key and follow the principle of minimum permission to reduce risks. For information on how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.
    context.setSecretId("AKID-522dabxxxxxxxxxxxxxxxxxx");
    context.setSecretKey("AKEY-0ff4cxxxxxxxxxxxxxxxxxxxx");
    /*****************Code modification starts****************/
    // If you do not set the `region` parameter, the API 2.0 will still be used for authentication even after it is deprecated.
    // However, the authentication via API 2.0 will fail then, so the SDK will not work properly.
    // Set `region` for subscription channel.
    context.setRegion("ap-chongqing");
    /*****************Code modification ends******************/
    
    
    // Create a client
    SubscribeClient client=new DefaultSubscribeClient(context);
    // Create a subscription listener
    ClusterListener listener= new ClusterListener() {
    @Override
    public void notify(List<ClusterMessage> messages) throws Exception {
    // Consume the subscribed data
    for(ClusterMessage m:messages){
    for(DataMessage.Record.Field f:m.getRecord().getFieldList()){
    if(f.getFieldname().equals("id")){
    System.out.println("seq:"+f.getValue());
    }
    DataMessage.Record record = m.getRecord();
    }
    // Acknowledge the consumption
    m.ackAsConsumed();
    }
    }
    @Override
    public void onException(Exception e){
    System.out.println("listen exception"+e);
    }};
    // Add a listener
    client.addClusterListener(listener);
    // Configure the requested subscription channel
    client.askForGUID("dts-channel-r0M8kKsSyRZmSxQt");
    // Start the client
    client.start();
    }
    }
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support