Skip to content

Commit f0bdc4d

Browse files
feat: migrate BQ Java samples from java-docs-samples to client lib (#22)
* feat: migrate BQ Java samples from java-docs-samples to client lib * Move sample module include into profile * Fix dependency issues
1 parent 841d075 commit f0bdc4d

21 files changed

+1173
-1
lines changed

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246

247247
<modules>
248248
<module>google-cloud-bigquery</module>
249-
<module>google-cloud-bigquery-bom</module>
249+
<module>google-cloud-bigquery-bom</module>
250250
</modules>
251251

252252
<reporting>
@@ -341,6 +341,17 @@
341341
<auto-value-annotations.version>${auto-value.version}</auto-value-annotations.version>
342342
</properties>
343343
</profile>
344+
345+
<profile>
346+
<id>samples-java8AndNewer</id>
347+
<activation>
348+
<jdk>[1.8,)</jdk>
349+
</activation>
350+
<modules>
351+
<module>samples</module>
352+
</modules>
353+
</profile>
354+
344355
</profiles>
345356

346357
</project>

samples/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Getting Started with BigQuery and the Google Java API Client library
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=README.md&cloudshell_working_dir=bigquery/cloud-client/">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
[Google's BigQuery Service][BigQuery] features a REST-based API that allows
7+
developers to create applications to run ad-hoc queries on massive datasets.
8+
These sample Java applications demonstrate how to access the BigQuery API using
9+
the [Google Cloud Client Library for Java][google-cloud-java].
10+
11+
[BigQuery]: https://cloud.google.com/bigquery/
12+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
13+
14+
## Quickstart
15+
16+
Install [Maven](http://maven.apache.org/).
17+
18+
[Authenticate using a service account](https://cloud.google.com/docs/authentication/getting-started).
19+
Create a service account, download a JSON key file, and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
20+
21+
Build your project with:
22+
23+
mvn clean package -DskipTests
24+
25+
You can then run a given `ClassName` via:
26+
27+
mvn exec:java -Dexec.mainClass=com.example.bigquery.ClassName \
28+
-DpropertyName=propertyValue \
29+
-Dexec.args="any arguments to the app"
30+
31+
### Creating a new dataset (using the quickstart sample)
32+
33+
mvn exec:java -Dexec.mainClass=com.example.bigquery.QuickstartSample
34+
35+
### Running a query using standard SQL syntax
36+
37+
mvn exec:java -Dexec.mainClass=com.example.bigquery.QuerySample \
38+
-Dexec.args=' \
39+
--query="SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" \
40+
--runStandardSqlQuery'
41+
42+
### Running the simple app example
43+
44+
To run the example from the [simple app example
45+
documentation](https://cloud.google.com/bigquery/create-simple-app-api):
46+
47+
mvn exec:java -Dexec.mainClass=com.example.bigquery.SimpleApp
48+

samples/pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<!--
2+
~ Copyright 2019 Google LLC
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>com.example.bigquery</groupId>
19+
<artifactId>bigquery-google-cloud-samples</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!--
23+
The parent pom defines common style checks and testing strategies for our samples.
24+
Removing or replacing it should not affect the execution of the samples in anyway.
25+
-->
26+
<parent>
27+
<groupId>com.google.cloud.samples</groupId>
28+
<artifactId>shared-configuration</artifactId>
29+
<version>1.0.11</version>
30+
</parent>
31+
32+
<properties>
33+
<maven.compiler.target>1.8</maven.compiler.target>
34+
<maven.compiler.source>1.8</maven.compiler.source>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
</properties>
37+
38+
<dependencies>
39+
<!-- [START bigquery_java_dependencies] -->
40+
<dependency>
41+
<groupId>com.google.cloud</groupId>
42+
<artifactId>google-cloud-bigquery</artifactId>
43+
<version>1.100.0</version>
44+
</dependency>
45+
<!-- [END bigquery_java_dependencies] -->
46+
<dependency>
47+
<groupId>com.google.api</groupId>
48+
<artifactId>gax</artifactId>
49+
<version>1.49.1</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.google.cloud</groupId>
53+
<artifactId>google-cloud-core</artifactId>
54+
<version>1.91.3</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.google.auth</groupId>
58+
<artifactId>google-auth-library-oauth2-http</artifactId>
59+
<version>0.18.0</version>
60+
</dependency>
61+
62+
<!-- Test dependencies -->
63+
<dependency>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
<version>4.13-beta-3</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.google.truth</groupId>
71+
<artifactId>truth</artifactId>
72+
<version>1.0</version>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import com.google.auth.oauth2.GoogleCredentials;
20+
import com.google.auth.oauth2.ServiceAccountCredentials;
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Dataset;
24+
import java.io.File;
25+
import java.io.FileInputStream;
26+
import java.io.IOException;
27+
28+
/**
29+
* Examples for authenticating to Google BigQuery.
30+
*
31+
* <p>See: https://cloud.google.com/bigquery/authentication
32+
*/
33+
public class AuthSnippets {
34+
35+
// [START bigquery_client_default_credentials]
36+
public static void implicit() {
37+
// Instantiate a client. If you don't specify credentials when constructing a client, the
38+
// client library will look for credentials in the environment, such as the
39+
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
40+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
41+
42+
// Use the client.
43+
System.out.println("Datasets:");
44+
for (Dataset dataset : bigquery.listDatasets().iterateAll()) {
45+
System.out.printf("%s%n", dataset.getDatasetId().getDataset());
46+
}
47+
}
48+
// [END bigquery_client_default_credentials]
49+
50+
// [START bigquery_client_json_credentials]
51+
public static void explicit() throws IOException {
52+
// Load credentials from JSON key file. If you can't set the GOOGLE_APPLICATION_CREDENTIALS
53+
// environment variable, you can explicitly load the credentials file to construct the
54+
// credentials.
55+
GoogleCredentials credentials;
56+
File credentialsPath = new File("service_account.json"); // TODO: update to your key path.
57+
try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
58+
credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
59+
}
60+
61+
// Instantiate a client.
62+
BigQuery bigquery =
63+
BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();
64+
65+
// Use the client.
66+
System.out.println("Datasets:");
67+
for (Dataset dataset : bigquery.listDatasets().iterateAll()) {
68+
System.out.printf("%s%n", dataset.getDatasetId().getDataset());
69+
}
70+
}
71+
// [END bigquery_client_json_credentials]
72+
73+
public static void main(String... args) throws IOException {
74+
boolean validArgs = args.length == 1;
75+
String sample = "explicit";
76+
if (validArgs) {
77+
sample = args[0];
78+
if (!sample.equals("explicit") && !sample.equals("implicit")) {
79+
validArgs = false;
80+
}
81+
}
82+
83+
if (!validArgs) {
84+
System.err.println("Expected auth type argument: implict|explict");
85+
System.exit(1);
86+
}
87+
88+
if (sample.equals("implicit")) {
89+
implicit();
90+
} else {
91+
explicit();
92+
}
93+
}
94+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_create_dataset]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Dataset;
24+
import com.google.cloud.bigquery.DatasetInfo;
25+
26+
public class CreateDataset {
27+
28+
public static void runCreateDataset() {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String datasetName = "my-dataset-name";
31+
createDataset(datasetName);
32+
}
33+
34+
public static void createDataset(String datasetName) {
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests.
37+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
38+
39+
DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
40+
try {
41+
Dataset newDataset = bigquery.create(datasetInfo);
42+
String newDatasetName = newDataset.getDatasetId().getDataset();
43+
System.out.println(newDatasetName + " created successfully");
44+
} catch (BigQueryException e) {
45+
System.out.println("Dataset was not created. \n" + e.toString());
46+
}
47+
}
48+
}
49+
// [END bigquery create_dataset]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_delete_dataset]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.DatasetId;
24+
25+
public class DeleteDataset {
26+
27+
public static void runDeleteDataset() {
28+
// TODO(developer): Replace these variables before running the sample.\
29+
String projectId = "my-project-id";
30+
String datasetName = "my-dataset-name";
31+
deleteDataset(projectId, datasetName);
32+
}
33+
34+
public static void deleteDataset(String projectId, String datasetName) {
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests.
37+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
38+
39+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
40+
boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
41+
if (success) {
42+
System.out.println("Dataset deleted successfully");
43+
} else {
44+
System.out.println("Dataset was not found");
45+
}
46+
}
47+
}
48+
// [END bigquery_delete_dataset]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_list_datasets]
20+
import com.google.api.gax.paging.Page;
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQuery.DatasetListOption;
23+
import com.google.cloud.bigquery.BigQueryException;
24+
import com.google.cloud.bigquery.BigQueryOptions;
25+
import com.google.cloud.bigquery.Dataset;
26+
27+
public class ListDatasets {
28+
29+
public static void runListDatasets() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my-project-id";
32+
listDatasets(projectId);
33+
}
34+
35+
public static void listDatasets(String projectId) {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests.
38+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
39+
40+
// List datasets in a specified project
41+
try {
42+
Page<Dataset> datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100));
43+
for (Dataset dataset : datasets.iterateAll()) {
44+
System.out.println(dataset.getDatasetId() + " dataset in project listed successfully");
45+
}
46+
} catch (BigQueryException e) {
47+
System.out.println("Project does not contain any datasets \n" + e.toString());
48+
}
49+
}
50+
}
51+
// [END bigquery_list_datasets]

0 commit comments

Comments
 (0)