将 Apache Hudi 与 Apache Flink 结合使用
Apache Hudi 是一个开源数据管理框架,包含插入、更新、更新插入和删除等记录级操作,可用于简化数据管理和数据管道开发。与 Amazon S3 中的高效数据管理相结合,Hudi 允许实时摄取和更新数据。Hudi 会维护在数据集上运行的所有操作的元数据,因此所有操作都能保持原子性和一致性。
Apache Hudi 已在 Amazon EMR on EKS 上投入使用,搭配 Apache Flink 和 Amazon EMR 7.2.0 及更高版本。请参阅以下步骤,了解如何开始和提交 Apache Hudi 作业。
提交 Apache Hudi 作业
请参阅以下步骤,了解如何提交 Apache Hudi 作业。
-
创建一个名为
default
的 AWS Glue 数据库。aws glue create-database --database-input "{\"Name\":\"default\"}"
-
按照 Flink Kubernetes Operator SQL 示例
构建 flink-sql-runner.jar
文件。 -
创建如下所示的 Hudi SQL 脚本。
CREATE CATALOG hudi_glue_catalog WITH ( 'type' = 'hudi', 'mode' = 'hms', 'table.external' = 'true', 'default-database' = 'default', 'hive.conf.dir' = '/glue/confs/hive/conf/', 'catalog.path' = 's3://
<hudi-example-bucket>
/FLINK_HUDI/warehouse/' ); USE CATALOG hudi_glue_catalog; CREATE DATABASE IF NOT EXISTS hudi_db; use hudi_db; CREATE TABLE IF NOT EXISTS hudi-flink-example-table( uuid VARCHAR(20), name VARCHAR(10), age INT, ts TIMESTAMP(3), `partition` VARCHAR(20) ) PARTITIONED BY (`partition`) WITH ( 'connector' = 'hudi', 'path' = 's3://<hudi-example-bucket>
/hudi-flink-example-table', 'hive_sync.enable' = 'true', 'hive_sync.mode' = 'glue', 'hive_sync.table' = 'hudi-flink-example-table', 'hive_sync.db' = 'hudi_db', 'compaction.delta_commits' = '1', 'hive_sync.partition_fields' = 'partition', 'hive_sync.partition_extractor_class' = 'org.apache.hudi.hive.MultiPartKeysValueExtractor', 'table.type' = 'COPY_ON_WRITE' ); EXECUTE STATEMENT SET BEGIN INSERT INTO hudi-flink-example-table VALUES ('id1','Alex',23,TIMESTAMP '1970-01-01 00:00:01','par1'), ('id2','Stephen',33,TIMESTAMP '1970-01-01 00:00:02','par1'), ('id3','Julian',53,TIMESTAMP '1970-01-01 00:00:03','par2'), ('id4','Fabian',31,TIMESTAMP '1970-01-01 00:00:04','par2'), ('id5','Sophia',18,TIMESTAMP '1970-01-01 00:00:05','par3'), ('id6','Emma',20,TIMESTAMP '1970-01-01 00:00:06','par3'), ('id7','Bob',44,TIMESTAMP '1970-01-01 00:00:07','par4'), ('id8','Han',56,TIMESTAMP '1970-01-01 00:00:08','par4'); END; -
将 Hudi SQL 脚本和
flink-sql-runner.jar
文件上传到 S3 位置。 -
在
FlinkDeployments
YAML 文件中,将hudi.enabled
设置为true
。spec: flinkConfiguration: hudi.enabled: "true"
-
创建一个 YAML 文件来运行配置。本示例文件名为
hudi-write.yaml
。apiVersion: flink.apache.org/v1beta1 kind: FlinkDeployment metadata: name: hudi-write-example spec: flinkVersion: v1_18 flinkConfiguration: taskmanager.numberOfTaskSlots: "2" hudi.enabled: "true" executionRoleArn: "
<JobExecutionRole>
" emrReleaseLabel: "emr-7.3.0-flink-latest" jobManager: highAvailabilityEnabled: false replicas: 1 resource: memory: "2048m" cpu: 1 taskManager: resource: memory: "2048m" cpu: 1 job: jarURI: local:///opt/flink/usrlib/flink-sql-runner.jar args: ["/opt/flink/scripts/hudi-write.sql"] parallelism: 1 upgradeMode: stateless podTemplate: spec: initContainers: - name: flink-sql-script-download args: - s3 - cp - s3://<s3_location>
/hudi-write.sql - /flink-scripts image: amazon/aws-cli:latest imagePullPolicy: Always resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /flink-scripts name: flink-scripts - name: flink-sql-runner-download args: - s3 - cp - s3://<s3_location>
/flink-sql-runner.jar - /flink-artifacts image: amazon/aws-cli:latest imagePullPolicy: Always resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /flink-artifacts name: flink-artifact containers: - name: flink-main-container volumeMounts: - mountPath: /opt/flink/scripts name: flink-scripts - mountPath: /opt/flink/usrlib name: flink-artifact volumes: - emptyDir: {} name: flink-scripts - emptyDir: {} name: flink-artifact -
将 Flink Hudi 作业提交到 Flink Kubernetes Operator。
kubectl apply -f hudi-write.yaml