|
| 1 | +/* |
| 2 | + * Copyright 2020 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 com.google.cloud.bigquery.Acl; |
| 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 | +import com.google.cloud.bigquery.QueryJobConfiguration; |
| 26 | +import com.google.cloud.bigquery.Table; |
| 27 | +import com.google.cloud.bigquery.TableId; |
| 28 | +import com.google.cloud.bigquery.TableInfo; |
| 29 | +import com.google.cloud.bigquery.ViewDefinition; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +// Sample of authorized view tutorial. |
| 34 | +public class AuthorizedViewTutorial { |
| 35 | + |
| 36 | + public static void runAuthorizedViewTutorial() { |
| 37 | + // TODO(developer): Replace these variables before running the sample. |
| 38 | + String projectId = "MY_PROJECT_ID"; |
| 39 | + String sourceDatasetId = "MY_SOURCE_DATASET"; |
| 40 | + String sourceTableId = "MY_SOURCE_TABLE"; |
| 41 | + String sharedDatasetId = "SHARED_VIEWS"; |
| 42 | + String sharedViewId = "MY_VIEW"; |
| 43 | + authorizedViewTutorial( |
| 44 | + projectId, sourceDatasetId, sourceTableId, sharedDatasetId, sharedViewId); |
| 45 | + } |
| 46 | + |
| 47 | + public static void authorizedViewTutorial( |
| 48 | + String projectId, |
| 49 | + String sourceDatasetId, |
| 50 | + String sourceTableId, |
| 51 | + String sharedDatasetId, |
| 52 | + String sharedViewId) { |
| 53 | + try { |
| 54 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 55 | + // once, and can be reused for multiple requests. |
| 56 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 57 | + // [START bigquery_authorized_view_tutorial] |
| 58 | + // [START bigquery_avt_create_source_dataset] |
| 59 | + // Create a source dataset to store your table. |
| 60 | + Dataset sourceDataset = bigquery.create(DatasetInfo.of(sourceDatasetId)); |
| 61 | + // [END bigquery_avt_create_source_dataset] |
| 62 | + |
| 63 | + // [START bigquery_avt_create_source_table] |
| 64 | + // Populate a source table |
| 65 | + String tableQuery = |
| 66 | + "SELECT commit, author, committer, repo_name" |
| 67 | + + " FROM `bigquery-public-data.github_repos.commits`" |
| 68 | + + " LIMIT 1000"; |
| 69 | + QueryJobConfiguration queryConfig = |
| 70 | + QueryJobConfiguration.newBuilder(tableQuery) |
| 71 | + .setDestinationTable(TableId.of(sourceDatasetId, sourceTableId)) |
| 72 | + .build(); |
| 73 | + bigquery.query(queryConfig); |
| 74 | + // [END bigquery_avt_create_source_table] |
| 75 | + |
| 76 | + // [START bigquery_avt_create_shared_dataset] |
| 77 | + // Create a separate dataset to store your view |
| 78 | + Dataset sharedDataset = bigquery.create(DatasetInfo.of(sharedDatasetId)); |
| 79 | + // [END bigquery_avt_create_shared_dataset] |
| 80 | + |
| 81 | + // [START bigquery_avt_create_view] |
| 82 | + // Create the view in the new dataset |
| 83 | + String viewQuery = |
| 84 | + String.format( |
| 85 | + "SELECT commit, author.name as author, committer.name as committer, repo_name FROM %s.%s.%s", |
| 86 | + projectId, sourceDatasetId, sourceTableId); |
| 87 | + |
| 88 | + ViewDefinition viewDefinition = ViewDefinition.of(viewQuery); |
| 89 | + |
| 90 | + Table view = |
| 91 | + bigquery.create(TableInfo.of(TableId.of(sharedDatasetId, sharedViewId), viewDefinition)); |
| 92 | + // [END bigquery_avt_create_view] |
| 93 | + |
| 94 | + // [START bigquery_avt_shared_dataset_access] |
| 95 | + // Assign access controls to the dataset containing the view |
| 96 | + List<Acl> viewAcl = new ArrayList<>(sharedDataset.getAcl()); |
| 97 | + viewAcl. add( Acl. of( new Acl. Group( "[email protected]"), Acl. Role. READER)); |
| 98 | + sharedDataset.toBuilder().setAcl(viewAcl).build().update(); |
| 99 | + // [END bigquery_avt_shared_dataset_access] |
| 100 | + |
| 101 | + // [START bigquery_avt_source_dataset_access] |
| 102 | + // Authorize the view to access the source dataset |
| 103 | + List<Acl> srcAcl = new ArrayList<>(sourceDataset.getAcl()); |
| 104 | + srcAcl.add(Acl.of(new Acl.View(view.getTableId()))); |
| 105 | + sourceDataset.toBuilder().setAcl(srcAcl).build().update(); |
| 106 | + // [END bigquery_avt_source_dataset_access] |
| 107 | + // [END bigquery_authorized_view_tutorial] |
| 108 | + System.out.println("Authorized view tutorial successfully"); |
| 109 | + } catch (BigQueryException | InterruptedException e) { |
| 110 | + System.out.println("Authorized view tutorial was not success. \n" + e.toString()); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments