您可以通过创建实时计算数据视图简化开发过程。

语法

如果计算的逻辑比较复杂,您可以通过定义视图的方式创建数据视图,简化开发过程。
说明 数据视图仅用于辅助计算逻辑的描述,不会产生数据的物理存储。
CREATE VIEW viewName[ (columnName[ , columnName]* ) ] AS queryStatement;

示例1

CREATE VIEW LargeOrders (r, t, c, u) AS
SELECT
    rowtime,
    productId,
    c,
    units
FROM
    orders;
INSERT INTO
    rds_output
SELECT
    r,
    t,
    c,
    u
FROM
    LargeOrders;

示例2

  • 测试数据
    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
  • 测试语句
    CREATE TABLE datahub_stream (
       a VARCHAR,
       b BIGINT,
       c TIMESTAMP,
       d AS PROCTIME()
    ) WITH (
      TYPE='datahub',
      ...
    );
    CREATE TABLE rds_output (
       a VARCHAR,
       b TIMESTAMP, 
       cnt BIGINT,
       PRIMARY KEY(a)
    )WITH(
      TYPE = 'rds',
      ...
    );
    
    CREATE VIEW rds_view AS
    SELECT a, 
       CAST(
          HOP_START(d, INTERVAL '5' SECOND, INTERVAL '30' SECOND) AS TIMESTAMP
       ) AS cc, 
       SUM(b) AS cnt
    FROM 
       datahub_stream 
    GROUP BY
        HOP(d, INTERVAL '5' SECOND, INTERVAL '30' SECOND),a;
    
    INSERT INTO 
       rds_output
    SELECT
       a,
       cc,
       cnt
    FROM 
       rds_view
    WHERE 
       cnt=4
  • 测试结果
    a(VARCHAR) b (TIMESTAMP) cnt (BIGINT)
    test2 2017-11-06 16:54:10 4