分享

Debezium-Flink-Hudi:实时流式CDC

本帖最后由 Mirinda 于 2021-5-27 14:09 编辑

问题导读:
1.什么是Debezium?
2.我们能用Debezium做些什么?
3.如何看待Debezium常规使用架构?


1. 什么是Debezium
Debezium是一个开源的分布式平台,用于捕捉变化数据(change data capture)的场景。它可以捕捉数据库中的事件变化(例如表的增、删、改等),并将其转为事件流,使得下游应用可以看到这些变化,并作出指定响应。

2. Debezium常规使用架构
根据Debezium官网[1]提供的常规使用的架构图:

a1.png


可以看到,在对RMSDB数据源做数据摄入时,使用的是Kafka Connect。Source Connector从数据库中获取记录并发送到Kafka;Sink Connectors将记录从Kafka Topic 传播到其他系统中。

上图中分别对MySQL 与 PostgreSQL部署了connector:
  1.MySQL connector使用的是一个客户端库访问binlog
  2.PostgreSQL connector读取的是的一个replication stream

另一种方式是仅部署Debezium Server(不带Kakfa),架构如下图所示:

a2.png


此方式使用的是Debezium自带的Source Connector。数据库端的事件会被序列化为JSON或Apache Avro格式,然后发送到其他消息系统如Kinesis、Apache Pulsar等。

3. 部署Debezium
在此次部署中,我们使用的均为AWS 资源,架构图如下:

a3.png


此架构说明:
  1.使用AWS RDS MySQL作为源端数据库
  2.使用AWS EKS 部署Kafka Connector
  3.使用AWS MSK 部署Kafka
  4.Kafka下游为AWS EMR,运行Flink,实现增量载入Hudi表

此处会省去创建AWS RDS、EKS、MSK 以及 EMR的过程,主要介绍搭建过程中的具体使用到的方法。

3.1. AWS EKS部署Kafka Connector
3.1.1. 安装Operator Framework 与 Strimzi Apache Kafka Operator

先安装Operator Framework[2],它是一个用来管理k8s原生应用(Operator)的开源工具。然后安装Kafka可以使用Strimzi Apache Kafka Operator[3]。
安装最新版 operator-framework[4],当前版本为 0.18.1:
  1. kubectl apply -f https://github.com/operator-fram ... d/v0.18.1/crds.yaml
  2. kubectl apply -f https://github.com/operator-fram ... ad/v0.18.1/olm.yaml
复制代码

安装Strimzi Apache Kafka Operator:
  1. kubectl apply -f https://operatorhub.io/install/strimzi-kafka-operator.yaml
  2. $ kubectl get csv -n operators
  3. NAME                               DISPLAY   VERSION   REPLACES                           PHASE
  4. strimzi-cluster-operator.v0.23.0   Strimzi   0.23.0    strimzi-cluster-operator.v0.22.1   Succeeded
复制代码

3.1.2. 打包Debezium的MySQL Kafka Connector

下面部署Debezium 的 MySQL Kafka Connector。源端数据库为MySQL,所以下载 debezium-connector-mysql,版本为1.5.0.Final:
  1. wget https://repo1.maven.org/maven2/i ... r-mysql/1.5.0.Final
  2. /debezium-connector-mysql-1.5.0.Final-plugin.tar.gz
  3. tar -zxvf debezium-connector-mysql-1.5.0.Final-plugin.tar.gz
复制代码

然后我们build一个自定义的debezium-connector-mysql Docker镜像, 创建Dockerfile:
  1. FROM strimzi/kafka:0.20.1-kafka-2.6.0
  2. USER root:root
  3. RUN mkdir -p /opt/kafka/plugins/debezium
  4. COPY ./debezium-connector-mysql/ /opt/kafka/plugins/debezium/
  5. USER 1001
复制代码

Bulid镜像并推送:
  1. # 登录aws ecr
  2. > aws ecr get-login --no-include-email
复制代码

3.1.3. 部署 Debezium MySQL Connector
  1. $ cat debezium-mysql-connector.yaml
  2. apiVersion: kafka.strimzi.io/v1beta2
  3. kind: KafkaConnect
  4. metadata:
  5.   name: debezium-connector
  6.   namespace: kafka
  7. #  annotations:
  8. #  # use-connector-resources configures this KafkaConnect
  9. #  # to use KafkaConnector resources to avoid
  10. #  # needing to call the Connect REST API directly
  11. #    strimzi.io/use-connector-resources: "true"
  12. spec:
  13.   version: 2.8.0
  14.   replicas: 1
  15.   bootstrapServers: xxxx
  16.   image: xxxxxx.dkr.ecr.cn-north-1.amazonaws.com.cn/connect-debezium:latest
  17.   config:
  18.     group.id: connect-cluster
  19.     offset.storage.topic: connect-cluster-offsets
  20.     config.storage.topic: connect-cluster-configs
  21.     status.storage.topic: connect-cluster-status
  22.     # -1 means it will use the default replication factor configured in the broker
  23.     config.storage.replication.factor: -1
  24.     offset.storage.replication.factor: -1
  25. status.storage.replication.factor: -1
  26. $ kubectl apply -f debezium-mysql-connector.yaml
  27. $ kubectl get pods -n kafka
  28. NAME                                          READY   STATUS    RESTARTS   AGE
  29. debezium-connector-connect-69c98cc784-kqvww   1/1     Running   0          5m44s
复制代码

替换其中的bootstrapServers为AWS MSK bootstrapServers;image为3.1.2 步骤中打包的镜像地址。

使用本地代理访问Kafka Connect 服务,并验证可用 Connectors:
  1. $ kubectl port-forward service/debezium-connector-connect-api 8083:8083 -n kafka
  2. $ curl localhost:8083/connector-plugins
  3. [{
  4.     "class": "io.debezium.connector.mysql.MySqlConnector",
  5.     "type": "source",
  6.     "version": "1.5.0.Final"
  7. }, {
  8.     "class": "org.apache.kafka.connect.file.FileStreamSinkConnector",
  9.     "type": "sink",
  10.     "version": "2.6.0"
  11. }
  12.    …
  13. ]
复制代码

编写 MySQL Connector 配置文件:
  1. $ cat mysql-connector-tang.json
  2. {
  3.     "name": "mysql-connector",
  4.     "config": {
  5.          "connector.class": "io.debezium.connector.mysql.MySqlConnector",
  6.          "tasks.max": "1",
  7.          "database.hostname": "xxxxx",
  8.          "database.port": "3306",
  9.          "database.user": "xxxx",
  10.          "database.password": "xxxx",
  11.          "database.server.id": "184055",
  12.          "database.server.name": "mysql-tang",
  13.          "database.include.list": "tang ",
  14.          "database.history.kafka.bootstrap.servers": "xxxxx",
  15.          "database.history.kafka.topic": " changes.tang"
  16.      }
  17. }
复制代码

将配置推送到 Kafka Connector:
  1. $ cat mysql-connector.json | curl -i -X POST -H "Accept:application/json" -H "Content-Type:application/json" localhost:8083/connectors/ -d @-
  2. HTTP/1.1 201 Created
  3. Date: Fri, 21 May 2021 11:00:25 GMT
  4. Location: http://localhost:8083/connectors/mysql-connector-tang
  5. Content-Type: application/json
  6. Content-Length: 733
  7. Server: Jetty(9.4.24.v20191120)
复制代码

3.1.4. 验证

部署完成后,在AWS RDS MySQL 中创建库与测试表,并写入测试数据。此时在AWS MSK中未发现对应 events生成。查看connector 的pod 日志:
  1. $ kubectl logs debezium-connector-connect-69c98cc784-kqvww -n kafka
  2. ….
  3. io.debezium.DebeziumException: The MySQL server is not configured to use a ROW binlog_format, which is required for this connector to work properly. Change the MySQL configuration to use a binlog_format=ROW and restart the connector.
  4.         at io.debezium.connector.mysql.MySqlConnectorTask.validateBinlogConfiguration(MySqlConnectorTask.java:203)
  5.         at io.debezium.connector.mysql.MySqlConnectorTask.start(MySqlConnectorTask.java:85)
  6.         at io.debezium.connector.common.BaseSourceTask.start(BaseSourceTask.java:130)
复制代码

可以看到MySQLConnector需要MySQL server 配置 binlog_format 为 ROW。

修改此配置后,再次通过进行kafka-console-consumer.sh 进行验证,即可看到测试数据库中的所有事件:
  1. $ ./kafka-console-consumer.sh --bootstrap-server xxxx --topic schema-changes.inventory --from-beginning
  2. {
  3.   "source" : {
  4.     "server" : "mysql-tang"
  5.   },
  6.   "position" : {
  7.     "ts_sec" : 1621585297,
  8.     "file" : "mysql-bin-changelog.000015",
  9.     "pos" : 511,
  10.     "snapshot" : true
  11.   },
  12.   "databaseName" : "inventory",
  13.   "ddl" : "CREATE DATABASE `inventory` CHARSET latin1 COLLATE latin1_swedish_ci",
  14.   "tableChanges" : [ ]
  15. }
  16. {
  17.   "source" : {
  18.     "server" : "mysql-tang"
  19.   },
  20.   "position" : {
  21.     "ts_sec" : 1621585297,
  22.     "file" : "mysql-bin-changelog.000015",
  23.     "pos" : 511,
  24.     "snapshot" : true
  25.   },
  26.   "databaseName" : "inventory",
  27.   "ddl" : "CREATE TABLE `test` (\n  `id` int(11) DEFAULT NULL,\n  `name` varchar(10) DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=latin1",
  28.   "tableChanges" : [ {
  29.     "type" : "CREATE",
  30.     "id" : ""inventory"."test"",
  31.     "table" : {
  32.       "defaultCharsetName" : "latin1",
  33.       "primaryKeyColumnNames" : [ ],
  34.       "columns" : [ {
  35.         "name" : "id",
  36.         "jdbcType" : 4,
  37.         "typeName" : "INT",
  38.         "typeExpression" : "INT",
  39.         "charsetName" : null,
  40.         "length" : 11,
  41.         "position" : 1,
  42.         "optional" : true,
  43.         "autoIncremented" : false,
  44.         "generated" : false
  45.       }, {
  46.         "name" : "name",
  47.         "jdbcType" : 12,
  48.         "typeName" : "VARCHAR",
  49.         "typeExpression" : "VARCHAR",
  50.         "charsetName" : "latin1",
  51.         "length" : 10,
  52.         "position" : 2,
  53.         "optional" : true,
  54.         "autoIncremented" : false,
  55.         "generated" : false
  56.       } ]
  57.     }
  58.   } ]
  59. }
复制代码

4. Flink 消费Debezium 类型消息
RMDB数据经Debezium Connector写入Kafka后,先由Flink进行消费。可以参考Flink官网中对Debezium格式的处理代码[5]:
  1. CREATE TABLE topic_products (
  2.   -- schema is totally the same to the MySQL "products" table
  3.   id BIGINT,
  4.   name STRING,
  5.   description STRING,
  6.   weight DECIMAL(10, 2)
  7. ) WITH (
  8. 'connector' = 'kafka',
  9. 'topic' = 'products_binlog',
  10. 'properties.bootstrap.servers' = 'localhost:9092',
  11. 'properties.group.id' = 'testGroup',
  12. -- using 'debezium-json' as the format to interpret Debezium JSON messages
  13. -- please use 'debezium-avro-confluent' if Debezium encodes messages in Avro format
  14. 'format' = 'debezium-json'
  15. )
复制代码

5. 写入Hudi表
RMDB数据经Debezium Connector写入Kafka后,接下来通过 Flink 将流式数据写入到一张Hudi表,实现实时数据到Hudi。此部分可以参考Hudi官网对Flink支持的代码[6]:
  1. CREATE TABLE t1(
  2.     uuid VARCHAR(20), -- you can use 'PRIMARY KEY NOT ENFORCED' syntax to mark the field as record key
  3.     name VARCHAR(10),
  4.    age INT,
  5.    ts TIMESTAMP(3),
  6.    `partition` VARCHAR(20)
  7. )
  8. PARTITIONED BY (`partition`)
  9. WITH (
  10.     'connector' = 'hudi',
  11.     'path' = 'table_base_path',
  12.     'write.tasks' = '1', -- default is 4 ,required more resource
  13.     'compaction.tasks' = '1', -- default is 10 ,required more resource
  14.     'table.type' = 'MERGE_ON_READ' -- this creates a MERGE_ON_READ table, by default is COPY_ON_WRITE
  15. );
复制代码

5.1. 依赖包问题
在这个过程中,有一点需要注意的是,在使用Hudi官网提到的 hudi-flink-bundle_2.11-0.7.0.jar (或hudi-flink-bundle_2.11-0.8.0.jar) 时,会遇到以下问题:
  1. Caused by: org.apache.flink.table.api.ValidationException: Could not find any factory for identifier 'hudi' that implements 'org.apache.flink.table.factories.DynamicTableFactory' in the classpath.
复制代码

从报错来看,hudi-flink-bundle_2.11-0.7.0.jar版本并未提供flink 与 hudi 通过 “connector=hudi” 集成的功能。但是在最新版的Hudi tutorial中有提到(当前为hudi 0.9 版本)需要hudi-flink-bundle_2.1?-*.*.*.jar。

于是笔者尝试了手动编译hudi 0.9 版本,build出hudi-flink-bundle_2.11-0.9.0-SNAPSHOT.jar。但是在编译过程中遇到以下问题:
  1. [ERROR] Failed to execute goal on project hudi-hadoop-mr: Could not resolve dependencies for project org.apache.hudi:hudi-hadoop-mr:jar:0.9.0-SNAPSHOT: Failed to collect dependencies at org.apache.hive:hive-exec:jar:core:2.3.2 -> org.apache.calcite:calcite-core:jar:1.10.0 -> org.pentaho:pentaho-aggdesigner-algorithm:jar:5.1.5-jhyde: Failed to read artifact descriptor for org.pentaho:pentaho-aggdesigner-algorithm:jar:5.1.5-jhyde: Could not transfer artifact org.pentaho:pentaho-aggdesigner-algorithm:pom:5.1.5-jhyde from/to maven-default-http-blocker (http://0.0.0.0/): Blocked mirror for repositories: [nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public/, default, releases), datanucleus (http://www.datanucleus.org/downloads/maven2, default, releases), glassfish-repository (http://maven.glassfish.org/content/groups/glassfish, default, disabled), glassfish-repo-archive (http://maven.glassfish.org/content/groups/glassfish, default, disabled), apache.snapshots (http://repository.apache.org/snapshots, default, snapshots), central (http://repo.maven.apache.org/maven2, default, releases), conjars (http://conjars.org/repo, default, releases+snapshots)] -> [Help 1]
复制代码

此问题说明的是无法从提供的任一maven 源中拉取org.pentaho:pentaho-aggdesigner-algorithm:jar:5.1.5-jhyde 包。
解决此问题的方法是:手动下载此jar包(位置为https://public.nexus.pentaho.org ... thm-5.1.5-jhyde.jar
),并install 到本地 maven仓库中,再修改对应编译模块的pom文件,加上此依赖说明即可。

Maven install package的命令如:
  1. ../apache-maven-3.8.1/bin/mvn install:install-file -DgroupId=org.pentaho -DartifactId=pentaho-aggdesigner-algorithm -Dversion=5.1.5-jhyde -Dpackaging=jar -Dfile=/home/hadoop/.m2/repository/org/pentaho/pentaho-aggdesigner-algorithm/5.15-jhyde/pentaho-aggdesigner-algorithm-5.15-jhyde.jar
复制代码

此过程完成后,可以成功解决flink sql 映射 hudi 表的问题。

5.2. Flink 版本问题
在AWS EMR 最新版 emr-5.33.0 下,Flink版本为1.12.1,而hudi 0.9 版本编译所需的Flink版本为1.12.2。

笔者在编译0.9 版本 hudi 的 hudi-flink-bundle_2.11-0.9.0-SNAPSHOT.jar后,在EMR-5.33.0 下使用,遇到版本不一致报出的 NoSuchMethod问题。尝试各种jar包替换后仍未解决。

所以最终使用的是自建Flink 1.12.2 版本集群。

6. Flink消费Debezium与写入Hudi测试
使用简单的测试表进行测试。

MySQL中建表:
  1. create table customer(id varchar(20), name varchar(10), age int, user_level varchar(10));
复制代码

启动Flink程序,主体代码为:
  1. package cdc
  2. import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
  3. import org.apache.flink.table.api.{EnvironmentSettings, SqlDialect, TableResult}
  4. import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment
复制代码

提交Flink程序后正常运行:

a4.png



使用MySQL procedure 不断向customer 表中写入数据。可以观察到hudi路径下出现对应分区路径,并出现结果文件:
  1. $ hdfs dfs -ls s3://xxx-hudi/customer/
  2. Found 3 items
  3. drwxrwxrwx   - hadoop hadoop          0 1970-01-01 00:00 s3://tang-hudi/customer/.hoodie
  4. drwxrwxrwx   - hadoop hadoop          0 1970-01-01 00:00 s3://tang-hudi/customer/lv2
  5. drwxrwxrwx   - hadoop hadoop          0 1970-01-01 00:00 s3://tang-hudi/customer/lv3
  6. $ hdfs dfs -ls s3://xxx-hudi/customer/lv2/
  7. Found 2 items
  8. -rw-rw-rw-   1 hadoop hadoop         93 2021-05-24 13:52 s3://tang-hudi/customer/lv2/.hoodie_partition_metadata
  9. -rw-rw-rw-   1 hadoop hadoop    2092019 2021-05-24 14:00 s3://tang-hudi/customer/lv2/e8195cc8-aae4-4462-8605-7f4eceac90ce_0-1-0_20210524134250.parquet
复制代码

7. 验证hudi表
首先使用 AWS S3 Select 查询目标parquet文件,可以拿到正确结果:

a5.png


但是,而后分别使用了 SparkSQL与 Hive对Hudi表地址进行映射并执行读取操作,结果均失败。暂未得出失败原因。

初步判断可能与包环境依赖有关。由于最新版AWS EMR emr-5.33.0 下,Flink版本为1.12.1,而hudi 0.9 版本编译所需的Flink版本为1.12.2。所以笔者使用了自建的Flink集群,当时仅考虑了Flink与Hudi版本保持一致,但未将Spark与Hive版本纳入考虑范围内,所以可能导致了此原因。



8. 总结
总体来看,Debezium是一个非常方便部署使用的CDC工具,可以有效地将RMSDB数据抽取到消息系统中,供不同的下游应用消费。而Flink直接对接Debezium与Hudi的功能,极大方便了数据湖场景下的实时数据ingestion。

References
[1] https://debezium.io/documentation/reference/1.5/architecture.html

[2] https://operatorhub.io

[3] https://operatorhub.io/operator/strimzi-kafka-operator

[4] https://github.com/operator-fram ... e-manager/releases/

[5] https://ci.apache.org/projects/f ... e/formats/debezium/

[6] https://hudi.apache.org/docs/flink-quick-start-guide.html





最新经典文章,欢迎关注公众号


原文链接:https://blog.csdn.net/tsjjjjj/article/details/117235883









没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条