持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第12天,点击查看活动详情
书接上文
mysql环境参考
配置Mysql文件 /etc/mysql/my.cnf, 开启binlog
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[mysqld]
bind-address = 0.0.0.0
server_id = 1
log-bin = /var/lib/mysql/mysql-bin
#binlog-do-db = *
log-slave-updates
sync_binlog = 1
auto_increment_offset = 1
auto_increment_increment = 1
log_bin_trust_function_creators = 1
gtid_mode = on
enforce_gtid_consistency = on
创建测试库、测试表并准备数据
mysql> create database flink_cdc;
mysql> CREATE TABLE `test_a` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`data` varchar(10) DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
mysql> insert into test_a(data) values('d1');
FlinkCDC sink Hudi测试
FlinkCDC Sql Table DDL:
create table mysql_test_a(
id bigint primary key not enforced,
data String,
create_time Timestamp(3)
) with (
'connector'='mysql-cdc',
'hostname'='10.x.32.x',
'port'='3306',
'server-id'='5600-5604',
'username'='root',
'password'='root',
'server-time-zone'='Asia/Shanghai',
'debezium.snapshot.mode'='initial',
'database-name'='flink_cdc',
'table-name'='test_a'
);
FlinkCDC Sql Table查询验证
Flink SQL> select * from mysql_test_a;
这时Flink yarn session 集群中会启动一个作业来读取数据。
数据直接返回shell窗口:
读取结果与Mysql里的一致。在Mysql里执行一条更新:
mysql> update test_a set data='d5' where id=5;
观察到FlinkSql中,id=5的数据实时刷新了。
Hudi Sink Table DDL
create table hudi_test_a(
id bigint,
data String,
create_time Timestamp(3),
PRIMARY KEY (`id`) NOT ENFORCED
)
with(
'connector'='hudi',
'path'='hdfs://master.am.com:8020/tmp/flink/cdcdata/test_a',
'hoodie.datasource.write.recordkey.field'='id',
'hoodie.parquet.max.file.size'='268435456',
'write.precombine.field'='create_time',
'write.tasks'='1',
'write.bucket_assign.tasks'='1',
'write.task.max.size'='1024',
'write.rate.limit'='30000',
'table.type'='MERGE_ON_READ',
'compaction.tasks'='1',
'compaction.async.enabled'='true',
'compaction.delta_commits'='1',
'compaction.max_memory'='500',
'changelog.enabled'='true',
'read.streaming.enabled'='true',
'read.streaming.check.interval'='3',
'hive_sync.enable'='true',
'hive_sync.mode'='hms',
'hive_sync.metastore.uris'='thrift://master.am.com:9083,thrift://slave1.am.com:9083',
'hive_sync.db'='test',
'hive_sync.table'='test_a',
'hive_sync.username'='hive',
'hive_sync.support_timestamp'='true'
)
DDL是关键定义说明:
1、table_type是Hudi的表文件类型,定义了Hudi文件格式与索引组织方式。支持COPY_ON_WRITE 和MERGE_ON_READ,默认COPY_ON_WRITE 。
COPY_ON_WRITE:数据保存在列式文件中,如parquet。更新时可以定义数据版本或直接重写对应的parquet文件。支持快照读取和增量读取。
MERGE_ON_READ:数据保存在列式文件(如parquet) + 行记录级文件(如avro)中。数据更新时,会先将数据写到增量文件,然后会定时同步或异步合并成新的列式文件。支持快照读取和增量读取与读查询优化。
从MysqlCdc表同步数据到Hudi表
Flink SQL> set execution.checkpointing.interval=30sec;
Flink SQL> insert into hudi_test_a select * from mysql_test_a;
要设置execution.checkpointing.interval开启checkpoint,只有checkpoint开启时才会commit数据到hdfs,这时数据才可见。测试时可以设置较少的时间间隔以便于数据观察,线上设置应该根据实际情况设定,设置的间隔不宜过小。
命令执行后会显示成功提交了一个作业:
在Flink web ui上可以看到对应的作业信息:
在FlinkSql上验证Hudi表同步状态
Flink SQL> select * from hudi_test_a;