canal同步es,sql注意事项

官网地址:https://github.com/alibaba/canal/wiki/Sync-ES

sql支持多表关联自由组合, 但是有一定的限制:

  1. 主表不能为子查询语句
  2. 只能使用left outer join即最左表一定要是主表
  3. 关联从表如果是子查询不能有多张表
  4. 主sql中不能有where查询条件(从表子查询中可以有where条件但是不推荐, 可能会造成数据同步的不一致, 比如修改了where条件中的字段内容)
  5. 关联条件只允许主外键的'='操作不能出现其他常量判断比如: on a.role_id=b.id and b.statues=1
  6. 关联条件必须要有一个字段出现在主查询语句中比如: on a.role_id=b.id 其中的 a.role_id 或者 b.id 必须出现在主select语句中

Elastic Search的mapping 属性与sql的查询值将一一对应(不支持 select *), 比如: select a.id as _id, a.name, a.email as _email from user, 其中name将映射到es mapping的name field, _email将 映射到mapping的_email field, 这里以别名(如果有别名)作为最终的映射字段. 这里的_id可以填写到配置文件的 _id: _id映射.


分享一下,我遇到的问题:

0:同步前,注意要先全量导入数据,不然会报错找不到元素

命令:

全量:

curl -XPOST http://localhost:9200/索引名.yml

删除索引:

curl -XDELETE http://ip:端口号/索引名称

1.from后的表不能用反引号

错误示范:select id,order from `order`;

正确:select id,order from order;

不会报错,只是不会同步数据

注意:关键字除外:例如 user,order等

2.尽量不在select中写聚合函数

报错:NotSuchElementException

错误示例:

sql 复制代码
         SELECT
         i.user_id AS _id,
         i.id AS id,
         i.user_id AS userId,
         u.image,
         i.isDel,
         IFNULL( w.oc, 0 )  AS oc,
         (select count(oc) from wxx w  where i.user_id = w.user_id) AS oc,
         DATE_FORMAT( i.create_time, '%Y-%m-%d %H:%i:%S' ) AS createTime 
         FROM
         uxx_iyy i
         LEFT JOIN uyy u ON u.user_id = i.user_id 

正确示例:

sql 复制代码
         SELECT
         i.user_id AS _id,
         i.id AS id,
         i.user_id AS userId,
         i.isDel,
         IFNULL( w.oc, 0 )  AS oc,
         DATE_FORMAT( i.create_time, '%Y-%m-%d %H:%i:%S' ) AS createTime 
         FROM
         uxx_iyy i
         LEFT JOIN uxx u ON u.user_id = i.user_id
         LEFT JOIN (
	        SELECT
		        user_id,
		        COUNT( oc ) AS oc 
	        FROM
		      wxxx_cyyy w 
	        GROUP BY
          w.user_id 
          ) w ON w.user_id = i.user_id 
相关推荐
xcLeigh几秒前
让大模型长出手脚,自己写SQL查数据库(Function Calling初探)
数据库·人工智能·sql·时序数据库·timechoai
yume_sibai1 小时前
06-Rust Web 开发实战(Axum 框架 + 数据库 + JWT 认证 + 中间件 + 部署)
前端·数据库·rust
AugustRed2 小时前
Neo4j 图数据库原理 + 应用场景简单介绍
数据库·neo4j
Gl�ria2 小时前
Redis 高可用架构对比
数据库·redis
IpdataCloud3 小时前
高可用的IP数据接口平台有哪些?从可用性、P99延迟到离线部署的评估框架(含代码)
数据库·tcp/ip·ip
呆萌很3 小时前
MySQL 数据库和表的管理操作(命令行)
数据库·mysql
IvorySQL4 小时前
当PostgreSQL“听懂”MySQL——协议兼容层的设计与实战
数据库·人工智能·postgresql
这个DBA有点耶4 小时前
银行核心系统数据库迁移怎么选?6 步法+5 个避坑指南
数据库·安全·架构
风哥2号4 小时前
数据库教程FGMT31‑Linux平台MySQL9.7安装配置与版本升级
数据库
hanchenxing5 小时前
前端异步任务三方案:Celery vs Redis Stream vs BullMQ 实战对比消息队列
前端·数据库·redis·异步任务·方案对比