|
| 1 | +/* |
| 2 | + * Copyright 2024 Google Inc. |
| 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.bigtable; |
| 18 | + |
| 19 | +import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS; |
| 20 | + |
| 21 | +import com.google.api.gax.rpc.NotFoundException; |
| 22 | +import com.google.api.gax.rpc.PermissionDeniedException; |
| 23 | +import com.google.api.gax.rpc.ServerStream; |
| 24 | +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; |
| 25 | +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; |
| 26 | +import com.google.cloud.bigtable.admin.v2.models.AuthorizedView; |
| 27 | +import com.google.cloud.bigtable.admin.v2.models.CreateAuthorizedViewRequest; |
| 28 | +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; |
| 29 | +import com.google.cloud.bigtable.admin.v2.models.FamilySubsets; |
| 30 | +import com.google.cloud.bigtable.admin.v2.models.SubsetView; |
| 31 | +import com.google.cloud.bigtable.admin.v2.models.Table; |
| 32 | +import com.google.cloud.bigtable.admin.v2.models.UpdateAuthorizedViewRequest; |
| 33 | +import com.google.cloud.bigtable.data.v2.BigtableDataClient; |
| 34 | +import com.google.cloud.bigtable.data.v2.BigtableDataSettings; |
| 35 | +import com.google.cloud.bigtable.data.v2.models.AuthorizedViewId; |
| 36 | +import com.google.cloud.bigtable.data.v2.models.Filters.Filter; |
| 37 | +import com.google.cloud.bigtable.data.v2.models.Query; |
| 38 | +import com.google.cloud.bigtable.data.v2.models.Row; |
| 39 | +import com.google.cloud.bigtable.data.v2.models.RowCell; |
| 40 | +import com.google.cloud.bigtable.data.v2.models.RowMutation; |
| 41 | +import com.google.protobuf.ByteString; |
| 42 | +import java.io.IOException; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | + |
| 47 | +public class AuthorizedViewExample { |
| 48 | + |
| 49 | + private static final String COLUMN_FAMILY = "cf"; |
| 50 | + private static final String COLUMN_QUALIFIER_GREETING = "greeting"; |
| 51 | + private static final String COLUMN_QUALIFIER_NAME = "name"; |
| 52 | + private static final String ROW_KEY_PREFIX = "rowKey"; |
| 53 | + private final String tableId; |
| 54 | + private final String authorizedViewId; |
| 55 | + private final BigtableTableAdminClient adminClient; |
| 56 | + private final BigtableDataClient dataClient; |
| 57 | + |
| 58 | + public static void main(String[] args) throws IOException { |
| 59 | + |
| 60 | + if (args.length != 2) { |
| 61 | + System.out.println("Missing required project id or instance id"); |
| 62 | + return; |
| 63 | + } |
| 64 | + String projectId = args[0]; |
| 65 | + String instanceId = args[1]; |
| 66 | + |
| 67 | + AuthorizedViewExample authorizedViewExample = |
| 68 | + new AuthorizedViewExample(projectId, instanceId, "test-table", "test-authorized-view"); |
| 69 | + authorizedViewExample.run(); |
| 70 | + } |
| 71 | + |
| 72 | + public AuthorizedViewExample( |
| 73 | + String projectId, String instanceId, String tableId, String authorizedViewId) |
| 74 | + throws IOException { |
| 75 | + this.tableId = tableId; |
| 76 | + this.authorizedViewId = authorizedViewId; |
| 77 | + |
| 78 | + // Creates the settings to configure a bigtable data client. |
| 79 | + BigtableDataSettings settings = |
| 80 | + BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build(); |
| 81 | + |
| 82 | + // Creates a bigtable data client. |
| 83 | + dataClient = BigtableDataClient.create(settings); |
| 84 | + |
| 85 | + // Creates the settings to configure a bigtable table admin client. |
| 86 | + BigtableTableAdminSettings adminSettings = |
| 87 | + BigtableTableAdminSettings.newBuilder() |
| 88 | + .setProjectId(projectId) |
| 89 | + .setInstanceId(instanceId) |
| 90 | + .build(); |
| 91 | + |
| 92 | + // Creates a bigtable table admin client. |
| 93 | + adminClient = BigtableTableAdminClient.create(adminSettings); |
| 94 | + } |
| 95 | + |
| 96 | + public void close() { |
| 97 | + dataClient.close(); |
| 98 | + adminClient.close(); |
| 99 | + } |
| 100 | + |
| 101 | + public void run() { |
| 102 | + createTable(); |
| 103 | + createAuthorizedView(); |
| 104 | + updateAuthorizedView(); |
| 105 | + getAuthorizedView(); |
| 106 | + listAllAuthorizedViews(); |
| 107 | + writeToAuthorizedView(); |
| 108 | + readSingleRowFromAuthorizedView(); |
| 109 | + readRowsWithFilterFromAuthorizedView(); |
| 110 | + deleteAuthorizedView(); |
| 111 | + deleteTable(); |
| 112 | + close(); |
| 113 | + } |
| 114 | + |
| 115 | + public void createTable() { |
| 116 | + // Checks if table exists, creates table if it does not exist. |
| 117 | + if (!adminClient.exists(tableId)) { |
| 118 | + System.out.println("Table does not exist, creating table: " + tableId); |
| 119 | + CreateTableRequest createTableRequest = |
| 120 | + CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY); |
| 121 | + Table table = adminClient.createTable(createTableRequest); |
| 122 | + System.out.printf("Table: %s created successfully%n", table.getId()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public void deleteTable() { |
| 127 | + // Deletes the entire table. |
| 128 | + System.out.println("\nDelete table: " + tableId); |
| 129 | + try { |
| 130 | + adminClient.deleteTable(tableId); |
| 131 | + System.out.printf("Table: %s deleted successfully%n", tableId); |
| 132 | + } catch (NotFoundException e) { |
| 133 | + System.err.println("Failed to delete a non-existent table: " + e.getMessage()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Demonstrates how to create an authorized view under a table with the specified configuration. |
| 139 | + */ |
| 140 | + public void createAuthorizedView() { |
| 141 | + // Checks if the authorized view exists, creates it if it does not exist. |
| 142 | + try { |
| 143 | + adminClient.getAuthorizedView(tableId, authorizedViewId); |
| 144 | + } catch (NotFoundException exception) { |
| 145 | + System.out.printf("%nCreating authorized view %s in table %s%n", authorizedViewId, tableId); |
| 146 | + // [START bigtable_create_authorized_view] |
| 147 | + try { |
| 148 | + CreateAuthorizedViewRequest request = |
| 149 | + CreateAuthorizedViewRequest.of(tableId, authorizedViewId) |
| 150 | + .setAuthorizedViewType( |
| 151 | + SubsetView.create() |
| 152 | + .addRowPrefix("") |
| 153 | + .setFamilySubsets( |
| 154 | + COLUMN_FAMILY, |
| 155 | + FamilySubsets.create().addQualifierPrefix(COLUMN_QUALIFIER_NAME))); |
| 156 | + AuthorizedView authorizedView = adminClient.createAuthorizedView(request); |
| 157 | + System.out.printf("AuthorizedView: %s created successfully%n", authorizedView.getId()); |
| 158 | + } catch (NotFoundException e) { |
| 159 | + System.err.println( |
| 160 | + "Failed to create an authorized view from a non-existent table: " + e.getMessage()); |
| 161 | + } |
| 162 | + // [END bigtable_create_authorized_view] |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /** Demonstrates how to modify an authorized view. */ |
| 167 | + public void updateAuthorizedView() { |
| 168 | + System.out.printf("%nUpdating authorized view %s in table %s%n", authorizedViewId, tableId); |
| 169 | + // [START bigtable_update_authorized_view] |
| 170 | + try { |
| 171 | + // Update to an authorized view permitting everything. |
| 172 | + UpdateAuthorizedViewRequest request = |
| 173 | + UpdateAuthorizedViewRequest.of(tableId, authorizedViewId) |
| 174 | + .setAuthorizedViewType( |
| 175 | + SubsetView.create() |
| 176 | + .addRowPrefix("") |
| 177 | + .setFamilySubsets( |
| 178 | + COLUMN_FAMILY, FamilySubsets.create().addQualifierPrefix(""))); |
| 179 | + AuthorizedView authorizedView = adminClient.updateAuthorizedView(request); |
| 180 | + System.out.printf("AuthorizedView: %s updated successfully%n", authorizedView.getId()); |
| 181 | + } catch (NotFoundException e) { |
| 182 | + System.err.println("Failed to modify a non-existent authorized view: " + e.getMessage()); |
| 183 | + } |
| 184 | + // [END bigtable_update_authorized_view] |
| 185 | + } |
| 186 | + |
| 187 | + /** Demonstrates how to get an authorized view's metadata. */ |
| 188 | + public AuthorizedView getAuthorizedView() { |
| 189 | + System.out.printf("%nGetting authorized view %s in table %s%n", authorizedViewId, tableId); |
| 190 | + // [START bigtable_get_authorized_view] |
| 191 | + AuthorizedView authorizedView = null; |
| 192 | + try { |
| 193 | + authorizedView = adminClient.getAuthorizedView(tableId, authorizedViewId); |
| 194 | + SubsetView subsetView = (SubsetView) authorizedView.getAuthorizedViewType(); |
| 195 | + |
| 196 | + for (ByteString rowPrefix : subsetView.getRowPrefixes()) { |
| 197 | + System.out.printf("Row Prefix: %s%n", rowPrefix.toStringUtf8()); |
| 198 | + } |
| 199 | + for (Map.Entry<String, FamilySubsets> entry : subsetView.getFamilySubsets().entrySet()) { |
| 200 | + for (ByteString qualifierPrefix : entry.getValue().getQualifierPrefixes()) { |
| 201 | + System.out.printf( |
| 202 | + "Column Family: %s, Qualifier Prefix: %s%n", |
| 203 | + entry.getKey(), qualifierPrefix.toStringUtf8()); |
| 204 | + } |
| 205 | + for (ByteString qualifier : entry.getValue().getQualifiers()) { |
| 206 | + System.out.printf( |
| 207 | + "Column Family: %s, Qualifier: %s%n", entry.getKey(), qualifier.toStringUtf8()); |
| 208 | + } |
| 209 | + } |
| 210 | + } catch (NotFoundException e) { |
| 211 | + System.err.println( |
| 212 | + "Failed to retrieve metadata from a non-existent authorized view: " + e.getMessage()); |
| 213 | + } |
| 214 | + // [END bigtable_get_authorized_view] |
| 215 | + return authorizedView; |
| 216 | + } |
| 217 | + |
| 218 | + /** Demonstrates how to list all authorized views within a table. */ |
| 219 | + public List<String> listAllAuthorizedViews() { |
| 220 | + System.out.printf("%nListing authorized views in table %s%n", tableId); |
| 221 | + // [START bigtable_list_authorized_views] |
| 222 | + List<String> authorizedViewIds = new ArrayList<>(); |
| 223 | + try { |
| 224 | + authorizedViewIds = adminClient.listAuthorizedViews(tableId); |
| 225 | + for (String authorizedViewId : authorizedViewIds) { |
| 226 | + System.out.println(authorizedViewId); |
| 227 | + } |
| 228 | + } catch (NotFoundException e) { |
| 229 | + System.err.println( |
| 230 | + "Failed to list authorized views from a non-existent table: " + e.getMessage()); |
| 231 | + } |
| 232 | + // [END bigtable_list_authorized_views] |
| 233 | + return authorizedViewIds; |
| 234 | + } |
| 235 | + |
| 236 | + /** Demonstrates how to delete an authorized view. */ |
| 237 | + public void deleteAuthorizedView() { |
| 238 | + System.out.printf("%nDeleting authorized view %s in table %s%n", authorizedViewId, tableId); |
| 239 | + // [START bigtable_delete_authorized_view] |
| 240 | + try { |
| 241 | + adminClient.deleteAuthorizedView(tableId, authorizedViewId); |
| 242 | + System.out.printf("AuthorizedView: %s deleted successfully%n", authorizedViewId); |
| 243 | + } catch (NotFoundException e) { |
| 244 | + System.err.println("Failed to delete a non-existent authorized view: " + e.getMessage()); |
| 245 | + } |
| 246 | + // [END bigtable_delete_authorized_view] |
| 247 | + } |
| 248 | + |
| 249 | + /** Demonstrates how to write some rows to an authorized view. */ |
| 250 | + public void writeToAuthorizedView() { |
| 251 | + // [START bigtable_authorized_view_write_rows] |
| 252 | + try { |
| 253 | + System.out.println("\nWriting to authorized view"); |
| 254 | + String[] names = {"World", "Bigtable", "Java"}; |
| 255 | + for (int i = 0; i < names.length; i++) { |
| 256 | + String greeting = "Hello " + names[i] + "!"; |
| 257 | + RowMutation rowMutation = |
| 258 | + RowMutation.create(AuthorizedViewId.of(tableId, authorizedViewId), ROW_KEY_PREFIX + i) |
| 259 | + .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, names[i]) |
| 260 | + .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting); |
| 261 | + dataClient.mutateRow(rowMutation); |
| 262 | + System.out.println(greeting); |
| 263 | + } |
| 264 | + } catch (Exception e) { |
| 265 | + if (e instanceof NotFoundException) { |
| 266 | + System.err.println("Failed to write to non-existent authorized view: " + e.getMessage()); |
| 267 | + } else if (e instanceof PermissionDeniedException) { |
| 268 | + System.err.println( |
| 269 | + "Failed to apply mutations outside of the authorized view: " + e.getMessage()); |
| 270 | + } |
| 271 | + } |
| 272 | + // [END bigtable_authorized_view_write_rows] |
| 273 | + } |
| 274 | + |
| 275 | + /** Demonstrates how to read a single row from an authorized view. */ |
| 276 | + public Row readSingleRowFromAuthorizedView() { |
| 277 | + // [START bigtable_authorized_view_get_by_key] |
| 278 | + try { |
| 279 | + System.out.println("\nReading a single row by row key from an authorized view"); |
| 280 | + Row row = |
| 281 | + dataClient.readRow(AuthorizedViewId.of(tableId, authorizedViewId), ROW_KEY_PREFIX + 0); |
| 282 | + System.out.println("Row: " + row.getKey().toStringUtf8()); |
| 283 | + for (RowCell cell : row.getCells()) { |
| 284 | + System.out.printf( |
| 285 | + "Family: %s Qualifier: %s Value: %s%n", |
| 286 | + cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); |
| 287 | + } |
| 288 | + return row; |
| 289 | + } catch (NotFoundException e) { |
| 290 | + System.err.println("Failed to read from a non-existent authorized view: " + e.getMessage()); |
| 291 | + return null; |
| 292 | + } |
| 293 | + // [END bigtable_authorized_view_get_by_key] |
| 294 | + } |
| 295 | + |
| 296 | + /** Demonstrates how to read rows from an authorized view with a filter. */ |
| 297 | + public List<Row> readRowsWithFilterFromAuthorizedView() { |
| 298 | + // [START bigtable_authorized_view_scan_with_filter] |
| 299 | + try { |
| 300 | + // A filter that matches only the most recent cell within each column |
| 301 | + Filter filter = FILTERS.limit().cellsPerColumn(1); |
| 302 | + System.out.println("\nScanning authorized view with filter"); |
| 303 | + Query query = Query.create(AuthorizedViewId.of(tableId, authorizedViewId)).filter(filter); |
| 304 | + ServerStream<Row> rowStream = dataClient.readRows(query); |
| 305 | + List<Row> authorizedViewRows = new ArrayList<>(); |
| 306 | + for (Row r : rowStream) { |
| 307 | + System.out.println("Row Key: " + r.getKey().toStringUtf8()); |
| 308 | + authorizedViewRows.add(r); |
| 309 | + for (RowCell cell : r.getCells()) { |
| 310 | + System.out.printf( |
| 311 | + "Family: %s Qualifier: %s Value: %s%n", |
| 312 | + cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); |
| 313 | + } |
| 314 | + } |
| 315 | + return authorizedViewRows; |
| 316 | + } catch (NotFoundException e) { |
| 317 | + System.err.println("Failed to read a non-existent authorized view: " + e.getMessage()); |
| 318 | + return null; |
| 319 | + } |
| 320 | + // [END bigtable_authorized_view_scan_with_filter] |
| 321 | + } |
| 322 | +} |
0 commit comments