Database/Table Data Subscription to Redis

Last updated: 2020-04-27 17:04:09

    This document provides a simple example that walks you through how to pull a table from data subscription to Redis as well as a simple Redis Demo. The following operations are performed on CentOS.

    Configuring Environment

    Getting Key

    Log in to the CAM Console to get a key.

    Selecting Data Subscription

    1. Log in to the DTS Console and select Data Subscription on the left sidebar to enter the data subscription page.
    2. Select the name of the TencentDB instance to be synced, click "Start", return to the data subscription page, and click the data subscription you created. For detailed directions, please see How to Get a Data Subscription.
    3. Check the corresponding DTS channel, IP, and port and enter them together with the obtained key into the corresponding
     context.setSecretId("AKIDfdsfdsfsdt1331431sdfds"); Enter the `secretID` obtained from TencentCloud API.
            context.setSecretKey("test111usdfsdfsddsfRkeT"); Enter the `secretKey` obtained from TencentCloud API.
        // Enter the IP and port obtained through data subscription in the DTS service here
            context.setServiceIp(""); Enter the IP obtained from the data subscription configuration
            context.setServicePort(7507); Enter the port obtained from the data subscription configuration
            // Create a consumer
            //SubscribeClient client=new DefaultSubscribeClient(context,true);
            final DefaultSubscribeClient client = new DefaultSubscribeClient(context);
            final Jedis jedis = new Jedis("", 6379); Enter your corresponding Redis host and port
            final String targetDatabase = "test"; Enter the name of the database to subscribe to
            final String targetTable = "alantest"; Enter the name of the table to subscribe to. The table has two fields, namely, `id` and `name`, of which `id` is the primary key
            // Create a subscription listener
            ClusterListener listener = new ClusterListener() {
                public void notify(List<ClusterMessage> messages) throws Exception {
            //                System.out.println("--------------------:" + messages.size());
                    for(ClusterMessage m:messages){
                        DataMessage.Record record = m.getRecord();
                        // Filter out uninteresting subscription messages
                    if(!record.getDbName().equalsIgnoreCase(targetDatabase) || !record.getTablename().equalsIgnoreCase(targetTable)){
                            // Note: Ack must be performed for uninteresting messages too
                        if(record.getOpt() != DataMessage.Record.Type.BEGIN && record.getOpt() != DataMessage.Record.Type.COMMIT){
                            List<DataMessage.Record.Field> fields = record.getFieldList();
                            //INSERT RECORD
                            //String pk = record.getPrimaryKeys();
                            if(record.getOpt() == DataMessage.Record.Type.INSERT){
                    String keyid="";
                    String value="";
                                for (DataMessage.Record.Field field : fields) {
                                    // Get the `id` value as the primary key first, find the `name` column, and insert the corresponding values of `key` and `name` in Redis
                    jedis.set(keyid, value);

    Compiling and Testing

    1. [root@VM_71_10_centos ~]# javac -classpath binlogsdk-2.6.0-release.jar:jedis-2.9.0.jar -encoding UTF-8 
    2. Launch the program. If no errors are reported, the program works properly. Check the previously configured local file.
      java -XX:-UseGCOverheadLimit -Xms2g -Xmx2g -classpath .:binlogsdk-2.6.0-release.jar:jedis-2.9.0.jar RedisDemo
    3. Perform INSERT and UPDATE operations. If you find that the data has been indeed inserted and modified successfully in Redis, you can perform DELETE operations to delete the corresponding data from Redis.
    MySQL [test]> insert into alantest values(1001,'alan1');
    Query OK, 1 row affected (0.00 sec)
    MySQL [test]> update alantest set name='alan2' where id=1001;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    ------------------------> get 1001
    MySQL [test]> update alantest set name='alan3' where id=1001;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    ------------------------> get 1001
    MySQL [test]> delete from alantest where id=1001;
    Query OK, 1 row affected (0.00 sec)
    -----------------------> get 1001

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback