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 Configuration
and select
automatic
.
In the
Explorer
pane, 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>
In the
Explorer
pane, 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.java
and 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.