Query a public dataset with the BigQuery PHP client library
- Activate Cloud Shell in a Google Cloud project.
- Open the Cloud Shell Editor.
- Prepare files for queries.
- Query a public dataset in BigQuery.
- Clean up.
Estimated time to complete:
Click Start to begin.
Activate Cloud Shell in a Google Cloud project
If you do not enable billing for a project, you automatically work in the BigQuery sandbox. The BigQuery sandbox lets you learn BigQuery with a limited set of BigQuery features at no charge. If you do not plan to use your project beyond this document, we recommend that you use the BigQuery sandbox.
- Click
Activate Cloud Shell. Show me
To learn how to open the Cloud Shell Editor, click Next.
Open the Cloud Shell Editor
In Cloud Shell, create a new PHP project and file:
mkdir bigquery-php-quickstart \ && touch \ bigquery-php-quickstart/app.php
This command creates a PHP project that's named
bigquery-php-quickstart
and a file that's namedapp.php
.Open the Cloud Shell Editor:
cloudshell workspace bigquery-php-quickstart
To learn how to prepare files for queries, click Next.
Prepare files for queries
To open a terminal in the Cloud Shell Editor, click Open Terminal.
Open your project directory:
cd bigquery-php-quickstart
Install the BigQuery client library for PHP:
composer require google/cloud-bigquery
The output is similar to the following. Several lines are omitted to simplify the output.
Running composer update google/cloud-bigquery Loading composer repositories with package information Updating dependencies ... No security vulnerability advisories found Using version ^1.24 for google/cloud-bigquery
To learn how to query a public dataset in BigQuery, click Next.
Query a public dataset in BigQuery
Click Open Editor.
In the Explorer pane, locate your
BIGQUERY-PHP-QUICKSTART
project.Click the
app.php
file to open it.To create a query against the
bigquery-public-data.stackoverflow
dataset that returns the top 10 most viewed Stack Overflow pages and their view counts, copy the following code into theapp.php
file:Click Open Terminal.
In the terminal, run the
app.php
script. If you are prompted to authorize Cloud Shell and agree to the terms, click Authorize.php app.php
The result is similar to the following:
--- Row 1 --- url: https://stackoverflow.com/questions/35159967, 170023 views --- Row 2 --- url: https://stackoverflow.com/questions/22879669, 142581 views --- Row 3 --- url: https://stackoverflow.com/questions/10604135, 132406 views --- Row 4 --- url: https://stackoverflow.com/questions/44564887, 128781 views --- Row 5 --- url: https://stackoverflow.com/questions/27060396, 127008 views --- Row 6 --- url: https://stackoverflow.com/questions/12482637, 120766 views --- Row 7 --- url: https://stackoverflow.com/questions/20673986, 115720 views --- Row 8 --- url: https://stackoverflow.com/questions/39109817, 108368 views --- Row 9 --- url: https://stackoverflow.com/questions/11057219, 105175 views --- Row 10 --- url: https://stackoverflow.com/questions/43195143, 101878 views Found 10 row(s)
You have successfully queried a public dataset with the BigQuery PHP client library.
To avoid incurring charges to your account and learn about next steps, click Next.
Next steps
Keep the resources that you created and do more with BigQuery, or clean up to avoid billing charges.
Do more with BigQuery
- Learn more about using the BigQuery PHP client library.
- Learn more about BigQuery public datasets.
- Learn how to load data into BigQuery.
- Learn more about querying data in BigQuery.
- Get updates about BigQuery.
- Learn about BigQuery pricing.
- Learn about BigQuery quotas and limits.
Clean up
To avoid incurring charges to your Google Cloud account, either delete your Google Cloud project, or delete the resources that you created in this walkthrough.
Delete the project
If you created a new project to learn about BigQuery and you no longer need the project, delete it. Be aware that deleting a project deletes everything in the project and custom project IDs are lost.
Delete the resources
If you used an existing project, delete the bigquery-php-quickstart
folder
that you created:
In Cloud Shell, move up a directory:
cd ..
Delete the resources that you created:
rm -R bigquery-php-quickstart
The
-R
flag deletes all assets in a folder.