我正在尝试在ASA(Azure流分析)中编写一个查询,其中输入是一条看起来像这样的json行消息
{
"DeviceId": "Device3",
"DateTime": "2016-09-05T13:23:04.5444423",
"Value": [ 1, 0, 1, 0, 0, 1, 0 ]
}
我想要做的是执行一个Unpivot,以便数据出现在表单上
create table LightBeacon (
DeviceId int primary key not null,
EventDateTime datetime not null,
LightBeaconId varchar(25) not null,
LightBeaconState SmallInt not null
)
但似乎ASA不支持SQL Unpivot函数,并且左边有多选语句,如
with
DataUnArray as (
SELECT DeviceId, DateTime as EventDateTime
, GetArrayElement(Value, 0) as LigthBeacon01
, GetArrayElement(Value, 1) as LigthBeacon02
, GetArrayElement(Value, 2) as LigthBeacon03
, GetArrayElement(Value, 3) as LigthBeacon04
, GetArrayElement(Value, 4) as LigthBeacon05
, GetArrayElement(Value, 5) as LigthBeacon06
, GetArrayElement(Value, 6) as LigthBeacon07
FROM DataIoT
where DeviceId = 'Device3'),
DataUnpivot as (
select DeviceId, EventDateTime, 'LigthBeacon01' as LigthBeaconId, LigthBeacon01 as LigthBeaconState from DataUnArray
Union All select DeviceId, EventDateTime, 'LigthBeacon02' as LigthBeaconId, LigthBeacon02 as LigthBeaconState from DataUnArray
Union All select DeviceId, EventDateTime, 'LigthBeacon03' as LigthBeaconId, LigthBeacon03 as LigthBeaconState from DataUnArray
Union All select DeviceId, EventDateTime, 'LigthBeacon04' as LigthBeaconId, LigthBeacon04 as LigthBeaconState from DataUnArray
Union All select DeviceId, EventDateTime, 'LigthBeacon05' as LigthBeaconId, LigthBeacon05 as LigthBeaconState from DataUnArray
Union All select DeviceId, EventDateTime, 'LigthBeacon06' as LigthBeaconId, LigthBeacon06 as LigthBeaconState from DataUnArray
Union All select DeviceId, EventDateTime, 'LigthBeacon07' as LigthBeaconId, LigthBeacon07 as LigthBeaconState from DataUnArray
)
select DeviceId, EventDateTime, LigthBeaconId, LigthBeaconState
into DataLakeCSV
from DataUnpivot
ASA查询无法启动以下错误:
Stream Analytics作业有验证错误:作业将超过 最大数量的事件中心接收器
如果我把它减少到5个灯塔 - 它可以工作!!!那么如何编写一个可以在unpivot中处理5个以上列的ASA查询呢?
\比约恩
答案 0 :(得分:1)
不支持UNPIVOT,但您可以使用CROSS APPLY和GetRecordProperties函数获得相同的结果:
WITH DataUnArray
AS (
SELECT DeviceId
,DATETIME AS EventDateTime
,GetArrayElement(Value, 0) AS LigthBeacon01
,GetArrayElement(Value, 1) AS LigthBeacon02
,GetArrayElement(Value, 2) AS LigthBeacon03
,GetArrayElement(Value, 3) AS LigthBeacon04
,GetArrayElement(Value, 4) AS LigthBeacon05
,GetArrayElement(Value, 5) AS LigthBeacon06
,GetArrayElement(Value, 6) AS LigthBeacon07
FROM DataIoT
WHERE DeviceId = 'Device3'
)
SELECT
event.DeviceId
,event.EventDateTime
,p.PropertyName AS LigthBeaconId,
p.PropertyValue AS LigthBeaconState
FROM DataUnArray event
CROSS APPLY GetRecordProperties(event) p
WHERE p.PropertyName LIKE 'ligthbeacon%'