Skip to content

Commit 3973c4d

Browse files
committed
BigQuery: Add "managing datasets" snippets.
Add update ACLs sample. Add update dataset expiration sample.
1 parent 33a392f commit 3973c4d

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/DatasetInfo.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,20 @@ public DatasetId getDatasetId() {
318318
/**
319319
* Returns the dataset's access control configuration.
320320
*
321+
* <p>Update the ACLs for a dataset.
322+
* <pre> {@code
323+
* Dataset dataset = bigquery.getDataset(DatasetId.of("my_dataset"));
324+
* List<Acl> beforeAcls = dataset.getAcl();
325+
*
326+
* // Make a copy of the ACLs so that they can be modified.
327+
* ArrayList<Acl> acls = new ArrayList<>(beforeAcls);
328+
* acls.add(Acl.of(new Acl.User("sample.bigquery.dev@gmail.com"), Acl.Role.READER));
329+
* Dataset.Builder builder = dataset.toBuilder();
330+
* builder.setAcl(acls);
331+
*
332+
* bigquery.update(builder.build()); // API request.
333+
* }</pre>
334+
*
321335
* @see <a href="https://cloud.google.com/bigquery/access-control">Access Control</a>
322336
*/
323337
public List<Acl> getAcl() {
@@ -341,6 +355,18 @@ public Long getCreationTime() {
341355
* will be deleted automatically. If a table's expirationTime is modified or removed before the
342356
* table expires, or if you provide an explicit expirationTime when creating a table, that value
343357
* takes precedence over the default expiration time indicated by this property.
358+
*
359+
* <p>Update the default table expiration time for a dataset.
360+
* <pre> {@code
361+
* Dataset dataset = bigquery.getDataset(DatasetId.of("my_dataset"));
362+
* Long beforeExpiration = dataset.getDefaultTableLifetime();
363+
*
364+
* Long oneDayMilliseconds = 24 * 60 * 60 * 1000L;
365+
* Dataset.Builder builder = dataset.toBuilder();
366+
* builder.setDefaultTableLifetime(oneDayMilliseconds);
367+
* bigquery.update(builder.build()); // API request.
368+
* }</pre>
369+
*
344370
*/
345371
public Long getDefaultTableLifetime() {
346372
return defaultTableLifetime;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2018 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+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in DatasetInfo’s javadoc. Any change to this file should be reflected in
20+
* DatasetInfo’s javadoc. Use utilities/add_snipptets_to_file.py to copy.
21+
*/
22+
23+
package com.google.cloud.examples.bigquery.snippets;
24+
25+
import com.google.api.gax.paging.Page;
26+
import com.google.cloud.bigquery.*;
27+
import com.google.cloud.bigquery.DatasetInfo.Builder;
28+
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
/**
33+
* This class contains a number of snippets for the {@link DatasetInfo} interface.
34+
*/
35+
public class DatasetInfoSnippets {
36+
37+
private final BigQuery bigquery;
38+
39+
public DatasetInfoSnippets(BigQuery bigquery) {
40+
this.bigquery = bigquery;
41+
}
42+
43+
/**
44+
* Update the ACLs for a dataset.
45+
*/
46+
// [TARGET getAcl()]
47+
// [VARIABLE bigquery.getDataset(DatasetId.of("my_dataset"))]
48+
public List<Acl> updateDatasetAccess(Dataset dataset) {
49+
// [START bigquery_update_dataset_access]
50+
List<Acl> beforeAcls = dataset.getAcl();
51+
52+
// Make a copy of the ACLs so that they can be modified.
53+
ArrayList<Acl> acls = new ArrayList<>(beforeAcls);
54+
acls.add(Acl.of(new Acl.User("sample.bigquery.dev@gmail.com"), Acl.Role.READER));
55+
Dataset.Builder builder = dataset.toBuilder();
56+
builder.setAcl(acls);
57+
58+
bigquery.update(builder.build()); // API request.
59+
// [END bigquery_update_dataset_access]
60+
61+
return beforeAcls;
62+
}
63+
64+
/**
65+
* Update the default table expiration time for a dataset.
66+
*/
67+
// [TARGET getDefaultTableLifetime()]
68+
// [VARIABLE bigquery.getDataset(DatasetId.of("my_dataset"))]
69+
public Long updateDatasetExpiration(Dataset dataset) {
70+
// [START bigquery_update_dataset_expiration]
71+
Long beforeExpiration = dataset.getDefaultTableLifetime();
72+
73+
Long oneDayMilliseconds = 24 * 60 * 60 * 1000L;
74+
Dataset.Builder builder = dataset.toBuilder();
75+
builder.setDefaultTableLifetime(oneDayMilliseconds);
76+
bigquery.update(builder.build()); // API request.
77+
// [END bigquery_update_dataset_expiration]
78+
79+
return beforeExpiration;
80+
}
81+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2018 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.google.cloud.examples.bigquery.snippets;
18+
19+
import com.google.cloud.bigquery.*;
20+
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
21+
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
22+
import org.junit.AfterClass;
23+
import org.junit.BeforeClass;
24+
import org.junit.Rule;
25+
import org.junit.Test;
26+
import org.junit.rules.Timeout;
27+
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.PrintStream;
30+
import java.util.List;
31+
import java.util.concurrent.ExecutionException;
32+
33+
import static org.junit.Assert.*;
34+
35+
public class ITDatasetInfoSnippets {
36+
37+
private static final String DATASET = RemoteBigQueryHelper.generateDatasetName();
38+
39+
private static BigQuery bigquery;
40+
private static DatasetInfoSnippets datasetInfoSnippets;
41+
private static ByteArrayOutputStream bout;
42+
private static PrintStream out;
43+
44+
@Rule public Timeout globalTimeout = Timeout.seconds(300);
45+
46+
@BeforeClass
47+
public static void beforeClass() {
48+
bigquery = RemoteBigQueryHelper.create().getOptions().getService();
49+
datasetInfoSnippets = new DatasetInfoSnippets(bigquery);
50+
bigquery.create(DatasetInfo.newBuilder(DATASET).build());
51+
bout = new ByteArrayOutputStream();
52+
out = new PrintStream(bout);
53+
System.setOut(out);
54+
}
55+
56+
@AfterClass
57+
public static void afterClass() throws ExecutionException, InterruptedException {
58+
bigquery.delete(DATASET, DatasetDeleteOption.deleteContents());
59+
System.setOut(null);
60+
}
61+
62+
@Test
63+
public void testUpdateDatasetAccess() throws InterruptedException {
64+
Dataset dataset = bigquery.getDataset(DATASET);
65+
List<Acl> beforeAcls = datasetInfoSnippets.updateDatasetAccess(dataset);
66+
dataset = bigquery.getDataset(DATASET);
67+
List<Acl> afterAcls = dataset.getAcl();
68+
assertEquals(beforeAcls.size() + 1, afterAcls.size());
69+
}
70+
71+
@Test
72+
public void testUpdateDatasetExpiration() throws InterruptedException {
73+
Dataset dataset = bigquery.getDataset(DATASET);
74+
Long beforeExpiration = datasetInfoSnippets.updateDatasetExpiration(dataset);
75+
dataset = bigquery.getDataset(DATASET);
76+
Long afterExpiration = dataset.getDefaultTableLifetime();
77+
assertNotEquals(beforeExpiration, afterExpiration);
78+
}
79+
}

utilities/add_snippets_to_file.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ def __init__(self, data, methods):
330330
for match in pattern.finditer(data):
331331
line_number = len(NEWLINE_PATTERN.findall(data[:match.start()])) + 1
332332
signature = method.signature
333+
if signature in self.line_numbers:
334+
LOGGER.warning('Method %s duplicate at line %d.', signature, line_number)
335+
continue
333336
LOGGER.info('Method %s found at line %d.', signature, line_number)
334337
self.line_numbers[signature] = line_number
335338

0 commit comments

Comments
 (0)