Redis 维表

简述

Easystream 支持使用 Redis 表作为维表进行 join,支持配置 cache。

示例

  1. CREATE TABLE user_log (
  2. user_id VARCHAR,
  3. item_id VARCHAR,
  4. category_id VARCHAR,
  5. behavior VARCHAR,
  6. ts TIMESTAMP
  7. ) WITH (
  8. 'connector.type' = 'kafka',
  9. 'connector.version' = 'universal',
  10. 'connector.topic' = 'user_behavior',
  11. 'connector.startup-mode' = 'latest-offset',
  12. 'connector.properties.zookeeper.connect' = 'xxx',
  13. 'connector.properties.bootstrap.servers' = 'xxx',
  14. 'connector.properties.group.id' = 'xxx',
  15. 'update-mode' = 'append',
  16. 'format.type' = 'json',
  17. 'format.derive-schema' = 'true'
  18. );
  19. --dim
  20. CREATE TABLE dim_table (
  21. user_id VARCHAR,
  22. age varchar
  23. ) WITH (
  24. 'connector.type' = 'redis',
  25. 'host' = '*',
  26. 'port' = '*',
  27. 'mode' = 'single',
  28. 'db.index' = '0',
  29. 'password' = '*',
  30. 'connector.lookup.cache.type' = 'all',
  31. 'connector.lookup.cache.ttl' = '3000'
  32. );
  33. -- sink
  34. CREATE TABLE pvuvage_sink (
  35. dt VARCHAR,
  36. age INT,
  37. pv BIGINT,
  38. uv BIGINT
  39. ) WITH (
  40. 'connector.type' = 'jdbc',
  41. 'connector.url' = 'jdbc:mysql://10.122.173.167:3306/flink-test',
  42. 'connector.table' = 'pvuv_age_sink_redis',
  43. 'connector.username' = '*',
  44. 'connector.password' = '*',
  45. 'connector.write.flush.max-rows' = '1'
  46. );
  47. INSERT INTO pvuvage_sink
  48. select DATE_FORMAT(d.ts, 'yyyy-MM-dd HH:00') as dt, d.age, count(*) as pv, count(distinct d.user_id) as uv
  49. from (
  50. SELECT
  51. u.ts,
  52. cast(w.age as int) as age,
  53. u.user_id,
  54. u.behavior
  55. FROM (select user_id,item_id,category_id,behavior,ts,PROCTIME() as proc from user_log) as u
  56. left join dim_table for system_time as of u.proc as w
  57. on u.user_id = w.user_id
  58. where w.age > 10
  59. ) as d
  60. GROUP BY DATE_FORMAT(d.ts, 'yyyy-MM-dd HH:00') , d.age;

With 参数

参数 注释说明 备注
connector.type 维表类型 必填:redis
host redis servers 必填,多个直接用个逗号分隔
port redis servers 服务端口 选填,默认值为‘6379’
mode redis 集群模式 选填;默认值为single;支持single, sentinel, cluster 3种集群模式;cluster及sentinel模式中,host用逗号分隔,如host,host,host…;sentinel模式需要设置master
master master 地址 当 mode = ‘sentinel’ 时才需要
password redis 密码 当 mode 为 single、sentinel 可选填
db.index 对应 redis 的 db 选填;默认为0;
connector.lookup.cache.type 缓存类型 选填,默认’none’,支持:’all’, ‘lru’, ‘none’
connector.lookup.cache.max-rows 最大缓存条数 ,默认10000条, type 为 lru 时有效 选填
connector.lookup.cache.ttl 当选择’lru’表示缓存失效时间,默认不过期;当选择’all’表示 reload 间隔时间 选填,默认不 reload
connector.lookup.max-retries dim 表获取失败时最大重试次数 选填,默认3次