From 3973c4d3896cc3af936a2d1df6b732174dbd8235 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Thu, 3 May 2018 15:12:49 -0700 Subject: [PATCH 1/2] BigQuery: Add "managing datasets" snippets. Add update ACLs sample. Add update dataset expiration sample. --- .../google/cloud/bigquery/DatasetInfo.java | 26 ++++++ .../snippets/DatasetInfoSnippets.java | 81 +++++++++++++++++++ .../snippets/ITDatasetInfoSnippets.java | 79 ++++++++++++++++++ utilities/add_snippets_to_file.py | 3 + 4 files changed, 189 insertions(+) create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetInfoSnippets.java create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetInfoSnippets.java diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java index 6ff92fdd2a51..702b8360b251 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java @@ -318,6 +318,20 @@ public DatasetId getDatasetId() { /** * Returns the dataset's access control configuration. * + *

Update the ACLs for a dataset. + *

 {@code
+   * Dataset dataset = bigquery.getDataset(DatasetId.of("my_dataset"));
+   * List beforeAcls = dataset.getAcl();
+   * 
+   * // Make a copy of the ACLs so that they can be modified.
+   * ArrayList acls = new ArrayList<>(beforeAcls);
+   * acls.add(Acl.of(new Acl.User("sample.bigquery.dev@gmail.com"), Acl.Role.READER));
+   * Dataset.Builder builder = dataset.toBuilder();
+   * builder.setAcl(acls);
+   * 
+   * bigquery.update(builder.build());  // API request.
+   * }
+ * * @see Access Control */ public List getAcl() { @@ -341,6 +355,18 @@ public Long getCreationTime() { * will be deleted automatically. If a table's expirationTime is modified or removed before the * table expires, or if you provide an explicit expirationTime when creating a table, that value * takes precedence over the default expiration time indicated by this property. + * + *

Update the default table expiration time for a dataset. + *

 {@code
+   * Dataset dataset = bigquery.getDataset(DatasetId.of("my_dataset"));
+   * Long beforeExpiration = dataset.getDefaultTableLifetime();
+   * 
+   * Long oneDayMilliseconds = 24 * 60 * 60 * 1000L;
+   * Dataset.Builder builder = dataset.toBuilder();
+   * builder.setDefaultTableLifetime(oneDayMilliseconds);
+   * bigquery.update(builder.build());  // API request.
+   * }
+ * */ public Long getDefaultTableLifetime() { return defaultTableLifetime; diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetInfoSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetInfoSnippets.java new file mode 100644 index 000000000000..120f51bbf9c3 --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetInfoSnippets.java @@ -0,0 +1,81 @@ +/* + * Copyright 2018 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. + */ + +/* + * EDITING INSTRUCTIONS + * This file is referenced in DatasetInfo’s javadoc. Any change to this file should be reflected in + * DatasetInfo’s javadoc. Use utilities/add_snipptets_to_file.py to copy. + */ + +package com.google.cloud.examples.bigquery.snippets; + +import com.google.api.gax.paging.Page; +import com.google.cloud.bigquery.*; +import com.google.cloud.bigquery.DatasetInfo.Builder; + +import java.util.ArrayList; +import java.util.List; + +/** + * This class contains a number of snippets for the {@link DatasetInfo} interface. + */ +public class DatasetInfoSnippets { + + private final BigQuery bigquery; + + public DatasetInfoSnippets(BigQuery bigquery) { + this.bigquery = bigquery; + } + + /** + * Update the ACLs for a dataset. + */ + // [TARGET getAcl()] + // [VARIABLE bigquery.getDataset(DatasetId.of("my_dataset"))] + public List updateDatasetAccess(Dataset dataset) { + // [START bigquery_update_dataset_access] + List beforeAcls = dataset.getAcl(); + + // Make a copy of the ACLs so that they can be modified. + ArrayList acls = new ArrayList<>(beforeAcls); + acls.add(Acl.of(new Acl.User("sample.bigquery.dev@gmail.com"), Acl.Role.READER)); + Dataset.Builder builder = dataset.toBuilder(); + builder.setAcl(acls); + + bigquery.update(builder.build()); // API request. + // [END bigquery_update_dataset_access] + + return beforeAcls; + } + + /** + * Update the default table expiration time for a dataset. + */ + // [TARGET getDefaultTableLifetime()] + // [VARIABLE bigquery.getDataset(DatasetId.of("my_dataset"))] + public Long updateDatasetExpiration(Dataset dataset) { + // [START bigquery_update_dataset_expiration] + Long beforeExpiration = dataset.getDefaultTableLifetime(); + + Long oneDayMilliseconds = 24 * 60 * 60 * 1000L; + Dataset.Builder builder = dataset.toBuilder(); + builder.setDefaultTableLifetime(oneDayMilliseconds); + bigquery.update(builder.build()); // API request. + // [END bigquery_update_dataset_expiration] + + return beforeExpiration; + } +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetInfoSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetInfoSnippets.java new file mode 100644 index 000000000000..b8468b0adfb4 --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetInfoSnippets.java @@ -0,0 +1,79 @@ +/* + * Copyright 2018 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.google.cloud.examples.bigquery.snippets; + +import com.google.cloud.bigquery.*; +import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.List; +import java.util.concurrent.ExecutionException; + +import static org.junit.Assert.*; + +public class ITDatasetInfoSnippets { + + private static final String DATASET = RemoteBigQueryHelper.generateDatasetName(); + + private static BigQuery bigquery; + private static DatasetInfoSnippets datasetInfoSnippets; + private static ByteArrayOutputStream bout; + private static PrintStream out; + + @Rule public Timeout globalTimeout = Timeout.seconds(300); + + @BeforeClass + public static void beforeClass() { + bigquery = RemoteBigQueryHelper.create().getOptions().getService(); + datasetInfoSnippets = new DatasetInfoSnippets(bigquery); + bigquery.create(DatasetInfo.newBuilder(DATASET).build()); + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @AfterClass + public static void afterClass() throws ExecutionException, InterruptedException { + bigquery.delete(DATASET, DatasetDeleteOption.deleteContents()); + System.setOut(null); + } + + @Test + public void testUpdateDatasetAccess() throws InterruptedException { + Dataset dataset = bigquery.getDataset(DATASET); + List beforeAcls = datasetInfoSnippets.updateDatasetAccess(dataset); + dataset = bigquery.getDataset(DATASET); + List afterAcls = dataset.getAcl(); + assertEquals(beforeAcls.size() + 1, afterAcls.size()); + } + + @Test + public void testUpdateDatasetExpiration() throws InterruptedException { + Dataset dataset = bigquery.getDataset(DATASET); + Long beforeExpiration = datasetInfoSnippets.updateDatasetExpiration(dataset); + dataset = bigquery.getDataset(DATASET); + Long afterExpiration = dataset.getDefaultTableLifetime(); + assertNotEquals(beforeExpiration, afterExpiration); + } +} diff --git a/utilities/add_snippets_to_file.py b/utilities/add_snippets_to_file.py index 5044eb1ea265..43d5c424715a 100644 --- a/utilities/add_snippets_to_file.py +++ b/utilities/add_snippets_to_file.py @@ -330,6 +330,9 @@ def __init__(self, data, methods): for match in pattern.finditer(data): line_number = len(NEWLINE_PATTERN.findall(data[:match.start()])) + 1 signature = method.signature + if signature in self.line_numbers: + LOGGER.warning('Method %s duplicate at line %d.', signature, line_number) + continue LOGGER.info('Method %s found at line %d.', signature, line_number) self.line_numbers[signature] = line_number From 1985805b51f02c4b72696db0165f4ede10dfdbfc Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Thu, 3 May 2018 15:32:12 -0700 Subject: [PATCH 2/2] Remove unused imports. --- .../bigquery/snippets/DatasetInfoSnippets.java | 14 +++++++------- .../bigquery/snippets/ITDatasetInfoSnippets.java | 5 ++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetInfoSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetInfoSnippets.java index 120f51bbf9c3..614ac564cf3f 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetInfoSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/DatasetInfoSnippets.java @@ -22,9 +22,9 @@ package com.google.cloud.examples.bigquery.snippets; -import com.google.api.gax.paging.Page; -import com.google.cloud.bigquery.*; -import com.google.cloud.bigquery.DatasetInfo.Builder; +import com.google.cloud.bigquery.Acl; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.DatasetInfo; import java.util.ArrayList; import java.util.List; @@ -45,14 +45,14 @@ public DatasetInfoSnippets(BigQuery bigquery) { */ // [TARGET getAcl()] // [VARIABLE bigquery.getDataset(DatasetId.of("my_dataset"))] - public List updateDatasetAccess(Dataset dataset) { + public List updateDatasetAccess(DatasetInfo dataset) { // [START bigquery_update_dataset_access] List beforeAcls = dataset.getAcl(); // Make a copy of the ACLs so that they can be modified. ArrayList acls = new ArrayList<>(beforeAcls); acls.add(Acl.of(new Acl.User("sample.bigquery.dev@gmail.com"), Acl.Role.READER)); - Dataset.Builder builder = dataset.toBuilder(); + DatasetInfo.Builder builder = dataset.toBuilder(); builder.setAcl(acls); bigquery.update(builder.build()); // API request. @@ -66,12 +66,12 @@ public List updateDatasetAccess(Dataset dataset) { */ // [TARGET getDefaultTableLifetime()] // [VARIABLE bigquery.getDataset(DatasetId.of("my_dataset"))] - public Long updateDatasetExpiration(Dataset dataset) { + public Long updateDatasetExpiration(DatasetInfo dataset) { // [START bigquery_update_dataset_expiration] Long beforeExpiration = dataset.getDefaultTableLifetime(); Long oneDayMilliseconds = 24 * 60 * 60 * 1000L; - Dataset.Builder builder = dataset.toBuilder(); + DatasetInfo.Builder builder = dataset.toBuilder(); builder.setDefaultTableLifetime(oneDayMilliseconds); bigquery.update(builder.build()); // API request. // [END bigquery_update_dataset_expiration] diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetInfoSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetInfoSnippets.java index b8468b0adfb4..815d07583fd0 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetInfoSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITDatasetInfoSnippets.java @@ -16,8 +16,11 @@ package com.google.cloud.examples.bigquery.snippets; -import com.google.cloud.bigquery.*; +import com.google.cloud.bigquery.Acl; +import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; +import com.google.cloud.bigquery.Dataset; +import com.google.cloud.bigquery.DatasetInfo; import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; import org.junit.AfterClass; import org.junit.BeforeClass;