2016-12-07 91 views
1

提取的資料,因此我有很多的結構是這樣的JSON文件:U型SQL - 從複雜的JSON對象

{ 
    "Id": "2551faee-20e5-41e4-a7e6-57bd20b02a22", 
    "Timestamp": "2016-12-06T08:09:57.5541438+01:00", 
    "EventEntry": { 
     "EventId": 1, 
     "Payload": [ 
      "1a3e0c9e-ef69-4c6a-ac8c-9b2de2fbc701", 
      "DHS.PlanCare.Business.BusinessLogic.VisionModels.VisionModelServiceWithoutUnitOfWork.FetchVisionModelsForClientOnReferenceDateAsync(System.Int64 clientId, System.DateTime referenceDate, System.Threading.CancellationToken cancellationToken)", 
      25, 
      "DHS.PlanCare.Business.BusinessLogic.VisionModels.VisionModelServiceWithoutUnitOfWork+<FetchVisionModelsForClientOnReferenceDateAsync>d__11.MoveNext\r\nDHS.PlanCare.Core.Extensions.IQueryableExtensions+<ExecuteAndThrowTaskCancelledWhenRequestedAsync>d__16`1.MoveNext\r\n", 
      false, 
      "2197, 6-12-2016 0:00:00, System.Threading.CancellationToken" 
     ], 
     "EventName": "Duration", 
     "KeyWordsDescription": "Duration", 
     "PayloadSchema": [ 
      "instanceSessionId", 
      "member", 
      "durationInMilliseconds", 
      "minimalStacktrace", 
      "hasFailed", 
      "parameters" 
     ] 
    }, 
    "Session": { 
     "SessionId": "0016e54b-6c4a-48bd-9813-39bb040f7736", 
     "EnvironmentId": "C15E535B8D0BD9EF63E39045F1859C98FEDD47F2", 
     "OrganisationId": "AC6752D4-883D-42EE-9FEA-F9AE26978E54" 
    } 
} 

我怎樣才能創建一個輸出

Id, 
Timestamp, 
EventEntry.EventId and 
EventEntry.Payload[2] (value 25 in the example below) 
是u-SQL查詢

我無法弄清楚如何延長我的查詢

@extract = 
    EXTRACT 
     Timestamp DateTime 
    FROM @"wasb://xxx/2016/12/06/0016e54b-6c4a-48bd-9813-39bb040f7736/yyy/{*}/{*}.json" 
    USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor(); 

@res = 
    SELECT Timestamp 
    FROM @extract; 

OUTPUT @res TO "/output/result.csv" USING Outputters.Csv(); 

我見過一些例子,如:

U- SQL Unable to extract data from JSON file =>這隻查詢文檔的一個級別,我需要來自多個級別的數據。

U-SQL - Extract data from json-array =>這隻查詢文檔的一個級別,我需要來自多個級別的數據。

+0

可能是這可以幫助你http://stackoverflow.com/questions/27198694/accesing-nested-properties-json –

回答

1

JSONTuple一次性支持多個JSONPath。

@extract = 
    EXTRACT 
     Id String, 
     Timestamp DateTime, 
     EventEntry String 
    FROM @"..." 
    USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor(); 

@res = 
    SELECT Id, Timestamp, EventEntry, 
    Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(EventEntry, 
     "EventId", "Payload[2]") AS Event 
    FROM @extract; 

@res = 
    SELECT Id, 
    Timestamp, 
    Event["EventId"] AS EventId, 
    Event["Payload[2]"] AS Something 
    FROM @res; 
+0

謝謝,正是我在找的! –

1

你可能想看看這個GIT的例子。 https://github.com/Azure/usql/blob/master/Examples/JsonSample/JsonSample/NestedJsonParsing.usql

這需要2個不同的數據元素並將它們結合起來,就像您有Payload和Payload架構一樣。如果您使用「甜甜圈」或「蛋糕和麪糊」示例創建鍵值對,則可以將靈敏度與負載匹配並使用交叉應用爆炸功能。

+0

儘管這並不直接回答我的回答它提供了一些有用的例子,謝謝! –