Query a public dataset with the BigQuery Java client library
- Activate Cloud Shell in a Google Cloud project.
- Open the Cloud Shell Editor.
- Prepare dependencies for queries.
- Query a public dataset in BigQuery.
- Clean up.
Estimated time to complete:
Click Startto 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 Java project using Apache Maven:
mvn archetype:generate \ -DgroupId = com.google.app \ -DartifactId = bigquery-java-quickstart \ -DinteractiveMode = false
This command creates a Maven project that's named
bigquery-java-quickstart
.The output is similar to the following. Several lines are omitted to simplify the output.
[INFO] Scanning for projects... ... [INFO] Building Maven Stub Project (No POM) 1 ... [INFO] BUILD SUCCESS ...
There are many dependency management systems that you can use other than Maven. For more information, learn how to set up a Java development environment to use with client libraries.
-
Rename the
App.java
file that Maven creates by default:mv \ bigquery-java-quickstart/src/main/java/com/google/app/App.java \ bigquery-java-quickstart/src/main/java/com/google/app/SimpleApp.java
-
Open the Cloud Shell Editor:
cloudshell workspace bigquery-java-quickstart
-
If you are prompted whether to synchronize the Java classpath or configuration, click Always.
If you are not prompted and encounter an error that is related to the classpath during this walkthrough, do the following:
- Click File > Preferences > Open Settings (UI).
- Click Extensions > Java.
- Scroll to Configuration: Update Build Configurationand select automatic.
To learn how to prepare dependencies for queries, click Next.
Prepare dependencies for queries
-
In the Explorerpane, locate your
BIGQUERY-JAVA-QUICKSTART
project. -
Click the
pom.xml
file to open it. -
Inside the
<dependencies>
tag, add the following dependency after any existing ones. Do not replace any existing dependencies.<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-bigquery</artifactId> </dependency>
-
On the line following the closing tag (
</dependencies>
), add the following:<dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>libraries-bom</artifactId> <version>26.1.5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
To learn how to query a public dataset in BigQuery, click Next.
Query a public dataset in BigQuery
-
In the Explorerpane, in your
BIGQUERY-JAVA-QUICKSTART
project, click src > main/java/com/google/app > SimpleApp.java. The file opens. -
To create a query against the
bigquery-public-data.stackoverflow
dataset, leave the first line of the file (package com.google.app;
), and replace the remaining contents of the file with the following code:
The query returns the top 10 most viewed Stack Overflow pages and their view counts.
Right-click SimpleApp.javaand click Run Java. If you are prompted to authorize Cloud Shell and agree to the terms, click Authorize.
The result is similar to the following:
https://stackoverflow.com/questions/35159967 : 170023 views
https://stackoverflow.com/questions/22879669 : 142581 views
https://stackoverflow.com/questions/10604135 : 132406 views
https://stackoverflow.com/questions/44564887 : 128781 views
https://stackoverflow.com/questions/27060396 : 127008 views
https://stackoverflow.com/questions/12482637 : 120766 views
https://stackoverflow.com/questions/20673986 : 115720 views
https://stackoverflow.com/questions/39109817 : 108368 views
https://stackoverflow.com/questions/11057219 : 105175 views
https://stackoverflow.com/questions/43195143 : 101878 views
You have successfully queried a public dataset with the BigQuery Java client library.
To avoid incurring charges to your account and learn about next steps, click Next.