开发者社区> 问答> 正文

flink sql lookup join中维表不可以是视图吗?

lookup join用的维表需要从两张mysql表做关联后得到,因此创建了一个视图。但发现flink sql不支持lookup join关联视图,会抛 Temporal Table Join requires primary key in versioned table, but no primary key can be found.

请问这种情况要怎么解决?

CREATE VIEW query_mer_view (mer_cust_id, update_time) AS SELECT a.mer_cust_id, k.update_time FROM ka_mer_info k INNER JOIN adp_mer_user_info a on k.mer_cust_id = a.mer_cust_id where k.mer_cust_id <> '';

SELECT DATE_FORMAT(c.create_time, 'yyyy-MM-dd') AS payment_date, c.mer_cust_id,

c.trans_amt, CASE c.trans_stat WHEN 'S' THEN c.trans_amt ELSE 0 END as succ_amt ,

1 as trans_cnt, CASE c.trans_stat WHEN 'S' THEN 1 ELSE 0 END as succ_cnt , CASE c.trans_stat WHEN 'F' THEN 1 ELSE 0 END as fail_cnt

FROM charge_log as c LEFT JOIN query_mer_view FOR SYSTEM_TIME AS OF c.proc_time AS q ON c.mer_cust_id = q.mer_cust_id;*来自志愿者整理的flink

展开
收起
雪哥哥 2021-12-08 19:40:09 2354 0
1 条回答
写回答
取消 提交回答
  • Hi, 如果兩次 left join 的話是否滿足你的需求呢? 然後在取 temporal table 的字段時,用 IF 去判斷取值。參考 SQL 如下

    SELECT c.mer_cust_id, IF(k.mer_cust_id IS NOT NULL AND a.mercust_id IS NOT NULL AND k.mer_cust_id <> '', k.update_time, NULL) AS update_time FROM charge_log as c LEFT JOIN ka_mer_info FOR SYSTEM_TIME AS OF c.proc_time AS k ON c.mer_cust_id = k.mer_cust_id LEFT JOIN adp_mer_user_info FOR SYSTEM_TIME AS OF c.proc_time AS a ON c.mer_cust_id = a.mer_cust_id

    不過,這種寫法只能適用在兩張 MySQL 表都保證 mer_cust_id 是唯一主鍵的狀況下。如果 mer_cust_id 不是唯一的話,輸出的結果數量會跟原本提供的 SQL 期望的輸出不一致 比如說 ka_mer_info 有 0 筆數據, adp_mer_user_info 有 2 筆數據,原先的 SQL 會得到 1 筆 left join 沒成功的數據,上面提供的 SQL 則會輸出 2 筆。*来自志愿者整理的flink

    2021-12-08 19:45:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Flink CDC Meetup PPT - 龚中强 立即下载
Flink CDC Meetup PPT - 王赫 立即下载
Flink CDC Meetup PPT - 覃立辉 立即下载