从数据格式转换的角度 flink cdc 如何写入paimon?

从一个测试用例着手

org/apache/flink/cdc/connectors/paimon/sink/v2/PaimonSinkITCase.java

java 复制代码
public void testSinkWithDataChange(String metastore, boolean enableDeleteVector)
            throws IOException, InterruptedException, Catalog.DatabaseNotEmptyException,
                    Catalog.DatabaseNotExistException, SchemaEvolveException {
         // 初始化paimon的元数据
        initialize(metastore);
                      // 创建paimonSink
        PaimonSink<Event> paimonSink =
                new PaimonSink<>(
                        catalogOptions, new PaimonRecordEventSerializer(ZoneId.systemDefault()));
        PaimonWriter<Event> writer = paimonSink.createWriter(new MockInitContext());
        Committer<MultiTableCommittable> committer = paimonSink.createCommitter();

        // insert
        writeAndCommit(
                writer, committer, createTestEvents(enableDeleteVector).toArray(new Event[0]));
        Assertions.assertThat(fetchResults(table1))
                .containsExactlyInAnyOrder(
                        Row.ofKind(RowKind.INSERT, "1", "1"), Row.ofKind(RowKind.INSERT, "2", "2"));

        // delete
        writeAndCommit(
                writer,
                committer,
                generateDelete(
                        table1, Arrays.asList(Tuple2.of(STRING(), "1"), Tuple2.of(STRING(), "1"))));

        Assertions.assertThat(fetchResults(table1))
                .containsExactlyInAnyOrder(Row.ofKind(RowKind.INSERT, "2", "2"));

        // update
        writeAndCommit(
                writer,
                committer,
                generateUpdate(
                        table1,
                        Arrays.asList(Tuple2.of(STRING(), "2"), Tuple2.of(STRING(), "2")),
                        Arrays.asList(Tuple2.of(STRING(), "2"), Tuple2.of(STRING(), "x"))));
        Assertions.assertThat(fetchResults(table1))
                .containsExactlyInAnyOrder(Row.ofKind(RowKind.INSERT, "2", "x"));

        if (enableDeleteVector) {
            Assertions.assertThat(fetchMaxSequenceNumber(table1.getTableName()))
                    .containsExactlyInAnyOrder(
                            Row.ofKind(RowKind.INSERT, 1L), Row.ofKind(RowKind.INSERT, 3L));
        } else {
            Assertions.assertThat(fetchMaxSequenceNumber(table1.getTableName()))
                    .containsExactlyInAnyOrder(
                            Row.ofKind(RowKind.INSERT, 1L),
                            Row.ofKind(RowKind.INSERT, 2L),
                            Row.ofKind(RowKind.INSERT, 3L));
        }
    }

整体流程:

  1. 创建变更事件。

  2. 创建PaimonWriter

  3. 对于每一个事件event,都调用paimonWriter中的write。具体又包括以下几个步骤

    • 序列化event为paimon 中的数据格式genericRow
    • 创建paimon的write算子(StoreSinkWrite),将genericRow写入paimon表. 进入paimon内部的写入逻辑。
      • 从每一条row中提取rowkind(INSERT,UPDATE_BEFORE,UPDATE_AFTER,DELETE)
      • 提取data,MergeTreeWriter中执行两个操作,内存够,就把数据put到内存,内存不够就flush到磁盘

源码

Flink cdc 代码

  • 创建测试data change event
  • 遍历事件
  • 序列化
  • 核心转换逻辑
  • paimon的写入

以下是paimon的代码

总结

回到本文的标题:从数据格式转换的角度 flink cdc 如何把数据处理成paimon?

在flink cdc端 核心是 convertEventToGenericRow,处理dataChangeEvent 和 genericRow 对应。

为什么要这么对应?

相关推荐
南讯股份Nascent44 分钟前
颖通电商全渠道项目启动会成功举办
大数据·人工智能·物联网
猴子15516811 小时前
问卷逻辑跳转功能谁更强?2026年四款工具条件设置实测
大数据
Elastic 中国社区官方博客1 小时前
使用 Elasticsearch 作为 Grafana 的直接替代 Prometheus 后端
大数据·数据库·sql·elasticsearch·搜索引擎·grafana·prometheus
XTIOT6661 小时前
CRPT 诚实标识采集落地技术实践:分工况硬件选型与合规数据标准化解决方案
大数据·运维·人工智能·嵌入式硬件·物联网
杨婷1231 小时前
2026 企业微信会话存档服务商选型指南:一维助手方案深度解析
大数据·数据库·企业微信
2301_780356701 小时前
全视通智慧医院解决方案:构建数智化医疗新生态
大数据·网络·人工智能
2601_949936962 小时前
2026财务分析师岗位,如何提升工作能力
大数据
媒介发稿小能手2 小时前
权威+垂直+全域媒体资源:选对GEO发稿平台,抢占AI搜索高地
大数据·人工智能·媒体
滴图客户服务经理3 小时前
出行APP里的“预计到达时间“是怎么算出来的?
大数据·经验分享·百度
@SmartSi3 小时前
新一代实时数据集成框架 Flink CDC 3.0 架构解析
flink·cdc