【问题标题】:U-SQL Json Extractor is only pulling one recordU-SQL Json Extractor 只提取一条记录
【发布时间】:2016-08-15 18:44:22
【问题描述】:

我正在为我正在开发的应用程序测试数据湖。我是 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":{}
   }
}

提前感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

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

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

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

    其中 [*] 是 JSON 路径表达式,表示给我数组的所有元素,在本例中是顶级数组。

    【讨论】:

    • 当我输入它时,它什么也没返回。我在帖子中添加了我要提取的数据的示例。
    • 我知道发生了什么,输出文件是一个行分隔文件。有没有办法像这样读取它,还是我需要将它格式化为数组?
    • 您可以编写自己的提取器,通过扩展当前的 JSONExtractor(查看我们的 GitHub 上的其他一些提取器 usql.io)来进行逐行处理,或者您可以使用内置提取器并将 JSON 作为字符串读取(最大长度为 128kB)。一个例子在这里github.com/Azure/usql/blob/master/Examples/DataFormats/…
    【解决方案2】:

    如果您的字段中有一个名为 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);
    

    【讨论】:

    • Extractors.Csv 不允许使用分隔符。
    • 您可以尝试使用相同语法的 Extractors.Text 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多