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 
相关推荐
daad77738 分钟前
记录一个zmq客户端的性能调优
大数据·elasticsearch·搜索引擎
minji...1 小时前
MySQL数据库 (八) MySQL表的基本查询(下),truncate、group by、聚合函数、分组聚合统计
数据库·mysql·聚合函数·update·分组聚合统计
乐世东方客1 小时前
备份脚本记录(binlog文件+mysql+mongo)
android·数据库·mysql
暴力求解1 小时前
MySQL---数据类型
数据库·mysql
Nturmoils1 小时前
分页别写太顺手,LIMIT 背后还有排序和边界
数据库·后端
小饕1 小时前
RAG学习之【向量数据库】Milvus 从入门到精通:索引、检索、混合搜索一篇打通(RAG 必备)
数据库·人工智能·学习·milvus
kisdiem2 小时前
RAG ENGINEERING · 中文教程从文档到可靠答案
数据库
SilentSamsara2 小时前
向量数据库实战:Chroma/Milvus/Qdrant 选型与语义搜索应用
开发语言·数据库·人工智能·python·青少年编程·milvus
沪漂阿龙2 小时前
LangChain 系列之Agent:从固定流程到模型自主决策
服务器·数据库·langchain
zh_xuan3 小时前
PC端操作SQLite数据库
数据库·c++·sqlite