2016-08-17 269 views
1

USql呼叫數據我有一個數據湖這個JSON文件看起來像這樣:多維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現在是一個字符串,我似乎無法提取任何數據並將其放入要使用的變量中。有沒有辦法做到這一點?還是我需要查看其他選項來讀取數據?

回答

2

您可以在配置文件字符串上使用JsonTuple來進一步提取所需的特定屬性。在此鏈接中提供了處理嵌套Json的U-SQL代碼示例 - https://github.com/Azure/usql/blob/master/Examples/JsonSample/JsonSample/NestedJsonParsing.usql

您可以使用JsonTuple在型材柱進一步提取特定的節點

例如使用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; 

另外,如果你有興趣的具體數值,也可以通過paramters到JsonTuple函數來得到你想要的特定節點。下面的代碼從遞歸嵌套節點由「$ ..值」通過JsonTuple

構建。

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

其他支持結構

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 

希望這有助於說明當地節點(