@@ -341,6 +341,17 @@
${auto-value.version}
+
+
+ samples-java8AndNewer
+
+ [1.8,)
+
+
+ samples
+
+
+
\ No newline at end of file
diff --git a/samples/README.md b/samples/README.md
new file mode 100644
index 000000000..b1988a7f8
--- /dev/null
+++ b/samples/README.md
@@ -0,0 +1,48 @@
+# Getting Started with BigQuery and the Google Java API Client library
+
+
+
+
+[Google's BigQuery Service][BigQuery] features a REST-based API that allows
+developers to create applications to run ad-hoc queries on massive datasets.
+These sample Java applications demonstrate how to access the BigQuery API using
+the [Google Cloud Client Library for Java][google-cloud-java].
+
+[BigQuery]: https://cloud.google.com/bigquery/
+[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
+
+## Quickstart
+
+Install [Maven](http://maven.apache.org/).
+
+[Authenticate using a service account](https://cloud.google.com/docs/authentication/getting-started).
+Create a service account, download a JSON key file, and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
+
+Build your project with:
+
+ mvn clean package -DskipTests
+
+You can then run a given `ClassName` via:
+
+ mvn exec:java -Dexec.mainClass=com.example.bigquery.ClassName \
+ -DpropertyName=propertyValue \
+ -Dexec.args="any arguments to the app"
+
+### Creating a new dataset (using the quickstart sample)
+
+ mvn exec:java -Dexec.mainClass=com.example.bigquery.QuickstartSample
+
+### Running a query using standard SQL syntax
+
+ mvn exec:java -Dexec.mainClass=com.example.bigquery.QuerySample \
+ -Dexec.args=' \
+ --query="SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;" \
+ --runStandardSqlQuery'
+
+### Running the simple app example
+
+To run the example from the [simple app example
+documentation](https://cloud.google.com/bigquery/create-simple-app-api):
+
+ mvn exec:java -Dexec.mainClass=com.example.bigquery.SimpleApp
+
diff --git a/samples/pom.xml b/samples/pom.xml
new file mode 100644
index 000000000..de6e9e984
--- /dev/null
+++ b/samples/pom.xml
@@ -0,0 +1,76 @@
+
+
+ 4.0.0
+ com.example.bigquery
+ bigquery-google-cloud-samples
+ jar
+
+
+
+ com.google.cloud.samples
+ shared-configuration
+ 1.0.11
+
+
+
+ 1.8
+ 1.8
+ UTF-8
+
+
+
+
+
+ com.google.cloud
+ google-cloud-bigquery
+ 1.100.0
+
+
+
+ com.google.api
+ gax
+ 1.49.1
+
+
+ com.google.cloud
+ google-cloud-core
+ 1.91.3
+
+
+ com.google.auth
+ google-auth-library-oauth2-http
+ 0.18.0
+
+
+
+
+ junit
+ junit
+ 4.13-beta-3
+ test
+
+
+ com.google.truth
+ truth
+ 1.0
+ test
+
+
+
diff --git a/samples/src/main/java/com/example/bigquery/AuthSnippets.java b/samples/src/main/java/com/example/bigquery/AuthSnippets.java
new file mode 100644
index 000000000..a1f3bfb3e
--- /dev/null
+++ b/samples/src/main/java/com/example/bigquery/AuthSnippets.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.ServiceAccountCredentials;
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.Dataset;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+/**
+ * Examples for authenticating to Google BigQuery.
+ *
+ * See: https://cloud.google.com/bigquery/authentication
+ */
+public class AuthSnippets {
+
+ // [START bigquery_client_default_credentials]
+ public static void implicit() {
+ // Instantiate a client. If you don't specify credentials when constructing a client, the
+ // client library will look for credentials in the environment, such as the
+ // GOOGLE_APPLICATION_CREDENTIALS environment variable.
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+
+ // Use the client.
+ System.out.println("Datasets:");
+ for (Dataset dataset : bigquery.listDatasets().iterateAll()) {
+ System.out.printf("%s%n", dataset.getDatasetId().getDataset());
+ }
+ }
+ // [END bigquery_client_default_credentials]
+
+ // [START bigquery_client_json_credentials]
+ public static void explicit() throws IOException {
+ // Load credentials from JSON key file. If you can't set the GOOGLE_APPLICATION_CREDENTIALS
+ // environment variable, you can explicitly load the credentials file to construct the
+ // credentials.
+ GoogleCredentials credentials;
+ File credentialsPath = new File("service_account.json"); // TODO: update to your key path.
+ try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
+ credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
+ }
+
+ // Instantiate a client.
+ BigQuery bigquery =
+ BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();
+
+ // Use the client.
+ System.out.println("Datasets:");
+ for (Dataset dataset : bigquery.listDatasets().iterateAll()) {
+ System.out.printf("%s%n", dataset.getDatasetId().getDataset());
+ }
+ }
+ // [END bigquery_client_json_credentials]
+
+ public static void main(String... args) throws IOException {
+ boolean validArgs = args.length == 1;
+ String sample = "explicit";
+ if (validArgs) {
+ sample = args[0];
+ if (!sample.equals("explicit") && !sample.equals("implicit")) {
+ validArgs = false;
+ }
+ }
+
+ if (!validArgs) {
+ System.err.println("Expected auth type argument: implict|explict");
+ System.exit(1);
+ }
+
+ if (sample.equals("implicit")) {
+ implicit();
+ } else {
+ explicit();
+ }
+ }
+}
diff --git a/samples/src/main/java/com/example/bigquery/CreateDataset.java b/samples/src/main/java/com/example/bigquery/CreateDataset.java
new file mode 100644
index 000000000..f61fce657
--- /dev/null
+++ b/samples/src/main/java/com/example/bigquery/CreateDataset.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_create_dataset]
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryException;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.Dataset;
+import com.google.cloud.bigquery.DatasetInfo;
+
+public class CreateDataset {
+
+ public static void runCreateDataset() {
+ // TODO(developer): Replace these variables before running the sample.
+ String datasetName = "my-dataset-name";
+ createDataset(datasetName);
+ }
+
+ public static void createDataset(String datasetName) {
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests.
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+
+ DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
+ try {
+ Dataset newDataset = bigquery.create(datasetInfo);
+ String newDatasetName = newDataset.getDatasetId().getDataset();
+ System.out.println(newDatasetName + " created successfully");
+ } catch (BigQueryException e) {
+ System.out.println("Dataset was not created. \n" + e.toString());
+ }
+ }
+}
+// [END bigquery create_dataset]
diff --git a/samples/src/main/java/com/example/bigquery/DeleteDataset.java b/samples/src/main/java/com/example/bigquery/DeleteDataset.java
new file mode 100644
index 000000000..f0f8ac96a
--- /dev/null
+++ b/samples/src/main/java/com/example/bigquery/DeleteDataset.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_delete_dataset]
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.DatasetId;
+
+public class DeleteDataset {
+
+ public static void runDeleteDataset() {
+ // TODO(developer): Replace these variables before running the sample.\
+ String projectId = "my-project-id";
+ String datasetName = "my-dataset-name";
+ deleteDataset(projectId, datasetName);
+ }
+
+ public static void deleteDataset(String projectId, String datasetName) {
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests.
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+
+ DatasetId datasetId = DatasetId.of(projectId, datasetName);
+ boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
+ if (success) {
+ System.out.println("Dataset deleted successfully");
+ } else {
+ System.out.println("Dataset was not found");
+ }
+ }
+}
+// [END bigquery_delete_dataset]
diff --git a/samples/src/main/java/com/example/bigquery/ListDatasets.java b/samples/src/main/java/com/example/bigquery/ListDatasets.java
new file mode 100644
index 000000000..69f8b91af
--- /dev/null
+++ b/samples/src/main/java/com/example/bigquery/ListDatasets.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_list_datasets]
+import com.google.api.gax.paging.Page;
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQuery.DatasetListOption;
+import com.google.cloud.bigquery.BigQueryException;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.Dataset;
+
+public class ListDatasets {
+
+ public static void runListDatasets() {
+ // TODO(developer): Replace these variables before running the sample.
+ String projectId = "my-project-id";
+ listDatasets(projectId);
+ }
+
+ public static void listDatasets(String projectId) {
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests.
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+
+ // List datasets in a specified project
+ try {
+ Page datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100));
+ for (Dataset dataset : datasets.iterateAll()) {
+ System.out.println(dataset.getDatasetId() + " dataset in project listed successfully");
+ }
+ } catch (BigQueryException e) {
+ System.out.println("Project does not contain any datasets \n" + e.toString());
+ }
+ }
+}
+// [END bigquery_list_datasets]
diff --git a/samples/src/main/java/com/example/bigquery/QuickstartSample.java b/samples/src/main/java/com/example/bigquery/QuickstartSample.java
new file mode 100644
index 000000000..edcf0df96
--- /dev/null
+++ b/samples/src/main/java/com/example/bigquery/QuickstartSample.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_quickstart]
+// Imports the Google Cloud client library
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.Dataset;
+import com.google.cloud.bigquery.DatasetInfo;
+
+public class QuickstartSample {
+ public static void main(String... args) throws Exception {
+ // Instantiate a client. If you don't specify credentials when constructing a client, the
+ // client library will look for credentials in the environment, such as the
+ // GOOGLE_APPLICATION_CREDENTIALS environment variable.
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+
+ // The name for the new dataset
+ String datasetName = "my_new_dataset";
+
+ // Prepares a new dataset
+ Dataset dataset = null;
+ DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
+
+ // Creates the dataset
+ dataset = bigquery.create(datasetInfo);
+
+ System.out.printf("Dataset %s created.%n", dataset.getDatasetId().getDataset());
+ }
+}
+// [END bigquery_quickstart]
diff --git a/samples/src/main/java/com/example/bigquery/SimpleApp.java b/samples/src/main/java/com/example/bigquery/SimpleApp.java
new file mode 100644
index 000000000..867e63979
--- /dev/null
+++ b/samples/src/main/java/com/example/bigquery/SimpleApp.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_simple_app_all]
+// [START bigquery_simple_app_deps]
+
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.FieldValueList;
+import com.google.cloud.bigquery.Job;
+import com.google.cloud.bigquery.JobId;
+import com.google.cloud.bigquery.JobInfo;
+import com.google.cloud.bigquery.QueryJobConfiguration;
+import com.google.cloud.bigquery.TableResult;
+import java.util.UUID;
+
+// [END bigquery_simple_app_deps]
+
+public class SimpleApp {
+ public static void main(String... args) throws Exception {
+ // [START bigquery_simple_app_client]
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+ // [END bigquery_simple_app_client]
+ // [START bigquery_simple_app_query]
+ QueryJobConfiguration queryConfig =
+ QueryJobConfiguration.newBuilder(
+ "SELECT "
+ + "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, "
+ + "view_count "
+ + "FROM `bigquery-public-data.stackoverflow.posts_questions` "
+ + "WHERE tags like '%google-bigquery%' "
+ + "ORDER BY favorite_count DESC LIMIT 10")
+ // Use standard SQL syntax for queries.
+ // See: https://cloud.google.com/bigquery/sql-reference/
+ .setUseLegacySql(false)
+ .build();
+
+ // Create a job ID so that we can safely retry.
+ JobId jobId = JobId.of(UUID.randomUUID().toString());
+ Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
+
+ // Wait for the query to complete.
+ queryJob = queryJob.waitFor();
+
+ // Check for errors
+ if (queryJob == null) {
+ throw new RuntimeException("Job no longer exists");
+ } else if (queryJob.getStatus().getError() != null) {
+ // You can also look at queryJob.getStatus().getExecutionErrors() for all
+ // errors, not just the latest one.
+ throw new RuntimeException(queryJob.getStatus().getError().toString());
+ }
+ // [END bigquery_simple_app_query]
+
+ // [START bigquery_simple_app_print]
+ // Get the results.
+ TableResult result = queryJob.getQueryResults();
+
+ // Print all pages of the results.
+ for (FieldValueList row : result.iterateAll()) {
+ String url = row.get("url").getStringValue();
+ long viewCount = row.get("view_count").getLongValue();
+ System.out.printf("url: %s views: %d%n", url, viewCount);
+ }
+ // [END bigquery_simple_app_print]
+ }
+}
+// [END bigquery_simple_app_all]
diff --git a/samples/src/main/java/com/example/bigquery/UpdateDatasetAccess.java b/samples/src/main/java/com/example/bigquery/UpdateDatasetAccess.java
new file mode 100644
index 000000000..1166a2baa
--- /dev/null
+++ b/samples/src/main/java/com/example/bigquery/UpdateDatasetAccess.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_update_dataset_access]
+import com.google.cloud.bigquery.Acl;
+import com.google.cloud.bigquery.Acl.Role;
+import com.google.cloud.bigquery.Acl.User;
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryException;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.Dataset;
+import java.util.ArrayList;
+
+public class UpdateDatasetAccess {
+
+ public static void runUpdateDatasetAccess() {
+ // TODO(developer): Replace these variables before running the sample.
+ String datasetName = "my-dataset-name";
+ updateDatasetAccess(datasetName);
+ }
+
+ public static void updateDatasetAccess(String datasetName) {
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests.
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+
+ Dataset dataset = bigquery.getDataset(datasetName);
+
+ // Create a new ACL granting the READER role to "sample.bigquery.dev@gmail.com"
+ // For more information on the types of ACLs available see:
+ // https://cloud.google.com/storage/docs/access-control/lists
+ Acl newEntry = Acl.of(new User("sample.bigquery.dev@gmail.com"), Role.READER);
+
+ // Get a copy of the ACLs list from the dataset and append the new entry
+ ArrayList acls = new ArrayList<>(dataset.getAcl());
+ acls.add(newEntry);
+
+ // Update the dataset to use the new ACLs
+ try {
+ bigquery.update(dataset.toBuilder().setAcl(acls).build());
+ System.out.println("Dataset Access Control updated successfully");
+ } catch (BigQueryException e) {
+ System.out.println("Dataset Access control was not updated \n" + e.toString());
+ }
+ }
+}
+// [END bigquery_update_dataset_access]
diff --git a/samples/src/main/java/com/example/bigquery/UpdateDatasetDescription.java b/samples/src/main/java/com/example/bigquery/UpdateDatasetDescription.java
new file mode 100644
index 000000000..8f6ab8a5c
--- /dev/null
+++ b/samples/src/main/java/com/example/bigquery/UpdateDatasetDescription.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_update_dataset_description]
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryException;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.Dataset;
+
+public class UpdateDatasetDescription {
+
+ public static void runUpdateDatasetDescription() {
+ // TODO(developer): Replace these variables before running the sample.
+ String datasetName = "my-dataset-name";
+ String newDescription = "this is the new dataset description";
+ updateDatasetDescription(datasetName, newDescription);
+ }
+
+ public static void updateDatasetDescription(String datasetName, String newDescription) {
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests.
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+
+ Dataset dataset = bigquery.getDataset(datasetName);
+
+ // Update dataset description
+ try {
+ bigquery.update(dataset.toBuilder().setDescription(newDescription).build());
+ System.out.println("Dataset description updated successfully to " + newDescription);
+ } catch (BigQueryException e) {
+ System.out.println("Dataset description was not updated \n" + e.toString());
+ }
+ }
+}
+// [END bigquery_update_dataset_description]
diff --git a/samples/src/main/java/com/example/bigquery/UpdateDatasetExpiration.java b/samples/src/main/java/com/example/bigquery/UpdateDatasetExpiration.java
new file mode 100644
index 000000000..4cb16b4f4
--- /dev/null
+++ b/samples/src/main/java/com/example/bigquery/UpdateDatasetExpiration.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_update_dataset_expiration]
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryException;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.Dataset;
+import java.util.concurrent.TimeUnit;
+
+public class UpdateDatasetExpiration {
+
+ public static void runUpdateDatasetExpiration() {
+ // TODO(developer): Replace these variables before running the sample.
+ String datasetName = "my-dataset-name";
+ updateDatasetExpiration(datasetName);
+ }
+
+ public static void updateDatasetExpiration(String datasetName) {
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests.
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+
+ Dataset dataset = bigquery.getDataset(datasetName);
+
+ // Update dataset expiration to one day
+ Long newExpiration = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
+ try {
+ bigquery.update(dataset.toBuilder().setDefaultTableLifetime(newExpiration).build());
+ System.out.println("Dataset description updated successfully to " + newExpiration);
+ } catch (BigQueryException e) {
+ System.out.println("Dataset expiration was not updated \n" + e.toString());
+ }
+ }
+}
+// [END bigquery_update_dataset_expiration]
diff --git a/samples/src/test/java/com/example/bigquery/AuthSnippetsIT.java b/samples/src/test/java/com/example/bigquery/AuthSnippetsIT.java
new file mode 100644
index 000000000..ec7705580
--- /dev/null
+++ b/samples/src/test/java/com/example/bigquery/AuthSnippetsIT.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2017 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for auth samples. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class AuthSnippetsIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ @Before
+ public void setUp() {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testAuthSnippetsImplicit() throws Exception {
+ AuthSnippets.main(new String[] {"implicit"});
+ String got = bout.toString();
+ assertThat(got).contains("Datasets:");
+ }
+}
diff --git a/samples/src/test/java/com/example/bigquery/CreateDatasetIT.java b/samples/src/test/java/com/example/bigquery/CreateDatasetIT.java
new file mode 100644
index 000000000..32222c603
--- /dev/null
+++ b/samples/src/test/java/com/example/bigquery/CreateDatasetIT.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CreateDatasetIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ @Before
+ public void setUp() {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testCreateDataset() {
+ String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();
+ CreateDataset.createDataset(generatedDatasetName);
+ assertThat(bout.toString()).contains(generatedDatasetName + " created successfully");
+ }
+}
diff --git a/samples/src/test/java/com/example/bigquery/DeleteDatasetIT.java b/samples/src/test/java/com/example/bigquery/DeleteDatasetIT.java
new file mode 100644
index 000000000..ebd8103b9
--- /dev/null
+++ b/samples/src/test/java/com/example/bigquery/DeleteDatasetIT.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.DatasetId;
+import com.google.cloud.bigquery.DatasetInfo;
+import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class DeleteDatasetIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ @Before
+ public void setUp() {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void deleteDataset() {
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+
+ // create the dataset to be deleted
+ String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();
+ DatasetInfo datasetInfo = DatasetInfo.newBuilder(generatedDatasetName).build();
+ bigquery.create(datasetInfo);
+
+ // delete the dataset that was just created
+ DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), generatedDatasetName);
+ DeleteDataset.deleteDataset(datasetId.getProject(), generatedDatasetName);
+
+ assertThat(bout.toString()).contains("Dataset deleted successfully");
+ }
+}
diff --git a/samples/src/test/java/com/example/bigquery/ListDatasetsIT.java b/samples/src/test/java/com/example/bigquery/ListDatasetsIT.java
new file mode 100644
index 000000000..6150390af
--- /dev/null
+++ b/samples/src/test/java/com/example/bigquery/ListDatasetsIT.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ListDatasetsIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ @Before
+ public void setUp() throws Exception {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void listDatasets() {
+ // List datasets in bigquery-public-data project
+ ListDatasets.listDatasets("bigquery-public-data");
+ assertThat(bout.toString()).contains("dataset in project listed successfully");
+ }
+}
diff --git a/samples/src/test/java/com/example/bigquery/QuickstartSampleIT.java b/samples/src/test/java/com/example/bigquery/QuickstartSampleIT.java
new file mode 100644
index 000000000..669ab0789
--- /dev/null
+++ b/samples/src/test/java/com/example/bigquery/QuickstartSampleIT.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.DatasetId;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for quickstart sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class QuickstartSampleIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ private static final void deleteMyNewDataset() {
+ BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
+ String datasetName = "my_new_dataset";
+ DatasetId datasetId = DatasetId.of(datasetName);
+ DatasetDeleteOption deleteContents = DatasetDeleteOption.deleteContents();
+ bigquery.delete(datasetId, deleteContents);
+ }
+
+ @Before
+ public void setUp() {
+ deleteMyNewDataset();
+
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ deleteMyNewDataset();
+ }
+
+ @Test
+ public void testQuickstart() throws Exception {
+ QuickstartSample.main();
+ String got = bout.toString();
+ assertThat(got).contains("Dataset my_new_dataset created.");
+ }
+}
diff --git a/samples/src/test/java/com/example/bigquery/SimpleAppIT.java b/samples/src/test/java/com/example/bigquery/SimpleAppIT.java
new file mode 100644
index 000000000..ea7873bbc
--- /dev/null
+++ b/samples/src/test/java/com/example/bigquery/SimpleAppIT.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for simple app sample. */
+@RunWith(JUnit4.class)
+@SuppressWarnings("checkstyle:abbreviationaswordinname")
+public class SimpleAppIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ @Before
+ public void setUp() {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testQuickstart() throws Exception {
+ SimpleApp.main();
+ String got = bout.toString();
+ assertThat(got).contains("views:");
+ }
+}
diff --git a/samples/src/test/java/com/example/bigquery/UpdateDatasetAccessIT.java b/samples/src/test/java/com/example/bigquery/UpdateDatasetAccessIT.java
new file mode 100644
index 000000000..ed45f2946
--- /dev/null
+++ b/samples/src/test/java/com/example/bigquery/UpdateDatasetAccessIT.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class UpdateDatasetAccessIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ @Before
+ public void setUp() throws Exception {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void updateDatasetAccess() {
+ String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();
+ // Create a dataset in order to modify its ACL
+ CreateDataset.createDataset(generatedDatasetName);
+
+ // Modify dataset's ACL
+ UpdateDatasetAccess.updateDatasetAccess(generatedDatasetName);
+ assertThat(bout.toString()).contains("Dataset Access Control updated successfully");
+ }
+}
diff --git a/samples/src/test/java/com/example/bigquery/UpdateDatasetDescriptionIT.java b/samples/src/test/java/com/example/bigquery/UpdateDatasetDescriptionIT.java
new file mode 100644
index 000000000..75bb06e8c
--- /dev/null
+++ b/samples/src/test/java/com/example/bigquery/UpdateDatasetDescriptionIT.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class UpdateDatasetDescriptionIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ @Before
+ public void setUp() throws Exception {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void updateDatasetDescription() {
+ String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();
+ String newDescription = "new description!";
+ // Create a dataset in order to modify its description
+ CreateDataset.createDataset(generatedDatasetName);
+
+ // Modify dataset's description
+ UpdateDatasetDescription.updateDatasetDescription(generatedDatasetName, newDescription);
+ assertThat(bout.toString())
+ .contains("Dataset description updated successfully to " + newDescription);
+ }
+}
diff --git a/samples/src/test/java/com/example/bigquery/UpdateDatasetExpirationIT.java b/samples/src/test/java/com/example/bigquery/UpdateDatasetExpirationIT.java
new file mode 100644
index 000000000..bd06863dd
--- /dev/null
+++ b/samples/src/test/java/com/example/bigquery/UpdateDatasetExpirationIT.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2019 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class UpdateDatasetExpirationIT {
+ private ByteArrayOutputStream bout;
+ private PrintStream out;
+
+ @Before
+ public void setUp() throws Exception {
+ bout = new ByteArrayOutputStream();
+ out = new PrintStream(bout);
+ System.setOut(out);
+ }
+
+ @After
+ public void tearDown() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void updateDatasetExpiration() {
+ String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();
+ // Create a dataset in order to modify its expiration
+ CreateDataset.createDataset(generatedDatasetName);
+
+ // Modify dataset's expiration
+ UpdateDatasetExpiration.updateDatasetExpiration(generatedDatasetName);
+ assertThat(bout.toString()).contains("Dataset description updated successfully");
+ }
+}