Azure流分析:按名称获取数组元素

时间:2018-02-11 02:02:50

标签: sql azure azure-stream-analytics

我想知道是否有可能通过属性名称获取数组的元素而不是位置。例如,这是我的传入数据:

    {
    "salesdata": {
        "productsbyzone": {
            "zones": [{
                    "eastzone": "shirts, trousers"
                },
                {
                    "westzone": "slacks"
                },
                {
                    "northzone": "gowns"
                },
                {
                    "southzone": "maxis"
                }
            ]
        }
    }
}

我打算将它移到SQL数据库中,我在每个区域的数据库中都有列。问题是不同区域的顺序在每个json内发生变化。我成功地使用了以下查询,直到我意识到区域的位置在每个json中发生变化:

WITH 
salesData AS
(
    SELECT
    (c.salesdata.productsbyzone.zone,0) as eastzone,
    (c.salesdata.productsbyzone.zone,1) as westzone,
    (c.salesdata.productsbyzone.zone,2) as northzone,
    (c.salesdata.productsbyzone.zone,3) as sourthzone,
    FROM [sales-data] as c
)
SELECT
eastzone.eastzone as PRODUCTS_EAST,
westzone.westzone as PRODUCTS_WEST,
northzone.northzone as PRODUCTS_NORTH,
southzone.southzone as PRODUCTS_SOUTH
INTO PRODUCTSDATABASE
FROM salesData 

需要一种方法来通过名称而不是位置来引用这些字段。

2 个答案:

答案 0 :(得分:2)

您可以使用DateFormatter返回数组元素,然后访问每个属性。请参考以下查询

let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "MMMM '\(String(describing: day!)),' yyyy h:mm a"

let dateString = "\(dateFormatter.string(from: date))"

print(dateString)

答案 1 :(得分:2)

我建议使用解决方案:使用azure流作业中的JavaScript UDF来完成列排序。

请参阅我的样本:

输入数据(扰乱订单):

git log --ancestry-path BRANCH..MASTER

js udf code:

{
    "salesdata": {
        "productsbyzone": {
            "zones": [{
                    "westzone": "slacks"  
                },
                {
                    "eastzone": "shirts, trousers"
                },
                {
                    "northzone": "gowns"
                },
                {
                    "southzone": "maxis"
                }
            ]
        }
    }
}

您可以在function test(arg) { var z = arg; var obj = { eastzone: "", westzone: "", northzone: "", southzone: "" } for(var i=0;i<z.length;i++){ switch(Object.keys(z[i])[0]){ case "eastzone": obj.eastzone = z[i]["eastzone"]; continue; case "westzone": obj.westzone = z[i]["westzone"]; continue; case "northzone": obj.northzone = z[i]["northzone"]; continue; case "southzone": obj.southzone = z[i]["southzone"]; continue; } } return obj; } 参数

中定义所需的顺序

<强> SQL:

obj

<强> 输出:

enter image description here

希望它对你有所帮助。