{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "\n", "So far, you've worked with many types of data, including numeric types (integers, floating point values), strings, and the [DATETIME](https://www.kaggle.com/dansbecker/order-by) type. In this tutorial, you'll learn how to query **nested and repeated data**. These are the most complex data types that you can find in BigQuery datasets! \n", "\n", "# Nested data\n", "\n", "Consider a hypothetical dataset containing information about pets and their toys. We could organize this information in two different tables (a `pets` table and a `toys` table). The `toys` table could contain a \"Pet_ID\" column that could be used to match each toy to the pet that owns it.\n", "\n", "Another option in BigQuery is to organize all of the information in a single table, similar to the `pets_and_toys` table below. \n", "\n", "![nested data](https://storage.googleapis.com/kaggle-media/learn/images/wxuogYA.png)\n", "\n", "In this case, all of the information from the `toys` table is collapsed into a single column (the \"Toy\" column in the `pets_and_toys` table). We refer to the \"Toy\" column in the `pets_and_toys` table as a **nested** column, and say that the \"Name\" and \"Type\" fields are nested inside of it. \n", "\n", "Nested columns have type **STRUCT** (or type **RECORD**). This is reflected in the table schema below.\n", "> Recall that we refer to the structure of a table as its **schema**. If you need to review how to interpret table schema, feel free to check out [this lesson](https://www.kaggle.com/dansbecker/getting-started-with-sql-and-bigquery) from the Intro to SQL micro-course.\n", "\n", "![nested data](https://storage.googleapis.com/kaggle-media/learn/images/epXFXdb.png)\n", "\n", "To query a column with nested data, we need to identify each field in the context of the column that contains it: \n", "- `Toy.Name` refers to the \"Name\" field in the \"Toy\" column, and\n", "- `Toy.Type` refers to the \"Type\" field in the \"Toy\" column. \n", "\n", "![nested data](https://storage.googleapis.com/kaggle-media/learn/images/eE2Gt62.png)\n", "\n", "Otherwise, our usual rules remain the same - we need not change anything else about our queries.\n", "\n", "# Repeated data \n", "\n", "Now consider the (more realistic!) case where each pet can have multiple toys. In this case, to collapse this information into a single table, we need to leverage a different datatype.\n", "\n", "![repeated data](https://storage.googleapis.com/kaggle-media/learn/images/S93FJTE.png)\n", "\n", "We say that the \"Toys\" column contains **repeated data**, because it permits more than one value for each row. This is reflected in the table schema below, where the mode of the \"Toys\" column appears as **'REPEATED'**.\n", "\n", "![repeated data](https://storage.googleapis.com/kaggle-media/learn/images/KlrjpDM.png)\n", "\n", "Each entry in a repeated field is an **ARRAY**, or an ordered list of (zero or more) values with the same datatype. For instance, the entry in the \"Toys\" column for Moon the Dog is **[Frisbee, Bone, Rope]**, which is an ARRAY with three values.\n", "\n", "When querying repeated data, we need to put the name of the column containing the repeated data inside an **UNNEST()** function. \n", "\n", "![repeated data](https://storage.googleapis.com/kaggle-media/learn/images/p3fXPxY.png)\n", "\n", "This essentially flattens the repeated data (which is then appended to the right side of the table) so that we have one element on each row. For an illustration of this, check out the image below.\n", "\n", "![repeated data](https://storage.googleapis.com/kaggle-media/learn/images/8j4XK8f.png)\n", "\n", "# Nested and repeated data\n", "\n", "Now, what if pets can have multiple toys, _and_ we'd like to keep track of both the name and type of each toy? In this case, we can make the \"Toys\" column both **nested** and **repeated**.\n", "\n", "![repeated data](https://storage.googleapis.com/kaggle-media/learn/images/psKtza2.png)\n", "\n", "In the `more_pets_and_toys` table above, \"Name\" and \"Type\" are both fields contained within the \"Toys\" STRUCT, and each entry in both \"Toys.Name\" and \"Toys.Type\" is an ARRAY.\n", "\n", "![repeated data](https://storage.googleapis.com/kaggle-media/learn/images/fO5OymI.png)\n", "\n", "Let's look at a sample query.\n", "\n", "![repeated data](https://storage.googleapis.com/kaggle-media/learn/images/DiMCZaO.png)\n", "\n", "Since the \"Toys\" column is repeated, we flatten it with the **UNNEST()** function. And, since we give the flattened column an alias of `t`, we can refer to the \"Name\" and \"Type\" fields in the \"Toys\" column as `t.Name` and `t.Type`, respectively. \n", "\n", "To reinforce what you've learned, we'll apply these ideas to a real dataset in the section below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example\n", "\n", "We'll work with the [Google Analytics Sample](https://www.kaggle.com/bigquery/google-analytics-sample) dataset. It contains information tracking the behavior of visitors to the Google Merchandise store, an e-commerce website that sells Google branded items.\n", "\n", "We begin by printing the first few rows of the `ga_sessions_20170801` table. (_We have hidden the corresponding code. To take a peek, click on the \"Code\" button below._) This table tracks visits to the website on August 1, 2017. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_kg_hide-input": true }, "outputs": [], "source": [ "from google.cloud import bigquery\n", "\n", "# Create a \"Client\" object\n", "client = bigquery.Client()\n", "\n", "# Construct a reference to the \"google_analytics_sample\" dataset\n", "dataset_ref = client.dataset(\"google_analytics_sample\", project=\"bigquery-public-data\")\n", "\n", "# Construct a reference to the \"ga_sessions_20170801\" table\n", "table_ref = dataset_ref.table(\"ga_sessions_20170801\")\n", "\n", "# API request - fetch the table\n", "table = client.get_table(table_ref)\n", "\n", "# Preview the first five lines of the table\n", "client.list_rows(table, max_results=5).to_dataframe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a description of each field, refer to this [data dictionary](https://support.google.com/analytics/answer/3437719?hl=en).\n", "\n", "The table has many nested fields, which you can verify by looking at either the [data dictionary](https://support.google.com/analytics/answer/3437719?hl=en) (_hint: search for appearances of 'RECORD' on the page_) or the table preview above.\n", "\n", "In our first query against this table, we'll work with the \"totals\" and \"device\" columns. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_kg_hide-input": true }, "outputs": [], "source": [ "print(\"SCHEMA field for the 'totals' column:\\n\")\n", "print(table.schema[5])\n", "\n", "print(\"\\nSCHEMA field for the 'device' column:\\n\")\n", "print(table.schema[7])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We refer to the \"browser\" field (which is nested in the \"device\" column) and the \"transactions\" field (which is nested inside the \"totals\" column) as `device.browser` and `totals.transactions` in the query below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Query to count the number of transactions per browser\n", "query = \"\"\"\n", " SELECT device.browser AS device_browser,\n", " SUM(totals.transactions) as total_transactions\n", " FROM `bigquery-public-data.google_analytics_sample.ga_sessions_20170801`\n", " GROUP BY device_browser\n", " ORDER BY total_transactions DESC\n", " \"\"\"\n", "\n", "# Run the query, and return a pandas DataFrame\n", "result = client.query(query).result().to_dataframe()\n", "result.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By storing the information in the \"device\" and \"totals\" columns as STRUCTs (as opposed to separate tables), we avoid expensive JOINs. This increases performance and keeps us from having to worry about JOIN keys (and which tables have the exact data we need).\n", "\n", "Now we'll work with the \"hits\" column as an example of data that is both nested and repeated. Since:\n", "- \"hits\" is a STRUCT (contains nested data) and is repeated,\n", "- \"hitNumber\", \"page\", and \"type\" are all nested inside the \"hits\" column, and\n", "- \"pagePath\" is nested inside the \"page\" field,\n", "\n", "we can query these fields with the following syntax:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Query to determine most popular landing point on the website\n", "query = \"\"\"\n", " SELECT hits.page.pagePath as path,\n", " COUNT(hits.page.pagePath) as counts\n", " FROM `bigquery-public-data.google_analytics_sample.ga_sessions_20170801`, \n", " UNNEST(hits) as hits\n", " WHERE hits.type=\"PAGE\" and hits.hitNumber=1\n", " GROUP BY path\n", " ORDER BY counts DESC\n", " \"\"\"\n", "\n", "# Run the query, and return a pandas DataFrame\n", "result = client.query(query).result().to_dataframe()\n", "result.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, most users land on the website through the `\"/home\"` page.\n", "\n", "# Your turn \n", "\n", "Use what you've learned to **[query complex datatypes](https://www.kaggle.com/kernels/fork/5045823)** in a real-world dataset." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 1 }