当前位置:   article > 正文

使用最新版flink tidb cdc 同步数据到StarRocks_flink-sql-connector-tidb

flink-sql-connector-tidb

新发布的Flink CDC 2.2 版本新增了 OceanBase CE,PolarDB-X,SqlServer,TiDB 四种数据源接入。其中新增 OceanBase CDC,SqlServer CDC,TiDB CDC 三个连接器,而 PolarDB-X 的支持则是通过对 MySQL CDC 连接器进行兼容适配实现。

发布原文链接:https://blog.csdn.net/weixin_44904816/article/details/123836091

最近在测tidb原始的ticdc(PingCap提供)导入数据到kafka,再使用flink StarRocks connector导入到StarRocks过程中碰到较多问题,刚好可以使用flink新发布的flink tidb cdc来对接,实现从tidb快速同步数据到StarRocks。

tidb环境部署

  1. 下载tidb部署安装包tidb-community-server-v4.0.9-linux-amd64.tar.gz,下载地址

https://download.pingcap.org/tidb-community-server-v4.0.9-linux-amd64.tar.gz

  1. 安装环境
  1. tar -xvf tidb-community-server-v4.0.9-linux-amd64.tar.gz
  2. cd tidb-community-server-v4.0.9-linux-amd64
  3. sh local_install.sh
  1. 使用tidb playground模式启动tidb单节点测试环境
  1. sr@cs03:~/.tiup/bin$pwd
  2. /home/disk1/sr/.tiup/bin
  3. sr@cs03:~/.tiup/bin$./tiup playground
  4. Starting component `playground`: /home/disk1/sr/.tiup/components/playground/v1.3.2/tiup-playground
  5. Use the latest stable version: v4.0.9
  6. Specify version manually: tiup playground <version>
  7. The stable version: tiup playground v4.0.0
  8. The nightly version: tiup playground nightly
  9. Playground Bootstrapping...
  10. Start pd instance
  11. Start tikv instance
  12. Start tidb instance
  13. Waiting for tidb instances ready
  14. 127.0.0.1:4000 ... Done
  15. Start tiflash instance
  16. Waiting for tiflash instances ready
  17. 127.0.0.1:3930 ... Done
  18. CLUSTER START SUCCESSFULLY, Enjoy it ^-^
  19. To connect TiDB: mysql --host 127.0.0.1 --port 4000 -u root
  20. To view the dashboard: http://127.0.0.1:2379/dashboard
  21. To view the Prometheus: http://127.0.0.1:37449
  22. To view the Grafana: http://127.0.0.1:3000

Flink tidb cdc connector下载

下载flink tidb cdc connector,放在flink lib下:

wget https://repo1.maven.org/maven2/com/ververica/flink-sql-connector-tidb-cdc/2.2.0/flink-sql-connector-tidb-cdc-2.2.0.jar
  1. sr@cs03:~/app/flink-1.13.3/lib$pwd
  2. /home/disk1/sr/app/flink-1.13.3/lib
  3. sr@cs03:~/app/flink-1.13.3/lib$ll
  4. total 353940
  5. -rw-rw-r-- 1 sr sr 9404460 Mar 19 17:29 flink-connector-starrocks-1.2.1_flink-1.13_2.12.jar
  6. -rw-r--r-- 1 sr sr 92313 Oct 13 2021 flink-csv-1.13.3.jar
  7. -rw-r--r-- 1 sr sr 115418686 Oct 13 2021 flink-dist_2.11-1.13.3.jar
  8. -rw-r--r-- 1 sr sr 19583 Nov 5 21:27 flink-format-changelog-json-1.4.0.jar
  9. -rw-r--r-- 1 sr sr 148127 Oct 13 2021 flink-json-1.13.3.jar
  10. -rw-rw-r-- 1 sr sr 41368997 Feb 12 2020 flink-shaded-hadoop-2-uber-2.7.5-10.0.jar.bak
  11. -rwxrwxr-x 1 sr sr 7709740 Jun 8 2021 flink-shaded-zookeeper-3.4.14.jar
  12. -rw-rw-r-- 1 sr sr 3674114 Oct 12 2021 flink-sql-connector-kafka_2.12-1.13.3.jar
  13. -rw-rw-r-- 1 sr sr 19648014 Dec 16 23:36 flink-sql-connector-mysql-cdc-2.1.1.jar
  14. -rw-rw-r-- 1 sr sr 85355380 Apr 19 17:03 flink-sql-connector-tidb-cdc-2.2.0.jar
  15. -rw-r--r-- 1 sr sr 36453353 Oct 13 2021 flink-table_2.11-1.13.3.jar
  16. -rw-r--r-- 1 sr sr 41061738 Oct 13 2021 flink-table-blink_2.11-1.13.3.jar
  17. -rwxrwxr-x 1 sr sr 67114 Mar 31 2021 log4j-1.2-api-2.12.1.jar
  18. -rwxrwxr-x 1 sr sr 276771 Mar 31 2021 log4j-api-2.12.1.jar
  19. -rwxrwxr-x 1 sr sr 1674433 Mar 31 2021 log4j-core-2.12.1.jar
  20. -rwxrwxr-x 1 sr sr 23518 Mar 31 2021 log4j-slf4j-impl-2.12.1.jar

Yarn session模式启动flink集群

  1. sr@cs03:~/app/flink-1.13.3/bin$./yarn-session.sh
  2. SLF4J: Class path contains multiple SLF4J bindings.
  3. SLF4J: Found binding in [jar:file:/home/disk1/sr/app/flink-1.13.3/lib/log4j-slf4j-impl-2.12.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
  4. ......
  5. ......
  6. ...... ​​​​​​​
  7. 2022-04-19 17:23:39,069 INFO org.apache.hadoop.yarn.client.api.impl.YarnClientImpl [] - Submitted application application_1641365150688_0057
  8. 2022-04-19 17:23:39,070 INFO org.apache.flink.yarn.YarnClusterDescriptor [] - Waiting for the cluster to be allocated
  9. 2022-04-19 17:23:39,071 INFO org.apache.flink.yarn.YarnClusterDescriptor [] - Deploying cluster, current state ACCEPTED
  10. 2022-04-19 17:23:44,097 INFO org.apache.flink.yarn.YarnClusterDescriptor [] - YARN application has been deployed successfully.
  11. 2022-04-19 17:23:44,159 INFO org.apache.flink.yarn.YarnClusterDescriptor [] - Found Web Interface cs03:38081 of application 'application_1641365150688_0057'.
  12. JobManager Web Interface: http://cs03:38081

tidb环境建表,插入数据,更新等

  1. mysql> create database gong;
  2. mysql> use gong;
  3. mysql> CREATE TABLE orders (
  4. -> order_id INT,
  5. -> order_date TIMESTAMP(3),
  6. -> customer_name varchar(20),
  7. -> price DECIMAL(10, 5),
  8. -> product_id INT,
  9. -> order_status BOOLEAN,
  10. -> PRIMARY KEY(order_id)
  11. -> ) ;
  12. mysql> insert into orders values(1,now(),"gong",13.99,123,1);
  13. Query OK, 1 row affected (0.00 sec)
  14. mysql> insert into orders values(2,now(),"gong",13.99,123,1);
  15. Query OK, 1 row affected (0.00 sec)
  16. mysql> insert into orders values(3,now(),"gong",13.99,123,1);
  17. Query OK, 1 row affected (0.00 sec)
  18. mysql> insert into orders values(4,now(),"gong",13.99,123,1);
  19. Query OK, 1 row affected (0.01 sec)
  20. ......
  21. ......
  22. mysql> insert into orders values(12,now(),"gong",13.99,123,1);
  23. Query OK, 1 row affected (0.00 sec)
  24. mysql> SELECT * FROM ORDERS;
  25. +----------+-------------------------+---------------+----------+------------+--------------+
  26. | order_id | order_date | customer_name | price | product_id | order_status |
  27. +----------+-------------------------+---------------+----------+------------+--------------+
  28. | 1 | 2022-04-19 17:12:33.000 | gong | 13.99000 | 123 | 1 |
  29. | 2 | 2022-04-19 17:14:20.000 | gong | 13.99000 | 123 | 1 |
  30. | 3 | 2022-04-19 17:14:25.000 | gong | 13.99000 | 123 | 1 |
  31. | 4 | 2022-04-19 17:14:46.000 | gong | 13.99000 | 123 | 1 |
  32. ......
  33. ......
  34. | 12 | 2022-04-19 17:20:37.000 | gong | 13.99000 | 123 | 1 |
  35. +----------+-------------------------+---------------+----------+------------+--------------+
  36. mysql> update orders set customer_name = "xxxxxx" where order_id = 12;
  37. Query OK, 1 row affected (0.02 sec)
  38. Rows matched: 1 Changed: 1 Warnings: 0

测试tidb数据到flink sql

开启flink sql:

  1. cd $FLINK_HOME/bin
  2. ./sql_client.sh embedded
  1. -- checkpoint every 3000 milliseconds
  2. Flink SQL> SET 'execution.checkpointing.interval' = '3s';
  3. -- register a TiDB table 'orders' in Flink SQL
  4. Flink SQL> CREATE TABLE orders (
  5. order_id INT,
  6. order_date TIMESTAMP(3),
  7. customer_name STRING,
  8. price DECIMAL(10, 5),
  9. product_id INT,
  10. order_status BOOLEAN,
  11. PRIMARY KEY(order_id) NOT ENFORCED
  12. ) WITH (
  13. 'connector' = 'tidb-cdc',
  14. 'tikv.grpc.timeout_in_ms' = '20000',
  15. 'pd-addresses' = 'localhost:2379',
  16. 'database-name' = 'gong',
  17. 'table-name' = 'orders'
  18. );
  19. -- read snapshot and binlogs from orders table
  20. Flink SQL> SELECT * FROM orders;

check生成了flink任务正常运行(经常会碰到提交任务失败):

  1. sr@cs03:~/app/flink-1.13.3/bin$./flink list
  2. SLF4J: Class path contains multiple SLF4J bindings
  3. ......
  4. ......
  5. 2022-04-19 18:16:02,146 INFO org.apache.flink.yarn.YarnClusterDescriptor [] - Found Web Interface cs03:38081 of application 'application_1641365150688_0057'.
  6. Waiting for response...
  7. ------------------ Running/Restarting Jobs -------------------
  8. 19.04.2022 17:25:27 : 4852d0bdb40aba6ae4041f1e36eadd1c : collect (RUNNING)
  9. --------------------------------------------------------------

 从flink sql cli能查询出数据,说明数据flow到flink已经打通,再后面就是对接导入到StarRocks;

对接StarRocks,将数据写入到StarRocks

在StarRocks建表

  1. CREATE TABLE `orders` (
  2. `order_id` int(11) NOT NULL COMMENT "",
  3. `order_date` datetime NULL COMMENT "",
  4. `customer_name` varchar(20) NULL COMMENT "",
  5. `price` decimal64(10, 5) NULL COMMENT "",
  6. `product_id` int(11) NULL COMMENT "",
  7. `order_status` boolean NULL COMMENT ""
  8. ) ENGINE=OLAP
  9. PRIMARY KEY(`order_id`)
  10. COMMENT "OLAP"
  11. DISTRIBUTED BY HASH(`order_id`) BUCKETS 1
  12. PROPERTIES (
  13. "replication_num" = "3",
  14. "in_memory" = "false",
  15. "storage_format" = "DEFAULT"
  16. );

使用Flink sql将数据同步到StarRocks

先将flink StarRocks cdc 依赖包 flink-connector-starrocks-1.2.1_flink-1.13_2.12.jar下载好,放到flink lib目录下,重启flink服务;(下载链接 Flink StarRocks connector,请注意1.13版本和1.11/1.12版本使用不同的connector)

在flink sql cli端

  1. CREATE TABLE IF NOT EXISTS `orders_sink` (
  2. order_id INT,
  3. order_date TIMESTAMP(3),
  4. customer_name varchar(20),
  5. price DECIMAL(10, 5),
  6. product_id INT,
  7. order_status BOOLEAN,
  8. PRIMARY KEY(`order_id`) NOT ENFORCED
  9. ) with (
  10. 'load-url' = 'cs01:9011',
  11. 'sink.buffer-flush.interval-ms' = '15000',
  12. 'sink.properties.row_delimiter' = '\x02',
  13. 'sink.properties.column_separator' = '\x01',
  14. 'connector' = 'starrocks',
  15. 'database-name' = 'gong',
  16. 'table-name' = 'orders',
  17. 'jdbc-url' = 'jdbc:mysql://cs01:9013',
  18. 'password' = '',
  19. 'username' = 'root'
  20. );
  21. Flink SQL> show tables;
  22. +-------------+
  23. | table name |
  24. +-------------+
  25. | orders |
  26. | orders_sink |
  27. +-------------+
  28. 2 rows in set

将数据插入到StarRocks

  1. Flink SQL> insert into orders_sink select * from orders;
  2. [INFO] Submitting SQL update statement to the cluster...
  3. 2022-04-19 18:37:14,581 INFO org.apache.hadoop.yarn.client.RMProxy [] - Connecting to ResourceManager at /172.26.194.184:38035
  4. 2022-04-19 18:37:14,582 INFO org.apache.flink.yarn.YarnClusterDescriptor [] - No path for the flink jar passed. Using the location of class org.apache.flink.yarn.YarnClusterDescriptor to locate the jar
  5. 2022-04-19 18:37:14,587 INFO org.apache.flink.yarn.YarnClusterDescriptor [] - Found Web Interface cs03:38081 of application 'application_1641365150688_0057'.
  6. [INFO] SQL update statement has been successfully submitted to the cluster:
  7. Job ID: 33b03fb9d257b57c5243e721c19a6041

查看新flink sql生成的任务:

  1. sr@cs03:~/app/flink-1.13.3/bin$./flink list
  2. SLF4J: Class path contains multiple SLF4J bindings.
  3. SLF4J: Found binding in [jar:file:/home/disk1/sr/app/flink-1.13.3/lib/log4j-slf4j-impl-2.12.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
  4. ......
  5. ......
  6. 2022-04-19 18:37:46,875 INFO org.apache.flink.yarn.YarnClusterDescriptor [] - No path for the flink jar passed. Using the location of class org.apache.flink.yarn.YarnClusterDescriptor to locate the jar
  7. 2022-04-19 18:37:46,953 INFO org.apache.flink.yarn.YarnClusterDescriptor [] - Found Web Interface cs03:38081 of application 'application_1641365150688_0057'.
  8. Waiting for response...
  9. ------------------ Running/Restarting Jobs -------------------
  10. 19.04.2022 18:37:14 : 33b03fb9d257b57c5243e721c19a6041 : insert-into_default_catalog.default_database.orders_sink (RUNNING)
  11. --------------------------------------------------------------
  12. No scheduled jobs.

在StarRocks验证变更数据是否都已经进去了

  1. mysql> select current_version();
  2. +-------------------+
  3. | current_version() |
  4. +-------------------+
  5. | 2.1.0 1864de0 |
  6. +-------------------+
  7. 1 row in set (0.02 sec)
  8. mysql> select * from orders;
  9. +----------+---------------------+---------------+----------+------------+--------------+
  10. | order_id | order_date | customer_name | price | product_id | order_status |
  11. +----------+---------------------+---------------+----------+------------+--------------+
  12. | 1 | 2022-04-19 09:12:33 | gong | 13.99000 | 123 | 1 |
  13. | 2 | 2022-04-19 09:14:20 | gong | 13.99000 | 123 | 1 |
  14. | 3 | 2022-04-19 09:14:25 | gong | 13.99000 | 123 | 1 |
  15. | 4 | 2022-04-19 09:14:46 | gong | 13.99000 | 123 | 1 |
  16. ......
  17. ......
  18. | 12 | 2022-04-19 09:20:37 | gongxiucheng | 13.99000 | 123 | 1 |
  19. | 13 | 2022-04-19 09:26:56 | gong | 13.99000 | 123 | 1 |
  20. | 14 | 2022-04-19 09:33:21 | gong | 13.99000 | 123 | 1 |
  21. | 15 | 2022-04-19 10:08:03 | gong | 13.99000 | 123 | 1 |
  22. +----------+---------------------+---------------+----------+------------+--------------+
  23. 15 rows in set (0.01 sec)

总结:

整个测试过程还比较顺滑,比之前测试tidb 原生ticdc少了很多麻烦,且简化整个数据链路,不依赖kafka,是tidb数据到starrocks利器。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/579473
推荐阅读
相关标签
  

闽ICP备14008679号