将列逆透视为流分析中的两个新列

unpivot columns into two new columns in Stream Analytics

我有来自 IoT 中心的流,例如:

{
    "store_id": "111",
    "data": [
        {
            "timestamp": "2018-04-06T11:46:11.842305",
            "book_id": "001",
            "author_id": "101"
        },
        {
            "timestamp": "2018-04-06T11:46:11.842306",
            "book_id": "002",
            "author_id": "102"
        },
        {
            "timestamp": "2018-04-06T11:46:11.842307",
            "book_id": "003",
            "author_id": "103"
        }
    ]
}

我想像这样在 SQL 数据库中传递此流:

id    id_type     timestamp 
001   book_id     2018-04-06T11:46:11.842305  
101   author_id   2018-04-06T11:46:11.842305 
002   book_id     2018-04-06T11:46:11.842306 
102   author_id   2018-04-06T11:46:11.842306 
003   book_id     2018-04-06T11:46:11.842307 
103   author_id   2018-04-06T11:46:11.842307

有什么方法可以使用 cross apply 或其他方法从 multi json 元素

中创建两个新列

如果它是一个静态数据透视表(您事先知道字段列表并且您可以硬编码它们的值),那么您可以通过这样的方式到达那里:

WITH Unfolding AS (
    SELECT
        d.ArrayValue.*
    FROM input i
    CROSS APPLY GetArrayElements(i.data) d
),
Books AS (
    SELECT
        timestamp,
        book_id as id,
        'book' as id_type
    FROM Unfolding
),
Authors AS (
    SELECT
        timestamp,
        author_id as id,
        'author' as id_type
    FROM Unfolding
),
AllRecords AS (
    SELECT timestamp, id, id_type FROM Books
    UNION
    SELECT timestamp, id, id_type FROM Authors
)
SELECT
    *
INTO output
FROM AllRecords

您为每个实体创建一个 CTE 以在最后提取并 UNION 它们。

如果您有动态值,则需要使用 JavaScript UDF。遗憾的是我没有代码示例。它应该很简单(但有点痛苦)。