现在想把这两张表形成一张大宽表,setting_key 里面的内容作为一个独立的列拼接在主表上面,然后将拼接后的数据同步到 ES 中。
转换的 SQL 如下
SELECT
a.id AS _id,
a.user_id AS user_id,
a.advertiser_name AS advertiser_name,
a.advertiser_email AS advertiser_email,
a.advertiser_phone AS advertiser_phone,
a.advertiser_type AS advertiser_type,
a.status ASstatus,
a.gmt_create AS gmt_create,
a.gmt_update AS gmt_update,
c.advertiser_id AS advertiser_id,
c._sign_time AS _sign_time,
c._sign_account AS _sign_account
FROM
ead_advertiser a
LEFTJOIN (
SELECT
b.advertiser_id AS advertiser_id,
max((
CASE
b.setting_key
WHEN'_sign_time'THEN
b.setting_value ELSE''END
)) AS _sign_time,
max((
CASE
b.setting_key
WHEN'_sign_account'THEN
b.setting_value ELSE''END
)) AS _sign_account
FROM
ead_advertiser_setting b
GROUPBY
b.advertiser_id
) c ON ((
a.id = c.advertiser_id
))
那么对应的适配的配置如下所示
dataSourceKey:defaultDSdestination:exampleouterAdapterKey:es-keygroupId:g1esMapping:_index:search-advertiser_info_id:_idupsert:true#pk: idsql:"SELECT a.id AS _id,a.user_id AS user_id,a.advertiser_name AS advertiser_name,a.advertiser_email AS advertiser_email,a.advertiser_phone AS advertiser_phone,a.advertiser_type AS advertiser_type,a.status AS status,a.gmt_create AS gmt_create,a.gmt_update AS gmt_update,c.advertiser_id AS advertiser_id,c._sign_time AS _sign_time,c._sign_account AS _sign_account FROM ead_advertiser a LEFT JOIN (SELECT b.advertiser_id AS advertiser_id, max((CASE b.setting_key WHEN '_sign_time' THEN b.setting_value ELSE '' END )) AS _sign_time,max((CASE b.setting_key WHEN '_sign_account' THEN b.setting_value ELSE '' END )) AS _sign_account FROM ead_advertiser_setting b GROUP BY b.advertiser_id ) c ON ((a.id = c.advertiser_id ))"# objFields:# _labels: array:;#etlCondition: " where a.gmt_update>='{0}'"commitBatch:1
简单说明:
dataSourceKey: defaultDS
destination: example
outerAdapterKey: es-key
groupId: g1
上面的几个配置,都需要跟启动器里面的配置保持一致。
esMapping:该配置是表示的是如何将 MySQL 的数据同步到 ES 中,配置比较复杂,其中
_index 表示 ES 的索引(需要提前创建);
_id 和 pk 二选一配置,表示使用查询出来的哪个字段作为唯一值;
upsert 表示对应主键的数据不存在的时候执行插入动作,存在的时候执行更新动作;
sql:表示要同步的数据,这个的 SQL 形式要求会比较严格
sql 支持多表关联自由组合, 但是有一定的限制:
主表不能为子查询语句
只能使用 left outer join 即最左表一定要是主表
关联从表如果是子查询不能有多张表
主 sql 中不能有 where 查询条件(从表子查询中可以有 where 条件但是不推荐, 可能会造成数据同步的不一致, 比如修改了 where 条件中的字段内容)
关联条件只允许主外键的'='操作不能出现其他常量判断比如: on a.role_id=b.id and b.statues=1
关联条件必须要有一个字段出现在主查询语句中比如: on a.role_id=b.id 其中的 a.role_id 或者 b.id 必须出现在主 select 语句中
2024-04-14 16:11:17.746 [main] INFO c.a.o.canal.adapter.launcher.loader.CanalAdapterLoader - Start adapter for canal-client mq topic: example-g1 succeed
2024-04-14 16:11:17.746 [Thread-4] INFO c.a.otter.canal.adapter.launcher.loader.AdapterProcessor - =============> Start to connect destination: example <=============
2024-04-14 16:11:17.746 [main] INFO c.a.o.canal.adapter.launcher.loader.CanalAdapterService - ## the canal client adapters are running now ......
2024-04-14 16:11:17.769 [main] INFO c.a.otter.canal.adapter.launcher.CanalAdapterApplication - Started CanalAdapterApplication in 5.912 seconds (JVM running for 7.732)
2024-04-14 16:11:17.963 [Thread-4] INFO c.a.otter.canal.adapter.launcher.loader.AdapterProcessor - =============> Subscribe destination: example succeed <=============
现在想把这两张表形成一张大宽表,setting_key 里面的内容作为一个独立的列拼接在主表上面,然后将拼接后的数据同步到 ES 中。
转换的 SQL 如下
SELECT
a.id AS _id,
a.user_id AS user_id,
a.advertiser_name AS advertiser_name,
a.advertiser_email AS advertiser_email,
a.advertiser_phone AS advertiser_phone,
a.advertiser_type AS advertiser_type,
a.status ASstatus,
a.gmt_create AS gmt_create,
a.gmt_update AS gmt_update,
c.advertiser_id AS advertiser_id,
c._sign_time AS _sign_time,
c._sign_account AS _sign_account
FROM
ead_advertiser a
LEFTJOIN (
SELECT
b.advertiser_id AS advertiser_id,
max((
CASE
b.setting_key
WHEN'_sign_time'THEN
b.setting_value ELSE''END
)) AS _sign_time,
max((
CASE
b.setting_key
WHEN'_sign_account'THEN
b.setting_value ELSE''END
)) AS _sign_account
FROM
ead_advertiser_setting b
GROUPBY
b.advertiser_id
) c ON ((
a.id = c.advertiser_id
))
那么对应的适配的配置如下所示
dataSourceKey:defaultDSdestination:exampleouterAdapterKey:es-keygroupId:g1esMapping:_index:search-advertiser_info_id:_idupsert:true#pk: idsql:"SELECT a.id AS _id,a.user_id AS user_id,a.advertiser_name AS advertiser_name,a.advertiser_email AS advertiser_email,a.advertiser_phone AS advertiser_phone,a.advertiser_type AS advertiser_type,a.status AS status,a.gmt_create AS gmt_create,a.gmt_update AS gmt_update,c.advertiser_id AS advertiser_id,c._sign_time AS _sign_time,c._sign_account AS _sign_account FROM ead_advertiser a LEFT JOIN (SELECT b.advertiser_id AS advertiser_id, max((CASE b.setting_key WHEN '_sign_time' THEN b.setting_value ELSE '' END )) AS _sign_time,max((CASE b.setting_key WHEN '_sign_account' THEN b.setting_value ELSE '' END )) AS _sign_account FROM ead_advertiser_setting b GROUP BY b.advertiser_id ) c ON ((a.id = c.advertiser_id ))"# objFields:# _labels: array:;#etlCondition: " where a.gmt_update>='{0}'"commitBatch:1
简单说明:
dataSourceKey: defaultDS
destination: example
outerAdapterKey: es-key
groupId: g1
上面的几个配置,都需要跟启动器里面的配置保持一致。
esMapping:该配置是表示的是如何将 MySQL 的数据同步到 ES 中,配置比较复杂,其中
_index 表示 ES 的索引(需要提前创建);
_id 和 pk 二选一配置,表示使用查询出来的哪个字段作为唯一值;
upsert 表示对应主键的数据不存在的时候执行插入动作,存在的时候执行更新动作;
sql:表示要同步的数据,这个的 SQL 形式要求会比较严格
sql 支持多表关联自由组合, 但是有一定的限制:
主表不能为子查询语句
只能使用 left outer join 即最左表一定要是主表
关联从表如果是子查询不能有多张表
主 sql 中不能有 where 查询条件(从表子查询中可以有 where 条件但是不推荐, 可能会造成数据同步的不一致, 比如修改了 where 条件中的字段内容)
关联条件只允许主外键的'='操作不能出现其他常量判断比如: on a.role_id=b.id and b.statues=1
关联条件必须要有一个字段出现在主查询语句中比如: on a.role_id=b.id 其中的 a.role_id 或者 b.id 必须出现在主 select 语句中
2024-04-14 16:11:17.746 [main] INFO c.a.o.canal.adapter.launcher.loader.CanalAdapterLoader - Start adapter for canal-client mq topic: example-g1 succeed
2024-04-14 16:11:17.746 [Thread-4] INFO c.a.otter.canal.adapter.launcher.loader.AdapterProcessor - =============> Start to connect destination: example <=============
2024-04-14 16:11:17.746 [main] INFO c.a.o.canal.adapter.launcher.loader.CanalAdapterService - ## the canal client adapters are running now ......
2024-04-14 16:11:17.769 [main] INFO c.a.otter.canal.adapter.launcher.CanalAdapterApplication - Started CanalAdapterApplication in 5.912 seconds (JVM running for 7.732)
2024-04-14 16:11:17.963 [Thread-4] INFO c.a.otter.canal.adapter.launcher.loader.AdapterProcessor - =============> Subscribe destination: example succeed <=============