2

我正在为我正在开发的应用程序测试数据湖。我是 U-SQL 和数据湖的新手,我只是想查询 JSON 文件中的所有记录。现在,它只返回一条记录,我不知道为什么,因为该文件大约有 200 条。

我的代码是:

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

提前感谢您的帮助

4

2 回答 2

3

JsonExtractor 接受一个参数,该参数允许您使用 JSON 路径表达式指定哪些项目或对象被映射到行中。如果您不指定任何内容,它将采用最高根(即一行)。

您需要数组中的每一项,因此将其指定为:

使用新的 Microsoft.Analytics.Samples.Formats.Json.JsonExtractor("[*]");

其中 [*] 是 JSON 路径表达式,它说给我数组的所有元素,在这种情况下是顶级数组。

于 2016-08-15T20:04:38.323 回答
1

如果您的字段中有一个名为 id 的 JSON 节点,那么您在问题中发布的原始脚本将返回根节点下名称为“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);
于 2016-08-17T06:04:26.397 回答