当前位置: 首页 > news >正文

tidb实时同步到mysql

客户要求实时同步表的数据到mysql,但这个表在tidb。
测试直接通过tidb cdc写入到mysql,有些字段是null,所以中间加了一个kafka实现

客户库中创建表
CREATE TABLE tb_1 (
    id bigint primary key,
    cid bigint,
    gid bigint,
    fee DECIMAL(10,2),
    created_at timestamp,
    type smallint,
    remark string ,
    key i_cid(cid)
);
tidb配置cdc,写入到kafka

1、tidb添加cdc组件
2、配置cdc任务
cat your.toml

case-sensitive = true
enable-old-value = true
[filter]
rules = ['db.tb_1']

3、启动任务

tiup ctl:v5.3.0 cdc changefeed create --pd=http://pd-ip:2379 \
--sink-uri="kafka://kafka-ip:9092/your-topic?kafka-version=1.1.1&partition-num=1&max-message-bytes=67108864&replication-factor=1&protocol=canal-json" --changefeed-id="my" --sort-engine="unified" \
--start-ts=453870757254529193 --config your.toml

protocol=canal-json 使用这个格式
–start-ts这个通过一次导出查看,cat db/metadata

dumpling -u root -p pwd -h tidb-ip -P 3306  -F 1GiB --compress gzip -t 2 -o db -B db -T db.tb --where "cid=123456"

4、更新一条数据,看看kafka是不是有了

创建SQL作业,从kafka消费后入mysql
CREATE TABLE tb_1 (
    id bigint,
    cid bigint,
    gid bigint,
    fee DECIMAL(10,2),
    create_time timestamp,
    type smallint,
    remark string,
    PRIMARY KEY (`id`) NOT ENFORCED
) WITH (
  -- 定义 Kafka 参数
  'connector' = 'kafka',
  'topic' = 'your-topic',
  'scan.startup.mode' = 'latest-offset', 
  'properties.bootstrap.servers' = 'ip:9092',
  'properties.group.id' = 'your-group',
  'format' = 'canal-json', -- tidb 支持该方式
  'canal-json.ignore-parse-errors' = 'false'
);



CREATE TABLE kh_tb_1 (
    id bigint,
    cid bigint,
    gid bigint,
    fee DECIMAL(10,2),
    create_time timestamp,
    type smallint,
    remark string,
    PRIMARY KEY (`id`) NOT ENFORCED
) WITH (
  'connector' = 'jdbc',
  'url' = 'jdbc:mysql://xxxxxx:3306/db?rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai',
  'table-name' = 'tb_1',
  'username' = 'user',
  'password' = 'pwd',
  'sink.buffer-flush.max-rows' = '5000',
  'sink.buffer-flush.interval' = '2s',
  'sink.max-retries' = '10'
);

insert into kh_tb_1 select * from tb_1 where cid=xxxxxxx;

最后启动作业任务即可。

相关文章:

  • 多模态论文笔记——TECO
  • OpenCV二值化处理
  • 上位机知识篇---Docker容器
  • 用AI学历史1——中国通史
  • Python pathlib模块介绍
  • 【面试】Redis 常见面试题
  • 专利申请流程详解:从创意到授权的完整指南
  • Nginx 负载均衡详解
  • 在UBUNTU下搭建Deepseek
  • 力扣hot100——螺旋矩阵 超简单易懂的模拟搜索方法
  • 专题--Redis
  • STM32 HAL库标准库+ESP8266+机智云
  • 树莓派4基于Debian GNU/Linux 12 (Bookworm)添加多个静态ipv4网络
  • 【找工作】C++和算法复习(自用)
  • MySQL(2)索引篇
  • 金融数据库数字化转型:性能优化的实战经验
  • 【自动化脚本工具】Hammerspoon (Mac)
  • OpenCV对比度增强
  • 【Python】Python入门——笔记合集
  • 5.5 Soft Prompt技术:任务特定微调的新范式
  • 国家能源局通报上月投诉情况:赤峰有群众反映电费异常增高,已退费
  • 青海西宁市城西区副区长于媛媛主动投案,接受审查调查
  • 路边“僵尸车”被人以1450元卖了,嫌疑人被刑拘
  • “杭州六小龙”的招聘迷局
  • 法院为“外卖骑手”人身权益撑腰:依法认定实际投保人地位
  • 非法收受财物逾1648万,湖南原副厅级干部康月林一审被判十年半