赞
踩
技术解析|Doris Connector 结合 Flink CDC 实现 MySQL 分库分表 Exactly Once 精准接入-阿里云开发者社区
1. Flink环境:
wget https://dlcdn.apache.org/flink/flink-1.15.1/flink-1.15.1-bin-scala_2.12.tgz
tar -zxvf flink-1.15.1-bin-scala_2.12.tgz cd flink-1.15.1
修改rest.bind-address为 0.0.0.0
vi conf/flink-conf.yaml
- wget https://repo1.maven.org/maven2/com/ververica/flink-sql-connector-mysql-cdc/2.2.1/flink-sql-connector-mysql-cdc-2.2.1.jar
-
- wget https://repo1.maven.org/maven2/org/apache/doris/flink-doris-connector-1.14_2.12/1.0.3/flink-doris-connector-1.14_2.12-1.0.3.jar
./bin/start-cluster.sh
2、MySQL数据表及数据
- [mysqld]
- log_bin=mysql_bin
- binlog-format=Row
- server-id=1
- CREATE DATABASE emp;
-
- USE emp;
-
- CREATE TABLE employee (
- emp_no INT NOT NULL,
- birth_date DATE NOT NULL,
- first_name VARCHAR(14) NOT NULL,
- last_name VARCHAR(16) NOT NULL,
- gender ENUM ('M','F') NOT NULL,
- hire_date DATE NOT NULL, PRIMARY KEY (emp_no)
- );
-
- INSERT INTO `employee` VALUES
- (10001,'1953-09-02','Georgi','Facello','M','1986-06-26'),
- (10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21'),
- (10003,'1959-12-03','Parto','Bamford','M','1986-08-28'),
- (10004,'1954-05-01','Chirstian','Koblick','M','1986-12-01'),
- (10005,'1955-01-21','Kyoichi','Maliniak','M','1989-09-12'),
- (10006,'1953-04-20','Anneke','Preusig','F','1989-06-02'),
- (10007,'1957-05-23','Tzvetan','Zielinski','F','1989-02-10'),
- (10008,'1958-02-19','Saniya','Kalloufi','M','1994-09-15'),
- (10009,'1952-04-19','Sumant','Peac','F','1985-02-18'),
- (10010,'1963-06-01','Duangkaew','Piveteau','F','1989-08-24'),
- (10011,'1953-11-07','Mary','Sluis','F','1990-01-22'),
- (10012,'1960-10-04','Patricio','Bridgland','M','1992-12-18'),
- (10013,'1963-06-07','Eberhardt','Terkki','M','1985-10-20'),
- (10014,'1956-02-12','Berni','Genin','M','1987-03-11'),
- (10015,'1959-08-19','Guoxiang','Nooteboom','M','1987-07-02'),
- (10016,'1961-05-02','Kazuhito','Cappelletti','M','1995-01-27'),
- (10017,'1958-07-06','Cristinel','Bouloucos','F','1993-08-03'),
- (10018,'1954-06-19','Kazuhide','Peha','F','1987-04-03'),
- (10019,'1953-01-23','Lillian','Haddadi','M','1999-04-30'),
- (10020,'1952-12-24','Mayuko','Warwick','M','1991-01-26');
3. Doris数据表
- CREATE DATABASE demo;
-
- USE demo;
-
- CREATE TABLE employee_info (
- emp_no int NOT NULL,
- birth_date date,
- first_name varchar(20),
- last_name varchar(20),
- gender char(2),
- hire_date date,
- database_name varchar(50),
- table_name varchar(200)
- )
- UNIQUE KEY(`emp_no`, `birth_date`)
- DISTRIBUTED BY HASH(`birth_date`) BUCKETS 1
- PROPERTIES ( "replication_allocation" = "tag.location.default: 1" );
4. Flink数据表及数据
./bin/sql-client.sh embedded
Flink作业周期性执行checkpoint,记录Binlog位点,当作业发生Failover时,便会从之前记录的Binlog位点继续处理。
生产环境建议设置为60秒。
Flink SQL> SET execution.checkpointing.interval = 10s
- Flink SQL> CREATE TABLE employee_source (
- database_name STRING METADATA VIRTUAL,
- table_name STRING METADATA VIRTUAL,
- emp_no int NOT NULL,
- birth_date date,
- first_name STRING,
- last_name STRING,
- gender STRING,
- hire_date date,
- PRIMARY KEY (`emp_no`) NOT ENFORCED
- )
- WITH (
- 'connector' = 'mysql-cdc',
- 'hostname' = 'localhost',
- 'port' = '3336',
- 'username' = 'root',
- 'password' = '1234.abcd',
- 'database-name' = 'emp',
- 'table-name' = 'employee'
- );
查询数据:
Flink SQL> select * from employee_source limit 10;
- Flink SQL> CREATE TABLE cdc_doris_sink (
- emp_no int ,
- birth_date STRING,
- first_name STRING,
- last_name STRING,
- gender STRING,
- hire_date STRING,
- database_name STRING,
- table_name STRING
- )
- WITH (
- 'connector' = 'doris',
- 'fenodes' = 'localhost:8030',
- 'table.identifier' = 'demo.employee_info',
- 'username' = 'root',
- 'password' = '1234.abcd'
- );
参数说明:
connector : 指定连接器是doris
fenodes:doris FE节点IP地址及http port
table.identifier : Doris对应的数据库及表名
username:doris用户名
password:doris用户密码
查询数据:
Flink SQL> select * from cdc_doris_sink;
- Flink SQL> insert into cdc_doris_sink (emp_no,birth_date,first_name,last_name,gender,hire_date,database_name,table_name)
- select emp_no,cast(birth_date as string) as birth_date ,first_name,last_name,gender,cast(hire_date as string) as hire_date ,database_name,table_name from employee_source;
WebUI可以看到正在执行中的任务,说明添加完成
查看Doris数据表中数据
mysql> select * from employee_info;
5. 问题说明:
NoResourceAvailableException: Could not acquire the minimum required resources
进入flink目录,修改conf/conf/flink-conf.yaml:taskmanager.numberOfTaskSlots: 4 , 一般配置为cpu的个数。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。