在 BigQuery Reddit 数据集中加入带有评论的帖子

Joining Posts with Comments in the BigQuery Reddit dataset

Reddit 数据集中的 commentsposts 表格如何在 BigQuery 上可用? 这似乎并不明显。

使用 u/Infamous_Blue 的建议,我们可以通过在列 link_id 上使用 SUBSTR() 并将其与 post 的 id。例如,每个评论都会有一个 link_id 看起来像 t3_43go1r,所以要匹配 43go1r 的 post 的 id 我们必须调用 SUBSTR(link_id, 4).

这是一个完整的查询,我们将 post 的 title 与每个评论 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。

以下适用于 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

顺便说一句,在性能方面它用了 2 秒,而旧版用了 18 秒 SQL