Checkpoints
Run a query (dataset: samples, table: shakespeare, substring: raisin)
/ 10
Run a query (dataset: samples, table: shakespeare, substring: huzzah)
/ 10
Create a new dataset (name: babynames)
/ 20
Load the data into a new table
/ 20
Run queries against your dataset table
/ 20
Remove the babynames dataset
/ 20
BigQuery: Qwik Start - Command Line
GSP071
Overview
Storing and querying massive datasets can be time consuming and expensive without the right hardware and infrastructure. BigQuery is a serverless, highly scalable cloud data warehouse that solves this problem by enabling super-fast SQL queries using the processing power of Google's infrastructure. Simply move your data into BigQuery and let us handle the hard work. You can control access to both the project and your data based on your business needs, such as giving others the ability to view or query your data.
You can access BigQuery by using the Console, Web UI or a command-line tool using a variety of client libraries such as Java, .NET, or Python. There are also a variety of solution providers that you can use to interact with BigQuery.
This hands-on lab shows you how to use bq
, the python-based command line tool for BigQuery, to query public tables and load sample data into BigQuery.
What you'll do
- Query a public dataset
- Create a new dataset
- Load data into a new table
- Query a custom table
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).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
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 Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
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. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
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 Google Cloud console opens in this tab.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Examine a table
BigQuery offers a number of sample tables that you can run queries against. In this lab, you'll run queries against the shakespeare
table, which contains an entry for every word in every play.
To examine the schema of the Shakespeare table in the samples dataset, run:
In this command you're doing the following:
-
bq
to invoke the BigQuery command line tool -
show
is the action - Then you're listing the name of the
project:public dataset.table
in BigQuery that you want to see.
Output:
Task 2. Run the help command
When you include a command name with the help commands, you get information about that specific command.
- For example, the following call to
bq help
retrieves information about thequery
command:
- To see a list of all of the commands
bq
uses, run justbq help
.
Task 3. Run a query
Now you'll run a query to see how many times the substring "raisin" appears in Shakespeare's works.
- To run a query, run the command
bq query "[SQL_STATEMENT]"
:
-
Escape any quotation marks inside the [SQL_STATEMENT] with a \ mark, or
-
Use a different quotation mark type than the surrounding marks ("versus").
- Run the following standard SQL query in Cloud Shell to count the number of times that the substring "raisin" appears in all of Shakespeare's works:
In this command:
-
--use_legacy_sql=false
makes standard SQL the default query syntax.
Output:
The table demonstrates that although the actual word raisin doesn't appear, the letters appear in order in several of Shakespeare's works.
Test completed task
Click Check my progress to verify your performed task. If you have successfully run a query against a public dataset, you will see an assessment score.
If you search for a word that isn't in Shakespeare's works, no results are returned.
- Run the following search for "huzzah", returns no matches:
Test completed task
Click Check my progress to verify your performed task. If you have successfully run a query against a public dataset, you will see an assessment score.
Task 4. Create a new table
Now create your own table. Every table is stored inside a dataset. A dataset is a group of resources, such as tables and views.
Create a new dataset
- Use the
bq ls
command to list any existing datasets in your project:
You will be brought back to the command line since there aren't any datasets in your project yet.
- Run
bq ls
and thebigquery-public-data
Project ID to list the datasets in that specific project, followed by a colon (:):
Output:
Now create a dataset. A dataset name can be up to 1,024 characters long, and consist of A-Z, a-z, 0-9, and the underscore, but it cannot start with a number or underscore, or have spaces.
- Use the
bq mk
command to create a new dataset namedbabynames
in your project:
Sample output:
Test completed task
Click Check my progress to verify your performed task. If you have successfully created a BigQuery dataset named babynames, you will see an assessment score.
- Run
bq ls
to confirm that the dataset now appears as part of your project:
Sample output:
Upload the dataset
Before you can build the table, you need to add the dataset to your project. The custom data file you'll use contains approximately 7 MB of data about popular baby names, provided by the US Social Security Administration.
- Run this command to add the baby names zip file to your project, using the URL for the data file:
- List the file:
You can see the name of the file added to your project.
- Now unzip the file:
- That's a pretty big list of text files! List the files again:
The bq load
command creates or updates a table and loads data in a single step.
You will use the bq load
command to load your source file into a new table called names2010 in the babynames dataset you just created. By default, this runs synchronously, and will take a few seconds to complete.
The bq load
arguments you'll be running are:
- Create your table:
Sample output:
Test completed task
Click Check my progress to verify your performed task. If you have successfully loaded data into a dataset table, you will see an assessment score.
- Run
bq ls
andbabynames
to confirm that the table now appears in your dataset:
Output:
- Run
bq show
and yourdataset.table
to see the schema:
Output:
-E
flag. Learn more about Character Encodings from the Introduction to loading data guide.
Task 5. Run queries
Now you're ready to query the data and return some interesting results.
- Run the following command to return the top 5 most popular girls names:
Output:
- Run the following command to see the top 5 most unusual boys names:
Output:
Test completed task
Click Check my progress to verify your performed task. If you have successfully a against a custom dataset, you will see an assessment score.
Task 6. Test your Understanding
Below are multiple choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.
Task 7. Clean up
- Run the
bq rm
command to remove thebabynames
dataset with the-r
flag to delete all tables in the dataset:
- Confirm the delete command by typing
Y
.
Test completed task
Click Check my progress to verify your performed task. If you have successfully removed babynames dataset, you will see an assessment score.
Congratulations!
Now you can use the command line to query public tables and load sample data into BigQuery.
Next steps / Learn more
This lab is also part of a series of labs called Qwik Starts. These labs are designed to give you a little taste of the many features available with Google Cloud. Search for "Qwik Starts" in the lab catalog to find the next lab you'd like to take!
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 January 26, 2024
Lab Last Tested August 24, 2023
Copyright 2024 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.