Skip to content

Commit afb943e

Browse files
bq_update_dataset_access (GoogleCloudPlatform#1768)
* bq_update_dataset_access * update sample based on comments
1 parent af9820b commit afb943e

File tree

9 files changed

+131
-16
lines changed

9 files changed

+131
-16
lines changed

bigquery/cloud-client/src/main/java/com/example/bigquery/AuthSnippets.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void explicit() throws IOException {
5353
// environment variable, you can explicitly load the credentials file to construct the
5454
// credentials.
5555
GoogleCredentials credentials;
56-
File credentialsPath = new File("service_account.json"); // TODO: update to your key path.
56+
File credentialsPath = new File("service_account.json"); // TODO: update to your key path.
5757
try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
5858
credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
5959
}

bigquery/cloud-client/src/main/java/com/example/bigquery/CreateDataset.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.cloud.bigquery.BigQueryOptions;
2323
import com.google.cloud.bigquery.Dataset;
2424
import com.google.cloud.bigquery.DatasetInfo;
25-
import java.io.IOException;
2625

2726
public class CreateDataset {
2827

@@ -47,4 +46,4 @@ public static void createDataset(String datasetName) {
4746
}
4847
}
4948
}
50-
// [END bigquery create_dataset]
49+
// [END bigquery create_dataset]

bigquery/cloud-client/src/main/java/com/example/bigquery/DeleteDataset.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ public static void deleteDataset(String projectId, String datasetName) {
4545
}
4646
}
4747
}
48-
// [END bigquery_delete_dataset]
48+
// [END bigquery_delete_dataset]

bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import com.google.cloud.bigquery.JobId;
2727
import com.google.cloud.bigquery.JobInfo;
2828
import com.google.cloud.bigquery.QueryJobConfiguration;
29-
import com.google.cloud.bigquery.QueryResponse;
3029
import com.google.cloud.bigquery.TableResult;
3130
import java.util.UUID;
31+
3232
// [END bigquery_simple_app_deps]
3333

3434
public class SimpleApp {
@@ -39,12 +39,12 @@ public static void main(String... args) throws Exception {
3939
// [START bigquery_simple_app_query]
4040
QueryJobConfiguration queryConfig =
4141
QueryJobConfiguration.newBuilder(
42-
"SELECT "
43-
+ "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, "
44-
+ "view_count "
45-
+ "FROM `bigquery-public-data.stackoverflow.posts_questions` "
46-
+ "WHERE tags like '%google-bigquery%' "
47-
+ "ORDER BY favorite_count DESC LIMIT 10")
42+
"SELECT "
43+
+ "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, "
44+
+ "view_count "
45+
+ "FROM `bigquery-public-data.stackoverflow.posts_questions` "
46+
+ "WHERE tags like '%google-bigquery%' "
47+
+ "ORDER BY favorite_count DESC LIMIT 10")
4848
// Use standard SQL syntax for queries.
4949
// See: https://cloud.google.com/bigquery/sql-reference/
5050
.setUseLegacySql(false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_update_dataset_access]
20+
import com.google.cloud.bigquery.Acl;
21+
import com.google.cloud.bigquery.Acl.Role;
22+
import com.google.cloud.bigquery.Acl.User;
23+
import com.google.cloud.bigquery.BigQuery;
24+
import com.google.cloud.bigquery.BigQueryException;
25+
import com.google.cloud.bigquery.BigQueryOptions;
26+
import com.google.cloud.bigquery.Dataset;
27+
import java.util.ArrayList;
28+
29+
public class UpdateDatasetAccess {
30+
31+
public static void runUpdateDatasetAccess() {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String datasetName = "my-dataset-name";
34+
updateDatasetAccess(datasetName);
35+
}
36+
37+
public static void updateDatasetAccess(String datasetName) {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests.
40+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
41+
42+
Dataset dataset = bigquery.getDataset(datasetName);
43+
44+
// Create a new ACL granting the READER role to "sample.bigquery.dev@gmail.com"
45+
// For more information on the types of ACLs available see:
46+
// https://cloud.google.com/storage/docs/access-control/lists
47+
Acl newEntry = Acl.of(new User("sample.bigquery.dev@gmail.com"), Role.READER);
48+
49+
// Get a copy of the ACLs list from the dataset and append the new entry
50+
ArrayList<Acl> acls = new ArrayList<>(dataset.getAcl());
51+
acls.add(newEntry);
52+
53+
//Update the dataset to use the new ACLs
54+
try {
55+
bigquery.update(dataset.toBuilder().setAcl(acls).build());
56+
System.out.println("Dataset Access Control updated successfully");
57+
} catch (BigQueryException e) {
58+
System.out.println("Dataset Access control was not updated \n" + e.toString());
59+
}
60+
}
61+
}
62+
// [END bigquery_update_dataset_access]

bigquery/cloud-client/src/test/java/com/example/bigquery/AuthSnippetsIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void tearDown() {
4747

4848
@Test
4949
public void testAuthSnippetsImplicit() throws Exception {
50-
AuthSnippets.main(new String[]{"implicit"});
50+
AuthSnippets.main(new String[] {"implicit"});
5151
String got = bout.toString();
5252
assertThat(got).contains("Datasets:");
5353
}

bigquery/cloud-client/src/test/java/com/example/bigquery/CreateDatasetIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ public void testCreateDataset() {
4747
CreateDataset.createDataset(generatedDatasetName);
4848
assertThat(bout.toString()).contains(generatedDatasetName + " created successfully");
4949
}
50-
}
50+
}

bigquery/cloud-client/src/test/java/com/example/bigquery/DeleteDatasetIT.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ public void tearDown() {
4949
public void deleteDataset() {
5050
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
5151

52-
//create the dataset to be deleted
52+
// create the dataset to be deleted
5353
String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();
5454
DatasetInfo datasetInfo = DatasetInfo.newBuilder(generatedDatasetName).build();
5555
bigquery.create(datasetInfo);
5656

57-
//delete the dataset that was just created
57+
// delete the dataset that was just created
5858
DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), generatedDatasetName);
5959
DeleteDataset.deleteDataset(datasetId.getProject(), generatedDatasetName);
6060

6161
assertThat(bout.toString()).contains("Dataset deleted successfully");
6262
}
63-
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class UpdateDatasetAccessIT {
29+
private ByteArrayOutputStream bout;
30+
private PrintStream out;
31+
32+
@Before
33+
public void setUp() throws Exception {
34+
bout = new ByteArrayOutputStream();
35+
out = new PrintStream(bout);
36+
System.setOut(out);
37+
}
38+
39+
@After
40+
public void tearDown() {
41+
System.setOut(null);
42+
}
43+
44+
@Test
45+
public void updateDatasetAccess() {
46+
String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();
47+
// Create a dataset in order to modify its ACL
48+
CreateDataset.createDataset(generatedDatasetName);
49+
50+
// Modify dataset's ACL
51+
UpdateDatasetAccess.updateDatasetAccess(generatedDatasetName);
52+
assertThat(bout.toString()).contains("Dataset Access Control updated successfully");
53+
}
54+
}

0 commit comments

Comments
 (0)