Azure 流分析将行转换为列

Posted

技术标签:

【中文标题】Azure 流分析将行转换为列【英文标题】:Azure Stream Analytics Pivoting Rows To Columns 【发布时间】:2018-03-22 09:41:50 【问题描述】:

我有一个类似的数据;

        [
        "deviceid":"d1","parameter"="p1" value="1" timestamp="2018-03-22T12:33:00",
        "deviceid":"d1","parameter"="p2" value="2" timestamp="2018-03-22T12:34:00",
        "deviceid":"d1","parameter"="p2" value="3" timestamp="2018-03-22T12:35:00",
        "deviceid":"d2","parameter"="p1" value="4" timestamp="2018-03-22T12:36:00",
        "deviceid":"d2","parameter"="p2" value="5" timestamp="2018-03-22T12:37:00",
        "deviceid":"d2","parameter"="p2" value="6" timestamp="2018-03-22T12:38:00",
        "deviceid":"d2","parameter"="p1" value="7" timestamp="2018-03-22T12:43:00",
        "deviceid":"d2","parameter"="p1" value="8" timestamp="2018-03-22T12:44:00",
        "deviceid":"d2","parameter"="p2" value="9" timestamp="2018-03-22T12:45:00",
        "deviceid":"d1","parameter"="p1" value="10" timestamp="2018-03-22T12:46:00",
        "deviceid":"d1","parameter"="p1" value="11" timestamp="2018-03-22T12:47:00",
        "deviceid":"d1","parameter"="p2" value="12" timestamp="2018-03-22T12:49:00"
        ]

我想旋转该参数值(每 10 分钟窗口)并显示每个设备 ID 的每个参数的最后接收值,就像这样 edit*(参数将是动态的,而不仅仅是 p1 和 p2);

------------------------------------------------------------
| deviceid|     windowtime       |     p1    |     p2      |
------------------------------------------------------------
| d1      | 2018-03-22 12:40     |     1     |      3      |
------------------------------------------------------------
| d2      | 2018-03-22 12:40     |     4     |      6      | 
------------------------------------------------------------
| d2      | 2018-03-22 12:50     |     8     |      9      |
------------------------------------------------------------
| d1      | 2018-03-22 12:50     |     11    |     12      |
------------------------------------------------------------

谢谢。

【问题讨论】:

【参考方案1】:

另一种更清晰的方法是使用 UDA:

SELECT
    deviceid, system.Timestamp as windowtime, uda.P1UDA(test) AS P1, uda.P2UDA(test) AS P2   
FROM
    test TIMESTAMP BY timestamp
GROUP BY
    deviceid,
    TumblingWindow(minute, 10)

javascript UDA:

function P1UDA() 
    this.init = function () 

        this.state = 0;
    

    this.accumulate = function (value, timestamp) 

        if (value.parameter == 'p1') 
            if (value.value > this.state) 
                this.state = value.value;
            
        
    

    this.computeResult = function () 
        return this.state;
    


function P2UDA() 
    this.init = function () 
        this.state = 0;
    

    this.accumulate = function (value, timestamp) 

        if (value.parameter == 'p2') 
            if (value.value > this.state) 
                this.state = value.value;
            
        
    

    this.computeResult = function () 
        return this.state;
    

【讨论】:

首先谢谢。对不起,我忘了提。参数将是动态的,而不仅仅是 p1 和 p2。我想要每个窗口中参数和设备 ID 的最新记录(最大时间戳不是值) 此功能尚未作为标准 ASA 查询语言的一部分提供,但可以创建用户定义函数来执行此操作。更多信息请点击此处:docs.microsoft.com/en-us/azure/stream-analytics/…。【参考方案2】:

你可以试试下面的脚本:

with tempone as (SELECT
    deviceid, system.Timestamp as windowtime, max(value) AS P1      
FROM
    test TIMESTAMP BY timestamp
where parameter = 'p1'
GROUP BY
    deviceid,
    TumblingWindow(minute, 10)),

temptwo AS ( SELECT
    deviceid, system.Timestamp as windowtime, max(value) AS P2      
FROM
    test TIMESTAMP BY timestamp
where parameter = 'p2'
GROUP BY
    deviceid,
    TumblingWindow(minute, 10))

select tempone.deviceid, tempone.windowtime, tempone.P1, temptwo.P2 from tempone
join temptwo on tempone.deviceid = temptwo.deviceid
and DATEDIFF(minute,tempone, temptwo) BETWEEN 0 AND 1

【讨论】:

以上是关于Azure 流分析将行转换为列的主要内容,如果未能解决你的问题,请参考以下文章

Azure 流分析转换数组值

将行转换为列

将行数据转换为列

pyspark 将行转换为列

不能从子查询将行转换为列

Azure 流分析作业 - 转换查询 - ARM 模板中的正确格式