简体   繁体   English

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

[英]Azure Stream Analytics: Get Array Elements by name

I was wondering if it is possible for me to get the elements of the array by the name of property than the position. 我想知道是否可以通过属性名而不是位置名来获取数组的元素。 For example, this is my incoming data: 例如,这是我的传入数据:

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

I intend to move this to a SQL database and I have columns within the database for each zone. 我打算将其移动到SQL数据库,并且在数据库中每个区域都有列。 The problem is that the order of different zones changes within each json. 问题在于,每个json中不同区域的顺序都会变化。 I was successfully using the following query until I realized that the position of the zones changes within each 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 

Need a way to reference these fields by the name rather than by the position. 需要一种通过名称而不是位置来引用这些字段的方法。

You can use GetArrayElement to return array element then access to each property. 您可以使用GetArrayElement返回数组元素,然后访问每个属性。 Please refer the below query 请参考以下查询

WITH 
salesData AS
(
    SELECT
        GetArrayElement(zones,0) as z
        FROM [sales-data] as s
)

SELECT
    z.eastzone
    z.westzone
    z.northzone
    z.southzone
FROM PRODUCTSDATABASE
FROM salesData

I recommend a solution: Use the JavaScript UDF in the azure stream job to complete the columns sort. 我建议一个解决方案:在azure流作业中使用JavaScript UDF完成列排序。

Please refer to my sample: 请参考我的样本:

Input data(upset the order): 输入数据(更改顺序):

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

js udf code: js udf代码:

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;
}

You can define the order you want in the obj parameter 您可以在obj参数中定义所需的顺序

SQL: SQL:

WITH 
c AS
(
    SELECT 
    udf.test(jsoninput.salesdata.productsbyzone.zones) as result
    from jsoninput
),
b AS
(
  SELECT 
    c.result.eastzone as east,c.result.westzone as west,c.result.northzone as north,c.result.southzone as south
    from c
)

SELECT
    b.east,b.west,b.north,b.south
INTO
    jaycosmos
FROM
    b

Output: 输出:

在此处输入图片说明

Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM