USql在多维JSON数组中调用数据

时间:2016-08-17 15:44:04

标签: json multidimensional-array azure-data-lake u-sql

我在数据湖中有这个JSON文件,如下所示:

{
"id":"398507",
"contenttype":"POST",
"posttype":"post",
"uri":"http://twitter.com/etc",
"title":null,
"profile":{
   "@class":"PublisherV2_0",
   "name":"Company",
   "id":"2163171",
   "profileIcon":"https://pbs.twimg.com/image",
   "profileLocation":{
      "@class":"DocumentLocation",
      "locality":"Toronto",
      "adminDistrict":"ON",
      "countryRegion":"Canada",
      "coordinates":{
         "latitude":43.7217,
         "longitude":-31.432},
         "quadKey":"000000000000000"},
      "displayName":"Name",
      "externalId":"00000000000"},
   "source":{
       "name":"blogs",
       "id":"18",
       "param":"Twitter"},
   "content":{
       "text":"Description of post"},
       "language":{
           "name":"English",
           "code":"en"},
       "abstracttext":"More Text and links",
       "score":{}
   }
}

为了将数据调用到我的应用程序中,我必须使用以下代码将JSON转换为字符串:

DECLARE @input string = @"/MSEStream/{*}.json";

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


@allposts = 
EXTRACT 
    jsonString  string
FROM @input
USING Extractors.Text(delimiter:'\b', quoting:true);

@extractedrows = SELECT Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(jsonString) AS er FROM @allposts;


@result = 
SELECT er["id"] AS postID,
       er["contenttype"] AS contentType,
       er["posttype"] AS postType,
       er["uri"] AS uri,
       er["title"] AS Title,
       er["acquisitiondate"] AS acquisitionDate,
       er["modificationdate"] AS modificationDate,
       er["publicationdate"] AS publicationDate,
       er["profile"] AS profile
FROM @extractedrows;

OUTPUT  @result
TO  "/ProcessedQueries/all_posts.csv"
USING Outputters.Csv();

这将JSON输出到可读的.csv文件中,当我下载文件时,所有数据都正确显示。我的问题是当我需要在配置文件中获取数据时。因为JSON现在是一个字符串,所以我似乎无法提取任何数据并将其放入要使用的变量中。有没有办法做到这一点?或者我是否需要研究其他阅读数据的选项?

1 个答案:

答案 0 :(得分:3)

您可以在配置文件字符串上使用JsonTuple来进一步提取所需的特定属性。此链接提供了一个用于处理嵌套Json的U-SQL代码示例 - https://github.com/Azure/usql/blob/master/Examples/JsonSample/JsonSample/NestedJsonParsing.usql

您可以在配置文件列上使用JsonTuple来进一步提取特定节点

E.g。使用JsonTuple获取配置文件节点的所有子节点,并提取特定值,就像在代码中一样。

@childnodesofprofile = 
SELECT
   Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(profile) AS childnodes_map
FROM @result;

@values =
SELECT
   childnodes_map["name"] AS name,
   childnodes_map["id"] AS id
FROM @result;

或者,如果您对特定值感兴趣,还可以将参数传递给JsonTuple函数以获取所需的特定节点。下面的代码从递归嵌套节点获取位置节点(如“$ .. value”构造所描述。

@locality = 
SELECT Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(profile, "$..locality").Values AS locality
FROM @result;

JsonTuple支持的其他构造

    JsonTuple(json, "id", "name")              // field names          
    JsonTuple(json, "$.address.zip")           // nested fields        
    JsonTuple(json, "$..address")              // recursive children   
    JsonTuple(json, "$[?(@.id > 1)].id")       // path expression      
    JsonTuple(json)                            // all children

希望这有帮助。