【问题标题】:Joining Posts with Comments in the BigQuery Reddit dataset在 BigQuery Reddit 数据集中加入带有评论的帖子
【发布时间】:2018-01-07 10:10:19
【问题描述】:

BigQuery 上的 Reddit 数据集中的 commentsposts 表如何相关? 这似乎并不明显。

【问题讨论】:

    标签: sql google-bigquery reddit


    【解决方案1】:

    以下是 BigQuery 标准 SQL

    #standardSQL
    SELECT posts.title, comments.body
    FROM `fh-bigquery.reddit_comments.2016_01` AS comments
    JOIN `fh-bigquery.reddit_posts.2016_01`  AS posts
    ON posts.id = SUBSTR(comments.link_id, 4) 
    WHERE posts.id = '43go1r'
    

    如果您仍在使用 BigQuery 旧版 SQL,请考虑使用 migrating to BigQuery Standard SQL

    顺便说一句,在性能方面,旧版 SQL 需要 2 秒而 18 秒

    【讨论】:

    • 对于不熟悉的其他人(就像我一样)的注意事项:此答案使用的标准 SQL 语法位于表名周围的 ``'s 中。谢谢米哈伊尔。
    • 另一个注意事项:要在 BigQuery 查询编写器中关闭旧版 SQL,请点击“显示选项”并取消选中“使用旧版 SQL”框。
    【解决方案2】:

    使用来自u/Infamous_Blue 的建议,我们可以通过在link_id 列上使用SUBSTR() 并将其与帖子的id 匹配,将cmets 加入其父帖子。例如,每条评论都有一个link_id,看起来像t3_43go1r,所以要匹配帖子的id43go1r,我们必须调用SUBSTR(link_id, 4)

    这是一个完整的查询,我们将帖子的 title 与每个 cmets body 连接起来:

    select posts.title, comments.body --grab anything you like
    from (select SUBSTR(link_id, 4) as lnk, body 
          from [fh-bigquery:reddit_comments.2016_01]) as comments,
    join [fh-bigquery:reddit_posts.2016_01]  as posts
    on posts.id = comments.lnk
    where posts.id = '43go1r'; --random subreddit
    

    这在 40.3 秒内完成,运行时处理了 11.9 GB。

    【讨论】:

    • 在使用旧版 SQL 时,谓词不会通过连接自动“下推”,因此使用标准 SQL 是一个不错的选择。
    • 谢谢。对 bigquery 来说还是新手。我从使用标准语法的 Mikhail 中选择了答案。
    • 别担心!如果您有兴趣尝试默认使用标准 SQL 的全新 BigQuery UI,您可以submit this form
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 2022-11-10
    • 1970-01-01
    • 2016-12-02
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多