全部产品
阿里云办公

数据视图概念

更新时间:2018-09-30 17:26:15

如果计算的逻辑比较复杂,用一个SQL难以描述,您可以通过实时计算 Flink所支持的定义视图的方式,简化开发过程。需要明确的是,视图仅仅用于辅助计算逻辑的描述,不会产生数据的物理存储。视图是一种简便的方法,可以通过提供片段名称将SQL转换成可管理的视图块,并且不额外占用系统空间。

语法

  1. CREATE VIEW viewName
  2. [ (columnName[ , columnName]*) ]
  3. AS queryStatement;

示例一

  1. CREATE VIEW LargeOrders(r, t, c, u) AS
  2. SELECT rowtime,
  3. productId,
  4. c,
  5. units
  6. FROM Orders;
  7. INSERT INTO rds_output
  8. SELECT
  9. r,t,c,u
  10. FROM LargeOrders;

示例二

测试数据

a(varchar) b(bigint) c(TIMESTAMP)
test1 1 1506823820000
test2 1 1506823850000
test1 1 1506823810000
test2 1 1506823840000
test2 1 1506823870000
test1 1 1506823830000
test2 1 1506823860000

测试语句

  1. CREATE TABLE datahub_stream (
  2. a varchar,
  3. b BIGINT,
  4. c TIMESTAMP,
  5. d AS PROCTIME()
  6. ) WITH (
  7. type='datahub',
  8. endPoint='http://dh-cn-hangzhou.aliyuncs.com',
  9. project='blink_test',
  10. topic='window_count',
  11. accessId='LTAIYtafPsXXXX',
  12. accessKey='gUqyVwfkK2vfJI7jF90QLXXXXk'
  13. );
  14. CREATE TABLE rds_output (
  15. a varchar,
  16. b TIMESTAMP,
  17. cnt BIGINT,
  18. PRIMARY KEY(a)
  19. )with(
  20. type = 'rds',
  21. url='jdbc:mysql://rm-bp1gz4k20XXXXX.mysql.rds.aliyuncs.com:3306/XXXXX',
  22. tableName='window_rds',
  23. userName='XXXX',
  24. password='XXXX'
  25. );
  26. CREATE VIEW rds_view AS
  27. SELECT a,
  28. CAST(HOP_START(d, interval '5' second, interval '30' second) AS TIMESTAMP) AS cc,
  29. sum(b) AS cnt
  30. FROM datahub_stream
  31. GROUP BY HOP(d, interval '5' second, interval '30' second),a;
  32. INSERT INTO rds_output
  33. SELECT
  34. a,
  35. cc,
  36. cnt
  37. FROM rds_view
  38. WHERE cnt=4

测试结果

a(varchar) b (TIMESTAMP) cnt (BIGINT)
test2 2017-11-06 16:54:10 4