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 
相关推荐
TDengine (老段)8 分钟前
TDengine IDMP 组态面板 —— 总体介绍
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
jwn99911 分钟前
【Mysql】:如何恢复误删的数据?
数据库·mysql
yashuk26 分钟前
Redis的安装教程(Windows+Linux)【超详细】
linux·数据库·redis
何中应28 分钟前
Easy-Es整合
大数据·spring boot·elasticsearch·搜索引擎
lzp079133 分钟前
mysql之联合索引
数据库·mysql
qq56801807634 分钟前
【MySQL】超详细MySQL常用日期格式转换函数、字符串函数、聚合函数(最新版)
数据库·mysql
逆境不可逃1 小时前
【从零入门23种设计模式21】行为型之空对象模式
java·开发语言·数据库·算法·设计模式·职场和发展
float_六七1 小时前
Git忽略规则终极指南
大数据·git·elasticsearch
灰阳阳1 小时前
Redis的缓存机制
数据库·redis·缓存
wenlonglanying1 小时前
【Redis】设置Redis访问密码
数据库·redis·缓存