U-SQL从Json中提取包含Array的数据

时间:2017-07-21 06:52:55

标签: azure-data-lake u-sql

我想从我的json记录中获取始终与用户数组相同的PartnerId和Name。我目前正在尝试使用此代码:

@jsonFile =
    EXTRACT partnerId int,
            users string
    FROM @INPUT_FILE
    USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor();

@followingUsersArray =
    SELECT partnerId,
           Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(users) AS following_array
    FROM @jsonFile;

@followingUsers =
    SELECT partnerId AS PartnerId,
           following_array["name"] AS FriendName
    FROM @followingUsersArray;

但我没有得到任何结果。这是我的json示例文件:

{
    "partnerId": 2,
    "users": [{
            "name": "Anna ROGOWSKA",
            "profile_image_url": "http://pbs.twimg.com/profile_images/884844399338901504/0OYl8JA6_normal.jpg",
            "created_at": "2012-09-30T19:52:15+02:00",
            "location": "Sopot,Poland",
            "id_str": "855093368"
        },
        {
            "name": "Anna BARAŃSKA",
            "profile_image_url": "http://pbs.twimg.com/profile_images/884844399338901504/0OYl8JA6_normal.jpg",
            "created_at": "2012-09-30T19:52:15+02:00",
            "location": "Sopot,Poland",
            "id_str": "855093368"
        }
    ]
}

我想要的结果是: 2,“Anna ROGOWSKA” 2,“AnnaBARAŃSKA”

1 个答案:

答案 0 :(得分:4)

您应该利用U-SQL的component=43658 AND status in (Open)功能。

我用你的json文件对它进行了测试,结果是:

CROSS APPLY EXPLODE

输出是:

REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];

USING Microsoft.Analytics.Samples.Formats.Json;

DECLARE @path string = @"C:\Users\testUser\Documents\Visual Studio 2015\Projects\USQL_Json\";
DECLARE @input string = @path + @"sample.json";
DECLARE @to string = @path + @"output.csv";

@jsonFile =
 EXTRACT partnerId int,
        users string
FROM @input
USING new JsonExtractor();

@followingUsers =
 SELECT partnerId AS PartnerId,
       JsonFunctions.JsonTuple(users).Values AS user_array
 FROM @jsonFile;

@tabUsers =
 SELECT PartnerId,
       JsonFunctions.JsonTuple(t_user)["name"] AS FriendName
 FROM @followingUsers
     CROSS APPLY
         EXPLODE(user_array) AS A(t_user);


OUTPUT @tabUsers
TO @to
USING Outputters.Csv();