【发布时间】:2018-01-07 10:10:19
【问题描述】:
BigQuery 上的 Reddit 数据集中的 comments 和 posts 表如何相关?
这似乎并不明显。
【问题讨论】:
标签: sql google-bigquery reddit
BigQuery 上的 Reddit 数据集中的 comments 和 posts 表如何相关?
这似乎并不明显。
【问题讨论】:
标签: sql google-bigquery reddit
以下是 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 秒
【讨论】:
使用来自u/Infamous_Blue 的建议,我们可以通过在link_id 列上使用SUBSTR() 并将其与帖子的id 匹配,将cmets 加入其父帖子。例如,每条评论都有一个link_id,看起来像t3_43go1r,所以要匹配帖子的id 或43go1r,我们必须调用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。
【讨论】: