流数据分析使用CREATE TABLE作为输出结果数据的格式定义,同时定义数据如何输出到目的数据存储中。

创建结果表的语法如下:

  CREATE TABLE tableName
        (columnName dataType [, columnName dataType ]*)
        [ WITH (propertyName=propertyValue [, propertyName=propertyValue ]*) ];

边缘消息总线(EdgeBusSink)

示例:

create table edgehub_output (
    productKey varchar,
    deviceName varchar,
    temperature int,
    productTime timestamp,
    inputTime timestamp,
    outputTime timestamp,
    delayTime bigint
) with (
    type = 'edgebus_sink',
    topic = '/sys/streamCompute/highTemperature'
);

WITH参数说明如下:

参数 是否必选 描述
type 定义结果表类型为edgebus_sink。
topic 定义消息主题,必须以斜线“/”开头,支持英文大小写、数字、斜线“/”、下划线“_”和中划线“-”,不超过256字符。
注意 若topic需要流转到IoTHub(云端),则需要在网关的产品里定义topic,且该topic必须与SQL中的topic保持一致。

关系型数据库(RDS)

目前仅支持MySQL数据存储类型,数据库中必须要有真实的表存在。

示例:

create table db (
productKey varchar,
deviceName varchar,
ts timestamp,
temperature int
) with (
type = 'rds',
url='jdbc:mysql://your_db_host:your_db_port/your_db_name',
tableName='your_table_name',
userName='config://local_db_username', -- local_db_username为在配置中存储的用户名的key
password='config://local_db_password' -- local_db_password为在配置中存储的密码的key
);
注意 在实际使用过程中需要把所有your_xxxxxx参数值,替换为实际的数据库相关参数值,并根据如下步骤,在配置中心存入数据库的用户名密码。
  1. 物联网平台控制台左侧导航栏中,单击边缘计算 > 边缘实例
  2. 在相应的边缘实例右侧单击查看,进入实例详情页面,在网关页签下的网关右侧打开远程访问按钮,登录远程控制台,执行如下命令:
    cd /linkedge/gateway/build/bin
    ./tool_config -s local_db_username root
    ./tool_config -s local_db_password xxxxxxxx

WITH参数说明如下:

参数 描述
type 固定值rds,表示结果表的类型为RDS。
host MySQL数据库(若为localhost,则为本地数据库)的host地址。
port MySQL数据库端口号。
dbName 数据库名。
tableName 表名。
userName 数据库用户名。
password 数据库密码。

更多参数介绍,请参考创建云数据库RDS版结果表中RDS相关参数说明。

边缘文件系统(File)

示例:

create table print_sink (
    productKey varchar,
    deviceName varchar,
    eventCode varchar,
    ts timestamp,
    temperature int
) with (
    type = 'file'
    tag = 'prop_filter_b',
    filePath = '/linkedge/run/debug/prop_filter_b.txt'
);

with参数说明如下:

参数 是否必选 描述
type 定义结果表类型为file。
tag 为输出的消息设置标签。
  • 若设置该参数,表示每条输出的消息前都附带该参数的取值。
  • 若无需为输出消息设置标签,则删除参数。
filePath 输出消息的目的文件路径,不同的流数据分析任务不可共用一个文件路径。

示例表示,将消息输出到本地/linkedge/run/debug/prop_filter_b.txt文件中,并在每条输出消息前带prop_filter_b标签。