2121import com .google .api .gax .rpc .NotFoundException ;
2222import com .google .api .gax .rpc .PermissionDeniedException ;
2323import 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 ;
24+ import com .google .bigtable .admin .v2 .AuthorizedView ;
25+ import com .google .bigtable .admin .v2 .AuthorizedViewName ;
26+ import com .google .bigtable .admin .v2 .ColumnFamily ;
27+ import com .google .bigtable .admin .v2 .CreateAuthorizedViewRequest ;
28+ import com .google .bigtable .admin .v2 .CreateTableRequest ;
29+ import com .google .bigtable .admin .v2 .GetTableRequest ;
30+ import com .google .bigtable .admin .v2 .ListAuthorizedViewsRequest ;
31+ import com .google .bigtable .admin .v2 .Table ;
32+ import com .google .bigtable .admin .v2 .UpdateAuthorizedViewRequest ;
33+ import com .google .cloud .bigtable .admin .v2 .BigtableTableAdminClientV2 ;
3334import com .google .cloud .bigtable .data .v2 .BigtableDataClient ;
3435import com .google .cloud .bigtable .data .v2 .BigtableDataSettings ;
3536import com .google .cloud .bigtable .data .v2 .models .AuthorizedViewId ;
3940import com .google .cloud .bigtable .data .v2 .models .RowCell ;
4041import com .google .cloud .bigtable .data .v2 .models .RowMutation ;
4142import com .google .protobuf .ByteString ;
43+ import com .google .protobuf .FieldMask ;
4244import java .io .IOException ;
4345import java .util .ArrayList ;
4446import java .util .List ;
@@ -50,9 +52,11 @@ public class AuthorizedViewExample {
5052 private static final String COLUMN_QUALIFIER_GREETING = "greeting" ;
5153 private static final String COLUMN_QUALIFIER_NAME = "name" ;
5254 private static final String ROW_KEY_PREFIX = "rowKey" ;
55+ private final String projectId ;
56+ private final String instanceId ;
5357 private final String tableId ;
5458 private final String authorizedViewId ;
55- private final BigtableTableAdminClient adminClient ;
59+ private final BigtableTableAdminClientV2 adminClient ;
5660 private final BigtableDataClient dataClient ;
5761
5862 public static void main (String [] args ) throws IOException {
@@ -72,6 +76,8 @@ public static void main(String[] args) throws IOException {
7276 public AuthorizedViewExample (
7377 String projectId , String instanceId , String tableId , String authorizedViewId )
7478 throws IOException {
79+ this .projectId = projectId ;
80+ this .instanceId = instanceId ;
7581 this .tableId = tableId ;
7682 this .authorizedViewId = authorizedViewId ;
7783
@@ -82,15 +88,21 @@ public AuthorizedViewExample(
8288 // Creates a bigtable data client.
8389 dataClient = BigtableDataClient .create (settings );
8490
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-
9291 // Creates a bigtable table admin client.
93- adminClient = BigtableTableAdminClient .create (adminSettings );
92+ adminClient = BigtableTableAdminClientV2 .create ();
93+ }
94+
95+ private boolean exists (String tableId ) {
96+ try {
97+ adminClient .getTable (
98+ GetTableRequest .newBuilder ()
99+ .setName ("projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId )
100+ .setView (Table .View .NAME_ONLY )
101+ .build ());
102+ return true ;
103+ } catch (NotFoundException e ) {
104+ return false ;
105+ }
94106 }
95107
96108 public void close () {
@@ -114,20 +126,28 @@ public void run() {
114126
115127 public void createTable () {
116128 // Checks if table exists, creates table if it does not exist.
117- if (!adminClient . exists (tableId )) {
129+ if (!exists (tableId )) {
118130 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 ());
131+ CreateTableRequest request =
132+ CreateTableRequest .newBuilder ()
133+ .setParent ("projects/" + projectId + "/instances/" + instanceId )
134+ .setTableId (tableId )
135+ .setTable (
136+ Table .newBuilder ()
137+ .putColumnFamilies (COLUMN_FAMILY , ColumnFamily .getDefaultInstance ())
138+ .build ())
139+ .build ();
140+ Table table = adminClient .createTable (request );
141+ System .out .printf ("Table: %s created successfully%n" , table .getName ());
123142 }
124143 }
125144
126145 public void deleteTable () {
127146 // Deletes the entire table.
128147 System .out .println ("\n Delete table: " + tableId );
129148 try {
130- adminClient .deleteTable (tableId );
149+ adminClient .deleteTable (
150+ "projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId );
131151 System .out .printf ("Table: %s deleted successfully%n" , tableId );
132152 } catch (NotFoundException e ) {
133153 System .err .println ("Failed to delete a non-existent table: " + e .getMessage ());
@@ -140,24 +160,41 @@ public void deleteTable() {
140160 public void createAuthorizedView () {
141161 // Checks if the authorized view exists, creates it if it does not exist.
142162 try {
143- adminClient .getAuthorizedView (tableId , authorizedViewId );
163+ adminClient .getAuthorizedView (
164+ "projects/"
165+ + projectId
166+ + "/instances/"
167+ + instanceId
168+ + "/tables/"
169+ + tableId
170+ + "/authorizedViews/"
171+ + authorizedViewId );
144172 } catch (NotFoundException exception ) {
145173 System .out .printf ("%nCreating authorized view %s in table %s%n" , authorizedViewId , tableId );
146174 // [START bigtable_create_authorized_view]
147175 try {
176+ AuthorizedView .SubsetView subsetView =
177+ AuthorizedView .SubsetView .newBuilder ()
178+ .addRowPrefixes (ByteString .EMPTY )
179+ .putFamilySubsets (
180+ COLUMN_FAMILY ,
181+ AuthorizedView .FamilySubsets .newBuilder ()
182+ .addQualifierPrefixes (ByteString .copyFromUtf8 (COLUMN_QUALIFIER_NAME ))
183+ .build ())
184+ .build ();
185+ AuthorizedView authorizedViewObj =
186+ AuthorizedView .newBuilder ().setSubsetView (subsetView ).build ();
148187 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 ());
188+ CreateAuthorizedViewRequest .newBuilder ()
189+ .setParent (
190+ "projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId )
191+ .setAuthorizedViewId (authorizedViewId )
192+ .setAuthorizedView (authorizedViewObj )
193+ .build ();
194+ AuthorizedView authorizedView = adminClient .createAuthorizedViewAsync (request ).get ();
195+ System .out .printf ("AuthorizedView: %s created successfully%n" , authorizedView .getName ());
196+ } catch (Exception e ) {
197+ System .err .println ("Failed to create an authorized view: " + e .getMessage ());
161198 }
162199 // [END bigtable_create_authorized_view]
163200 }
@@ -169,17 +206,37 @@ public void updateAuthorizedView() {
169206 // [START bigtable_update_authorized_view]
170207 try {
171208 // Update to an authorized view permitting everything.
209+ AuthorizedView .SubsetView subsetView =
210+ AuthorizedView .SubsetView .newBuilder ()
211+ .addRowPrefixes (ByteString .EMPTY )
212+ .putFamilySubsets (
213+ COLUMN_FAMILY ,
214+ AuthorizedView .FamilySubsets .newBuilder ()
215+ .addQualifierPrefixes (ByteString .EMPTY )
216+ .build ())
217+ .build ();
218+ AuthorizedView authorizedViewObj =
219+ AuthorizedView .newBuilder ()
220+ .setSubsetView (subsetView )
221+ .setName (
222+ "projects/"
223+ + projectId
224+ + "/instances/"
225+ + instanceId
226+ + "/tables/"
227+ + tableId
228+ + "/authorizedViews/"
229+ + authorizedViewId )
230+ .build ();
172231 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 ());
232+ UpdateAuthorizedViewRequest .newBuilder ()
233+ .setAuthorizedView (authorizedViewObj )
234+ .setUpdateMask (FieldMask .newBuilder ().addPaths ("subset_view" ).build ())
235+ .build ();
236+ AuthorizedView authorizedView = adminClient .updateAuthorizedViewAsync (request ).get ();
237+ System .out .printf ("AuthorizedView: %s updated successfully%n" , authorizedView .getName ());
238+ } catch (Exception e ) {
239+ System .err .println ("Failed to modify authorized view: " + e .getMessage ());
183240 }
184241 // [END bigtable_update_authorized_view]
185242 }
@@ -190,19 +247,29 @@ public AuthorizedView getAuthorizedView() {
190247 // [START bigtable_get_authorized_view]
191248 AuthorizedView authorizedView = null ;
192249 try {
193- authorizedView = adminClient .getAuthorizedView (tableId , authorizedViewId );
194- SubsetView subsetView = (SubsetView ) authorizedView .getAuthorizedViewType ();
250+ authorizedView =
251+ adminClient .getAuthorizedView (
252+ "projects/"
253+ + projectId
254+ + "/instances/"
255+ + instanceId
256+ + "/tables/"
257+ + tableId
258+ + "/authorizedViews/"
259+ + authorizedViewId );
260+ AuthorizedView .SubsetView subsetView = authorizedView .getSubsetView ();
195261
196- for (ByteString rowPrefix : subsetView .getRowPrefixes ()) {
262+ for (ByteString rowPrefix : subsetView .getRowPrefixesList ()) {
197263 System .out .printf ("Row Prefix: %s%n" , rowPrefix .toStringUtf8 ());
198264 }
199- for (Map .Entry <String , FamilySubsets > entry : subsetView .getFamilySubsets ().entrySet ()) {
200- for (ByteString qualifierPrefix : entry .getValue ().getQualifierPrefixes ()) {
265+ for (Map .Entry <String , AuthorizedView .FamilySubsets > entry :
266+ subsetView .getFamilySubsetsMap ().entrySet ()) {
267+ for (ByteString qualifierPrefix : entry .getValue ().getQualifierPrefixesList ()) {
201268 System .out .printf (
202269 "Column Family: %s, Qualifier Prefix: %s%n" ,
203270 entry .getKey (), qualifierPrefix .toStringUtf8 ());
204271 }
205- for (ByteString qualifier : entry .getValue ().getQualifiers ()) {
272+ for (ByteString qualifier : entry .getValue ().getQualifiersList ()) {
206273 System .out .printf (
207274 "Column Family: %s, Qualifier: %s%n" , entry .getKey (), qualifier .toStringUtf8 ());
208275 }
@@ -221,9 +288,15 @@ public List<String> listAllAuthorizedViews() {
221288 // [START bigtable_list_authorized_views]
222289 List <String > authorizedViewIds = new ArrayList <>();
223290 try {
224- authorizedViewIds = adminClient .listAuthorizedViews (tableId );
225- for (String authorizedViewId : authorizedViewIds ) {
226- System .out .println (authorizedViewId );
291+ ListAuthorizedViewsRequest request =
292+ ListAuthorizedViewsRequest .newBuilder ()
293+ .setParent (
294+ "projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId )
295+ .build ();
296+ for (AuthorizedView view : adminClient .listAuthorizedViews (request ).iterateAll ()) {
297+ String id = AuthorizedViewName .parse (view .getName ()).getAuthorizedView ();
298+ System .out .println (id );
299+ authorizedViewIds .add (id );
227300 }
228301 } catch (NotFoundException e ) {
229302 System .err .println (
@@ -238,7 +311,15 @@ public void deleteAuthorizedView() {
238311 System .out .printf ("%nDeleting authorized view %s in table %s%n" , authorizedViewId , tableId );
239312 // [START bigtable_delete_authorized_view]
240313 try {
241- adminClient .deleteAuthorizedView (tableId , authorizedViewId );
314+ adminClient .deleteAuthorizedView (
315+ "projects/"
316+ + projectId
317+ + "/instances/"
318+ + instanceId
319+ + "/tables/"
320+ + tableId
321+ + "/authorizedViews/"
322+ + authorizedViewId );
242323 System .out .printf ("AuthorizedView: %s deleted successfully%n" , authorizedViewId );
243324 } catch (NotFoundException e ) {
244325 System .err .println ("Failed to delete a non-existent authorized view: " + e .getMessage ());
0 commit comments