How to query public data sets using BigQuery
BigQuery is a fully managed data warehouse and analytics platform. Public datasets are available for you to analyze using SQL queries. You can access BigQuery public data sets using the web UI, the command-line tool, or by making calls to the BigQuery REST API using a variety of client libraries such as Java, .NET, or Python.
Currently, BigQuery public datasets are stored in the
location. When you query a public dataset, supply
--location=US flag on the command line, choose
US as the
processing location in the BigQuery web UI, or specify the
location property in the
jobReference section of the job resource
when you use the API. Because the public datasets are stored in the US, you cannot write public
data query results to a table in another region, and you cannot join tables in public datasets
with tables in another region.
To get started using a BigQuery public dataset, create or select a project. The first terabyte of data processed per month is free, so you can start querying public datasets without enabling billing. If you intend to go beyond the free tier, you should also enable billing.
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
Select or create a GCP project.
Make sure that billing is enabled for your project.
- BigQuery is automatically enabled in new projects. To activate BigQuery in a pre-existing project, Enable the BigQuery API.
This dataset contains all stories and comments from Hacker News from its launch in 2006. Each story contains a story ID, the author that made the post, when it was written, and the number of points the story received.
You can start exploring this data in the BigQuery console:
Here are some examples of SQL queries you can run on this data in BigQuery.
These samples use BigQuery’s legacy SQL by setting the
prefix. For more information, see Setting a query prefix.
How are Hacker News story points distributed?
If you use the score as a dimension (group by score, in SQL) and count the number of posts with each score, you can get an idea about how likely a story is to get a given score.
#legacySQL SELECT stories.score AS stories_score, COUNT(stories.id) AS stories_count FROM [bigquery-public-data:hacker_news.full] AS stories WHERE NOT (stories.score IS NULL) GROUP EACH BY 1 ORDER BY 1 LIMIT 500
Where do the stories live?
By parsing out the host from the URL you can see where Hacker News stories originate.
#legacySQL SELECT REGEXP_EXTRACT((REGEXP_EXTRACT(stories.url,'http://([^/]+)/')),'([^\\.]+\\.[^\\.]+(?:\\.[a-zA-Z].)?)$') AS stories_url_domain, COUNT(stories.id) AS stories_count, COUNT(CASE WHEN stories.score >= 7 THEN 1 ELSE NULL END) AS storiescountscore_7_plus, 100.0 * (COUNT(CASE WHEN stories.score >= 7 THEN 1 ELSE NULL END)) / (COUNT(stories.id)) AS stories_percent_7_plus FROM [bigquery-public-data:hacker_news.full] AS stories GROUP EACH BY 1 HAVING (COUNT(CASE WHEN stories.score >= 7 THEN 1 ELSE NULL END) >= 6) ORDER BY 3 DESC LIMIT 500
About the data
Dataset Source: Hacker News
Category: Media, Social
Use: This dataset is publicly available for anyone to use under the following terms provided by the Dataset Source — https://github.com/HackerNews/API — and is provided "AS IS" without any warranty, express or implied, from Google. Google disclaims all liability for any damages, direct or indirect, resulting from the use of the dataset.
Update Frequency: Daily
View in BigQuery: Go to the Hacker News dataset