flink-cdc同步数据到doris中

1 创建数据库和表

1.1 数据库脚本

这样直接创建数据库是有问题,因为后面发现superset连接使用doris://root:123456@10.101.12.82:9030/internal.eayc?charset=utf8mb4

-- 创建数据库eayc
create database if not exists ods_eayc;
-- 创建数据表

1

2 数据同步

2.1 flnk-cdc

参考Flink CDC实时同步MySQL到Doris
Flink CDC 概述

2.1.1 最简单的单表同步

从下面的yml脚本可以看到,并没有doris中创建eayc_user表,应该是flink-cdc自动创建的。

#Mysql的参数配置
source:
  type: mysql
  hostname: 10.101.10.11
  port: 3306
  username: flink
  password: 123456
  tables: eayc.eayc_user
  server-id: 5400
  # server-time-zone: UTC
#Doris的参数配置
sink:
  type: doris
  fenodes: 10.101.11.2:8030,10.101.11.2:8030,10.101.11.3:8030
  username: root
  password: 123456
  table.create.properties.light_schema_change: true
  table.create.properties.replication_num: 1

route:
  - source-table: eayc.eayc_user
    sink-table: ods_eayc.eayc_user
pipeline:
  name: eayc to doris
  parallelism: 1

注意连接mysql的server-id的要唯一,否则提示下面的错误

A slave with the same server_uuid/server_id as this slave has connected to the master...
The 'server-id' in the mysql cdc connector should be globally unique, but conflicts happen now.

进入到flink的界面查看到错误日志,任务执行失败。下面报的错是mysql时区与flink配置不匹配。现在改生产库影响未知,不敢动,于是去掉server-time-zone: UTC设置。重新执行任务。
1

1
此时任务可以正常执行了,数据也可以正常过来了。因为flink-cdc是根据binlog,因此mysql变更,doris中的数据也实时更新过来。
1

2.1.2 多表同步

如下配置

source:
	tables: eayc.eayc_user,eayc.eayc_company,eayc.eayc_company_user
route:
  - source-table: eayc.eayc_user
    sink-table: ods_eayc.eayc_user
  - source-table: eayc.eayc_company
    sink-table: ods_eayc.eayc_company
  - source-table: eayc.eayc_company_user
    sink-table: ods_eayc.eayc_company_user

下面这种方式不支持,会报下面的错误:

Caused by: org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.String` from Array value (token `JsonToken.START_ARRAY`)
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: java.util.LinkedHashMap["tables"])

1

2.1.3 分表导入

taskmanager.numberOfTaskSlots默认为1,slot不够,就报下面的错误,因为是16C32G,于是我改成了8,parallelism.default默认也是1,我也改成了8,启动之后,没有报下面的错误,但是之前执行的任务没有了。

2025-02-19 15:05:07
java.util.concurrent.CompletionException: java.util.concurrent.CompletionException: org.apache.flink.runtime.jobmanager.scheduler.NoResourceAvailableException: Could not acquire the minimum required resources.
	at 

如果mysql的表没有主键,则报下面的错误,这个时候就需要修正原mysql表数据。

Caused by: org.apache.flink.table.api.ValidationException: 'scan.incremental.snapshot.chunk.key-column' must be set when the table doesn't have primary keys.

doris权限问题,这个是FE集群有问题,更改过来就好了。

reason: SchemaChange request error with Failed to schemaChange, response: {"msg":"Unauthorized","code":401,"data":"Access denied for user 'root@10.101.12.90' (using password: YES)","count":0}

可以看到下面,要获取acc的全部表,但是有一些是做了分表,需合并到其中doris的一张表里面,这个规则是有效的,开始parallelism: 1,我以为有一异常,只同步了一张表,过了几分钟才发现其他表也陆续进来。

source:
	tables: acc.\.*
route:
  - source-table: acc.acc_account_balance_\.*
    sink-table: acc.acc_account_balance
  - source-table: acc.acc_account_subject_\.*
    sink-table: acc.acc_account_subject
  - source-table: acc.acc_initial_balance_\.*
    sink-table: acc.acc_initial_balance
  - source-table: acc.acc_voucher_\.*
    sink-table: acc.acc_voucher
  - source-table: acc.acc_voucher_entry_\.*
    sink-table: acc.acc_voucher_entry    

于是将parallelism: 4,很快后台又抛异常。

java.util.concurrent.CompletionException: java.util.concurrent.CompletionException: org.apache.flink.runtime.jobmanager.scheduler.NoResourceAvailableException: Could not acquire the minimum required resources.

于是调整

taskmanager.memory.process.size: 8192m  # 增加 TaskManager 的内存

Flink CDC并行执行,会出现数据越界的问题。
Flink CDC报错ArrayIndexOutOfBoundsException解决思路

2.2 flink安装

2.2.1 单节点
tar -zxvf flink-1.18.0-bin-scala_2.12.tgz
# 配置环境变量
vi /etc/profile
export JAVA_HOME=/appdata/jdk1.8.0_181
export CLASSPATH=$JAVA_HOME/lib
export FLINK_HOME=/appdata/flink/flink-1.18.0
export PATH=$JAVA_HOME/bin:$FLINK_HOME/bin:$PATH
# 生效
source /etc/profile
# flink配置
vim conf/flink-conf.yaml
execution.checkpointing.interval: 3000
rest.bind-address: 0.0.0.0
cd bin
./start-cluster.sh
#
tar -zxvf flink-cdc-3.0.0-bin.tar.gz
# 执行任务
cd /appdata/flink/flink-cdc-3.0.0
bash bin/flink-cdc.sh /appdata/flink/job/eayc_to_doris.yml

flink-1.18.0
flink-cdc-3.0.0
mysql pipeline connector 3.0.0
doris pipeline connector 3.0.0
将上面两个connector放到cdc的lib目录
1

2.2.2 监控

1

1

3 变更

3.1 增加字段

eayc_company_user中增加了一个字段,但是
Caused by: java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: Unable to apply SchemaChangeEvent for table "eayc.eayc_company_user" without existing schema
   at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)
   at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895)
   at com.ververica.cdc.runtime.operators.schema.SchemaOperator.sendRequestToCoordinator(SchemaOperator.java:123)

下面的mysql的语句

ALTER TABLE `eayc`.`eayc_company_user` 
ADD COLUMN `is_main` char(1) NULL DEFAULT '0' COMMENT '是否主账号' AFTER `is_operational`;

因为我的任务是flink-cdc配置的,我看doris中这个字段已经自动加上了。
1

Flink CDC(Change Data Capture)是一种数据同步技术,可以从源数据库中捕获变更数据并将其同步到目标数据库中。DorisDB是一款分布式数据仓库,支持海量数据的存储和查询分析。下面以将数据DorisDB同步DorisDB为例,介绍如何使用Flink CDC实现数据同步。 1. 准备工作 在开始之前,需要安装好以下工具和环境: - DorisDB - Flink - Flink CDC 2. 创建数据源 首先需要创建一个数据源,用于从DorisDB中读取数据。可以使用Flink的JDBCInputFormat来读取DorisDB中的数据。在Flink中,可以使用以下代码创建一个JDBCInputFormat: ``` JDBCInputFormat jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(driverName) .setDBUrl(dbUrl) .setUsername(username) .setPassword(password) .setQuery("SELECT * FROM table") .finish(); ``` 其中,driverName、dbUrl、username和password是DorisDB的连接信息,"SELECT * FROM table"是要读取的表的SQL语句。 3. 创建数据同步任务 接下来需要创建一个Flink数据流任务,用于将从DorisDB中读取的数据同步到另一个DorisDB中。可以使用Flink的DataStream API来实现数据同步。以下是一个示例代码: ``` StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); DataStream<Row> sourceStream = env.createInput(jdbcInputFormat); DataStream<Row> sinkStream = sourceStream.map(new MapFunction<Row, Row>() { @Override public Row map(Row value) throws Exception { // 对数据进行转换 return value; } }); DorisDBOutputFormat dorisDBOutputFormat = new DorisDBOutputFormat(); dorisDBOutputFormat.setDrivername(driverName); dorisDBOutputFormat.setDBUrl(dbUrl); dorisDBOutputFormat.setUsername(username); dorisDBOutputFormat.setPassword(password); dorisDBOutputFormat.setTable(table); dorisDBOutputFormat.setBatchSize(batchSize); sinkStream.writeUsingOutputFormat(dorisDBOutputFormat); env.execute(); ``` 其中,sourceStream是从DorisDB中读取的数据流,sinkStream是经过转换后要写入到DorisDB的数据流。可以使用map函数对数据进行转换。DorisDBOutputFormat是一个自定义的输出格式,用于将数据写入到DorisDB中。在这个示例代码中,DorisDBOutputFormat的batchSize属性设置为1000,表示每1000条数据进行一次批量写入。 4. 运行数据同步任务 将上述代码保存为一个Java程序,并使用Flink命令行工具提交任务即可开始数据同步。在执行过程中,Flink CDC会自动监控DorisDB中的数据变更,将新增、修改、删除等操作同步到目标数据库中。 总的来说,使用Flink CDC实现DorisDB数据同步是一种高效、可靠的方式。它不仅可以帮助用户快速实现数据同步,还可以提高数据的实时性和准确性,为企业的数据分析和决策提供有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

warrah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值