MySQL到Doris的StreamingETL实现(Flink CDC 3.0)

本文详细描述了如何使用FlinkCDC3.0实现从MySQL到Doris的实时数据同步,包括环境准备、配置文件编写、任务启动、路由变更及数据操作的步骤。
摘要由CSDN通过智能技术生成

MySQL到Doris的StreamingETL实现(Flink CDC 3.0)

1 环境准备

1)安装FlinkCDC

[root@hadoop1 software]$ tar -zxvf flink-cdc-3.0.0-bin.tar.gz -C /opt/module/

2)拖入MySQL以及Doris依赖包

将flink-cdc-pipeline-connector-doris-3.0.0.jar以及flink-cdc-pipeline-connector-mysql-3.0.0.jar防止在FlinkCDC的lib目录下

2 同步变更

2)编写MySQL到Doris的同步变更配置文件

在FlinkCDC目录下创建job文件夹,并在该目录下创建同步变更配置文件

[root@hadoop1 flink-cdc-3.0.0]$ mkdir job/

[root@hadoop1 flink-cdc-3.0.0]$ cd job

[root@hadoop1 job]$ vim mysql-to-doris.yaml

 

source:

 type: mysql

 hostname: hadoop3

 port: 3306

 username: root

 password: "123456"

 tables: test.\.

 server-id: 5400-5404

 server-time-zone: UTC+8

 

sink:

 type: doris

 fenodes: hadoop1:7030

 username: root

 password: "123456"

 table.create.properties.light_schema_change: true

 table.create.properties.replication_num: 1

 

pipeline:

 name: Sync MySQL Database to Doris

 parallelism: 1

3)启动任务并测试

(1)开启Flink集群,注意:在Flink配置信息中打开CheckPoint

[root@hadoop3 flink-1.18.0]$ vim conf/flink-conf.yaml

添加如下配置信息

execution.checkpointing.interval: 5000

分发该文件至其他Flink机器并启动Flink集群

[root@hadoop3 flink-1.18.0]$ bin/start-cluster.sh

(2)开启Doris FE

[root@hadoop1 fe]$ bin/start_fe.sh

(3)开启Doris BE

[root@hadoop1 be]$ bin/start_be.sh

(4)在Doris中创建test数据库

[root@hadoop3 doris]$ mysql -uroot -p123456 -P9030 -hhadoop1

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or g.

Your MySQL connection id is 0

Server version: 5.7.99 Doris version doris-1.2.4-1-Unknown

 

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

 

mysql> create database test;

Query OK, 1 row affected (0.00 sec)

 

mysql> create database doris_test_route;

Query OK, 1 row affected (0.00 sec)

(5)启动FlinkCDC同步变更任务

[root@hadoop3 flink-cdc-3.0.0]$ bin/flink-cdc.sh job/mysql-to-doris.yaml

(6)刷新Doris中test数据库观察结果

(7)在MySQL的test数据中对应的几张表进行新增、修改数据以及新增列操作,并刷新Doris中test数据库观察结果

3 路由变更

1)编写MySQL到Doris的路由变更配置文件

source:

 type: mysql

 hostname: hadoop3

 port: 3306

 username: root

 password: "123456"

 tables: test_route.\.

 server-id: 5400-5404

 server-time-zone: UTC+8

 

sink:

 type: doris

 fenodes: hadoop1:7030

 benodes: hadoop1:7040

 username: root

 password: "123456"

 table.create.properties.light_schema_change: true

 table.create.properties.replication_num: 1

 

route:

 - source-table: test_route.t1

  sink-table: doris_test_route.doris_t1

 - source-table: test_route.t2

  sink-table: doris_test_route.doris_t1

 - source-table: test_route.t3

  sink-table: doris_test_route.doris_t3

 

pipeline:

 name: Sync MySQL Database to Doris

 parallelism: 1

2)启动任务并测试

[root@hadoop3 flink-cdc-3.0.0]$ bin/flink-cdc.sh job/mysql-to-doris-route.yaml

3)刷新Doris中test_route数据库观察结果

4)在MySQL的test_route数据中对应的几张表进行新增、修改数据操作,并刷新Doris中doris_test_route数据库观察结果

Flink CDC(Change Data Capture)是一种数据同步技术,可以从源数据库中捕获变更数据并将其同步到目标数据库中。DorisDB是一款分布式数据仓库,支持海量数据的存储和查询分析。下面以将数据DorisDB同步到DorisDB为例,介绍如何使用Flink CDC实现数据同步。 1. 准备工作 在开始之前,需要安装好以下工具和环境: - DorisDB - Flink - Flink CDC 2. 创建数据源 首先需要创建一个数据源,用于从DorisDB中读取数据。可以使用Flink的JDBCInputFormat来读取DorisDB中的数据。在Flink中,可以使用以下代码创建一个JDBCInputFormat: ``` JDBCInputFormat jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(driverName) .setDBUrl(dbUrl) .setUsername(username) .setPassword(password) .setQuery("SELECT * FROM table") .finish(); ``` 其中,driverName、dbUrl、username和password是DorisDB的连接信息,"SELECT * FROM table"是要读取的表的SQL语句。 3. 创建数据同步任务 接下来需要创建一个Flink数据流任务,用于将从DorisDB中读取的数据同步到另一个DorisDB中。可以使用Flink的DataStream API来实现数据同步。以下是一个示例代码: ``` StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); DataStream<Row> sourceStream = env.createInput(jdbcInputFormat); DataStream<Row> sinkStream = sourceStream.map(new MapFunction<Row, Row>() { @Override public Row map(Row value) throws Exception { // 对数据进行转换 return value; } }); DorisDBOutputFormat dorisDBOutputFormat = new DorisDBOutputFormat(); dorisDBOutputFormat.setDrivername(driverName); dorisDBOutputFormat.setDBUrl(dbUrl); dorisDBOutputFormat.setUsername(username); dorisDBOutputFormat.setPassword(password); dorisDBOutputFormat.setTable(table); dorisDBOutputFormat.setBatchSize(batchSize); sinkStream.writeUsingOutputFormat(dorisDBOutputFormat); env.execute(); ``` 其中,sourceStream是从DorisDB中读取的数据流,sinkStream是经过转换后要写入到DorisDB的数据流。可以使用map函数对数据进行转换。DorisDBOutputFormat是一个自定义的输出格式,用于将数据写入到DorisDB中。在这个示例代码中,DorisDBOutputFormat的batchSize属性设置为1000,表示每1000条数据进行一次批量写入。 4. 运行数据同步任务 将上述代码保存为一个Java程序,并使用Flink命令行工具提交任务即可开始数据同步。在执行过程中,Flink CDC会自动监控DorisDB中的数据变更,将新增、修改、删除等操作同步到目标数据库中。 总的来说,使用Flink CDC实现DorisDB数据同步是一种高效、可靠的方式。它不仅可以帮助用户快速实现数据同步,还可以提高数据的实时性和准确性,为企业的数据分析和决策提供有力支持。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值