2016-08-15 44 views
2

我正在測試我正在開發的應用程序的數據湖。我是U-SQL和數據湖的新手,只是試圖查詢JSON文件中的所有記錄。現在,這只是一個返回記錄,我不知道爲什麼,因爲文件中有大約200U-SQL Json提取器只提取一條記錄

我的代碼是:

DECLARE @input string = @"/MSEStream/output/2016/08/12_0_fc829ede3c1d4cf9a3278d43e7e4e9d0.json"; 

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


@allposts = 
EXTRACT 
    id string 
FROM @input 
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor(); 

@result = 
SELECT * 
FROM @allposts; 

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

數據舉例:

{ 
"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":{} 
    } 
} 

謝謝你提前提供幫助

回答

2

JsonExtractor接受一個參數,該參數允許您使用JSON路徑表達式指定將哪些項目或對象映射到行中。如果你沒有指定任何東西,它將採用頂部的根(這是一行)。

要在陣列中的項目的每一個,所以你將它指定爲:

採用新型Microsoft.Analytics.Samples.Formats.Json.JsonExtractor(「[*]」);

其中[*]是JSON路徑表達式,表示給我所有在這種情況下是頂層數組的數組元素。

+0

當我放入時,它什麼也沒有返回。我添加了一個我在帖子中提到的數據的例子。 – WorkHardWork

+0

我想清楚發生了什麼,輸出文件是一行分隔文件。有沒有辦法像這樣讀取它,或者我需要將它格式化爲數組? – WorkHardWork

+0

您可以通過擴展當前的JSONExtractor來編寫自己的提取器來完成每行處理的行數(請參閱http://usql.io上我們的GitHub上的其他一些提取器),也可以使用內置函數在提取器中讀取JSON並作爲字符串讀取(然後,最大長度爲128kB)。一個例子是https://github.com/Azure/usql/blob/master/Examples/DataFormats/Microsoft.Analytics.Samples.Formats/readme.md –

1

如果您的字段中有一個名爲id的JSON節點,則問題中發佈的原始腳本將返回rootnode下名爲「id」的節點。要獲得所有節點,您的腳本將被組織爲

@allposts = 
EXTRACT 
    id string, 
    contenttype string, 
    posttype string, 
    uri string, 
    title string, 
    profile string 
FROM @input 
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor(); 

請讓我們知道它是否有效。另一種方法是使用本地提取器將其解壓到一個字符串中(正如MRys提到的那樣,只要您的JSON低於128 KB,就可以工作)。

@allposts = 
EXTRACT 
    json string 
FROM @input 
USING Extractors.Text(delimiter:'\b', quoting:false); 
+0

'Extractors.Csv'不允許分隔符。 – WorkHardWork

+0

您可以嘗試使用Extractors.Text,而不是使用相同的語法? –