arrow_back

Weather Data in BigQuery

Join Sign in

Weather Data in BigQuery

45 minutes Free

GSP009

Google Cloud self-paced labs logo

Overview

In this lab you will analyze historical weather observations using BigQuery and use weather data in conjunction with other datasets.

What you'll learn

In this lab, you'll:

  • Carry out interactive queries on the BigQuery console.
  • Combine and run analytics on multiple datasets.

Introduction

This lab uses two public datasets in BigQuery: weather data from NOAA and citizen complaints data from New York City.

You will encounter, for the first time, several aspects of Google Cloud that are of great benefit to scientists:

  1. Serverless. No need to download data to your machine in order to work with it - the dataset will remain on the cloud.
  2. Ease of use. Run ad-hoc SQL queries on your dataset without having to prepare the data, like indexes, beforehand. This is invaluable for data exploration.
  3. Scale. Carry out data exploration on extremely large datasets interactively. You don't need to sample the data in order to work with it in a timely manner.
  4. Shareability. You will be able to run queries on data from different datasets without any issues. BigQuery is a convenient way to share datasets. Of course, you can also keep your data private, or share them only with specific persons -- not all data need to be public.

The end-result is that you will find what types of municipal complaints are correlated with weather. For example, you will find (not surprisingly) that complaints about residential furnaces are most common when it is cold outside:

Scatter plot of Daily 311 Calls Regarding Heat versus mean Daily Temperature

Prerequisites

This is a fundamental level lab and assumes some experience with BigQuery and SQL. If you have never worked with BigQuery or MySQL, the self-paced lab, BigQuery: Qwik Start - Console can get you up to speed with these Google Cloud services.

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

Task 1. Explore weather data

Open the BigQuery console

  1. In the Google Cloud Console, select Navigation menu > BigQuery.

The Welcome to BigQuery in the Cloud Console message box opens. This message box provides a link to the quickstart guide and the release notes.

  1. Click Done.

The BigQuery console opens.

  1. In the Explorer pane, click ADD DATA > Explore public datasets.

The Datasets window opens.

  1. In the Search bar, type "noaa_gsod" then press Enter.

1 result, GSOD dataset, displays.

  1. Click the GSOD dataset and then click VIEW DATASET.

The BigQuery console opens in a new browser tab.

  1. To keep your workspace organized, close this new browser tab, go to Navigation menu > BigQuery in the first tab and refresh the browser.

In the BigQuery console (in the first browser tab) you see two projects in the Explorer pane, one named your lab project ID, and one named bigquery-public-data.

Note: If the new project bigquery-public-data doesn't appear to the Explorer panel, then click on + ADD DATA > Pin a project > Enter project name (bigquery-public-data) and Pin.
  1. In the Explorer pane of the BigQuery console, expand bigquery-public-data > noaa_gsod and select the gsod2014 table.

  2. In the Table (gsod2014) window, click the Preview tab.

Preview tabbed page

  1. Examine the columns and some of the data values.

  2. Paste the following in the query EDITOR:

SELECT -- Create a timestamp from the date components. stn, TIMESTAMP(CONCAT(year,"-",mo,"-",da)) AS timestamp, -- Replace numerical null values with actual null AVG(IF (temp=9999.9, null, temp)) AS temperature, AVG(IF (wdsp="999.9", null, CAST(wdsp AS Float64))) AS wind_speed, AVG(IF (prcp=99.99, 0, prcp)) AS precipitation FROM `bigquery-public-data.noaa_gsod.gsod20*` WHERE CAST(YEAR AS INT64) > 2010 AND CAST(MO AS INT64) = 6 AND CAST(DA AS INT64) = 12 AND (stn="725030" OR -- La Guardia stn="744860") -- JFK GROUP BY stn, timestamp ORDER BY timestamp DESC, stn ASC
  1. Click RUN. Look at the result and try to determine what this query does.

Click Check my progress below to verify you're on track in this lab.

Explore weather data

Task 2. Explore New York citizen complaints data

  1. In the Explorer pane of the BigQuery Console, select the newly added bigquery-public-data project and expand the new_york dataset and select 311_service_requests table.

  2. Then click on the Preview tab. Your console should resemble the following:

311_service_requests Preview tabbed page

  1. Examine the columns and some of the data values.

  2. If the editor has been closed, click "+" (Compose New Query) icon.

  3. Paste the following into the query EDITOR:

SELECT EXTRACT(YEAR FROM created_date) AS year, complaint_type, COUNT(1) AS num_complaints FROM `bigquery-public-data.new_york.311_service_requests` GROUP BY year, complaint_type ORDER BY num_complaints DESC
  1. Click RUN.

  2. Look at the results to determine what the most common complaints are. You will try to determine if these complaints correlate to weather in a later part of this lab.

Click Check my progress below to verify you're on track in this lab.

Explore New York citizen complaints data

Task 3. Saving a new table of weather data

  1. In the Explorer pane of the BigQuery Console, click on the View actions icon (View actions icon) next to your Project ID and then select Create dataset.

  2. In the Create dataset dialog, set the Dataset ID to "demos" and leave the other options at their default values.

  3. Click Create dataset. Your project now has a dataset named "demos".

  4. Click "+" (Compose New Query) icon and then run the following query:

SELECT -- Create a timestamp from the date components. timestamp(concat(year,"-",mo,"-",da)) as timestamp, -- Replace numerical null values with actual nulls AVG(IF (temp=9999.9, null, temp)) AS temperature, AVG(IF (visib=999.9, null, visib)) AS visibility, AVG(IF (wdsp="999.9", null, CAST(wdsp AS Float64))) AS wind_speed, AVG(IF (gust=999.9, null, gust)) AS wind_gust, AVG(IF (prcp=99.99, null, prcp)) AS precipitation, AVG(IF (sndp=999.9, null, sndp)) AS snow_depth FROM `bigquery-public-data.noaa_gsod.gsod20*` WHERE CAST(YEAR AS INT64) > 2008 AND (stn="725030" OR -- La Guardia stn="744860") -- JFK GROUP BY timestamp
  1. In the query EDITOR section, click More > Query settings.

  2. In the Query settings dialog, set the following fields. Leave all others at their default value.

Destination: select Set a destination table for query results

Dataset: Type demos and select your dataset.

Table Id: Type nyc_weather

Results size: check Allow large results (no size limit)

  1. Click SAVE

  2. Click RUN.

The results are now saved in the dataset you created (demos).

  1. Navigate back to More > Query settings and, in the Destination field select Save query results in a temporary table. This removes the demos dataset as a destination for future queries.

  2. Click SAVE to close the query.

Click Check my progress below to verify you're on track in this lab.

Saving a new table of weather data

Task 4. Find correlation between weather and complaints

Compare the number of complaints and temperature using the CORR function.

  1. Go back to the query EDITOR and run the following query:

SELECT descriptor, sum(complaint_count) as total_complaint_count, count(temperature) as data_count, ROUND(corr(temperature, avg_count),3) AS corr_count, ROUND(corr(temperature, avg_pct_count),3) AS corr_pct From ( SELECT avg(pct_count) as avg_pct_count, avg(day_count) as avg_count, sum(day_count) as complaint_count, descriptor, temperature FROM ( SELECT DATE(timestamp) AS date, temperature FROM demos.nyc_weather) a JOIN ( SELECT x.date, descriptor, day_count, day_count / all_calls_count as pct_count FROM (SELECT DATE(created_date) AS date, concat(complaint_type, ": ", descriptor) as descriptor, COUNT(*) AS day_count FROM `bigquery-public-data.new_york.311_service_requests` GROUP BY date, descriptor)x JOIN ( SELECT DATE(timestamp) AS date, COUNT(*) AS all_calls_count FROM `demos.nyc_weather` GROUP BY date )y ON x.date=y.date )b ON a.date = b.date GROUP BY descriptor, temperature ) GROUP BY descriptor HAVING total_complaint_count > 5000 AND ABS(corr_pct) > 0.5 AND data_count > 5 ORDER BY ABS(corr_pct) DESC

The results indicate that Heating complaints are negatively correlated with temperature (i.e., more heating calls on cold days) and calls about dead trees are positively correlated with temperature (i.e., more calls on hot days).

Next, compare the number of complaints and wind speed with the CORR function.

  1. Click "+" (Compose New Query) icon and run the following query:

SELECT descriptor, sum(complaint_count) as total_complaint_count, count(wind_speed) as data_count, ROUND(corr(wind_speed, avg_count),3) AS corr_count, ROUND(corr(wind_speed, avg_pct_count),3) AS corr_pct From ( SELECT avg(pct_count) as avg_pct_count, avg(day_count) as avg_count, sum(day_count) as complaint_count, descriptor, wind_speed FROM ( SELECT DATE(timestamp) AS date, wind_speed FROM demos.nyc_weather) a JOIN ( SELECT x.date, descriptor, day_count, day_count / all_calls_count as pct_count FROM (SELECT DATE(created_date) AS date, concat(complaint_type, ": ", descriptor) as descriptor, COUNT(*) AS day_count FROM `bigquery-public-data.new_york.311_service_requests` GROUP BY date, descriptor)x JOIN ( SELECT DATE(timestamp) AS date, COUNT(*) AS all_calls_count FROM `demos.nyc_weather` GROUP BY date )y ON x.date=y.date )b ON a.date = b.date GROUP BY descriptor, wind_speed ) GROUP BY descriptor HAVING total_complaint_count > 5000 AND ABS(corr_pct) > 0.5 AND data_count > 5 ORDER BY ABS(corr_pct) DESC
  1. Notice that the Corr columns are both negative for noise related complaints — do you have a hypothesis for why noise complaints reduce on windy days? Are the coefficients statistically sufficient?

As you can see, BigQuery can give you insights into many different problems from many different angles.

Click Check my progress below to verify you're on track in this lab.

Find correlation between weather and complaints

Summary

In this lab you did ad-hoc queries on two datasets. You were able to query the data without setting up any clusters, creating any indexes, etc. You were also able to mash up the two datasets and get some interesting insights. All without ever leaving your browser!

Congratulations!

You learned how to run some very interesting queries on BigQuery!

Finish your quest

This self-paced lab is part of the Scientific Data Processing quest. A quest is a series of related labs that form a learning path. Completing this quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in this quest and get immediate completion credit if you've taken this lab. See other available quests.

Take your next lab

Continue your quest with Distributed Image Processing in Cloud Dataproc, or try one of these:

Next steps / Learn more

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated September 14, 2022

Lab Last Tested September 14, 2022

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.