2424import com .google .bigtable .admin .v2 .ListTablesRequest ;
2525import com .google .bigtable .admin .v2 .ModifyColumnFamiliesRequest ;
2626import com .google .bigtable .admin .v2 .Table ;
27- import com .google .cloud .bigtable .admin .v2 .BigtableTableAdminClient ;
28- import com .google .cloud .bigtable .admin .v2 .BigtableTableAdminSettings ;
27+ import com .google .cloud .bigtable .admin .v2 .BaseBigtableTableAdminSettings ;
28+ import com .google .cloud .bigtable .admin .v2 .BigtableTableAdminClientV2 ;
2929import com .google .cloud .bigtable .admin .v2 .models .GcRuleBuilder ;
3030import java .io .IOException ;
3131import java .time .Duration ;
@@ -62,7 +62,7 @@ public class TableAdminExample {
6262 private final String projectId ;
6363 private final String instanceId ;
6464 private final String tableId ;
65- private final BigtableTableAdminClient adminClient ;
65+ private final BigtableTableAdminClientV2 adminClient ;
6666
6767 public static void main (String [] args ) throws IOException {
6868
@@ -83,14 +83,11 @@ public TableAdminExample(String projectId, String instanceId, String tableId) th
8383 this .tableId = tableId ;
8484
8585 // Creates the settings to configure a bigtable table admin client.
86- BigtableTableAdminSettings adminSettings =
87- BigtableTableAdminSettings .newBuilder ()
88- .setProjectId (projectId )
89- .setInstanceId (instanceId )
90- .build ();
86+ BaseBigtableTableAdminSettings adminSettings =
87+ BaseBigtableTableAdminSettings .newBuilder ().build ();
9188
9289 // Creates a bigtable table admin client.
93- adminClient = BigtableTableAdminClient .create (adminSettings );
90+ adminClient = BigtableTableAdminClientV2 .create (adminSettings );
9491 }
9592
9693 public void run () {
@@ -119,7 +116,17 @@ void close() {
119116 public void createTable () {
120117 // [START bigtable_create_table]
121118 // Checks if table exists, creates table if does not exist.
122- if (!adminClient .exists (tableId )) {
119+ boolean exists = false ;
120+ try {
121+ adminClient .getTable (
122+ com .google .bigtable .admin .v2 .GetTableRequest .newBuilder ()
123+ .setName ("projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId )
124+ .build ());
125+ exists = true ;
126+ } catch (NotFoundException e ) {
127+ // ignore
128+ }
129+ if (!exists ) {
123130 System .out .println ("Table does not exist, creating table: " + tableId );
124131 String parent = "projects/" + projectId + "/instances/" + instanceId ;
125132 CreateTableRequest createTableRequest =
@@ -131,7 +138,7 @@ public void createTable() {
131138 .putColumnFamilies ("cf" , ColumnFamily .getDefaultInstance ())
132139 .build ())
133140 .build ();
134- Table table = adminClient .getBaseClient (). createTable (createTableRequest );
141+ Table table = adminClient .createTable (createTableRequest );
135142 System .out .printf ("Table: %s created successfully%n" , table .getName ());
136143 }
137144 // [END bigtable_create_table]
@@ -145,7 +152,7 @@ public void listAllTables() {
145152 try {
146153 String parent = "projects/" + projectId + "/instances/" + instanceId ;
147154 ListTablesRequest request = ListTablesRequest .newBuilder ().setParent (parent ).build ();
148- for (Table table : adminClient .getBaseClient (). listTables (request ).iterateAll ()) {
155+ for (Table table : adminClient .listTables (request ).iterateAll ()) {
149156 System .out .println (table .getName ());
150157 }
151158 } catch (NotFoundException e ) {
@@ -162,7 +169,7 @@ public void getTableMeta() {
162169 try {
163170 String tableName =
164171 "projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId ;
165- Table table = adminClient .getBaseClient (). getTable (tableName );
172+ Table table = adminClient .getTable (tableName );
166173 System .out .println ("Table: " + table .getName ());
167174 for (java .util .Map .Entry <String , ColumnFamily > entry :
168175 table .getColumnFamiliesMap ().entrySet ()) {
@@ -198,7 +205,7 @@ public void addFamilyWithMaxAgeRule() {
198205 .setId (COLUMN_FAMILY_1 )
199206 .setCreate (ColumnFamily .newBuilder ().setGcRule (maxAgeRule )))
200207 .build ();
201- adminClient .getBaseClient (). modifyColumnFamilies (request );
208+ adminClient .modifyColumnFamilies (request );
202209 System .out .println ("Created column family: " + COLUMN_FAMILY_1 );
203210 } catch (AlreadyExistsException e ) {
204211 System .err .println (
@@ -229,7 +236,7 @@ public void addFamilyWithMaxVersionsRule() {
229236 .setId (COLUMN_FAMILY_2 )
230237 .setCreate (ColumnFamily .newBuilder ().setGcRule (versionRule )))
231238 .build ();
232- adminClient .getBaseClient (). modifyColumnFamilies (request );
239+ adminClient .modifyColumnFamilies (request );
233240 System .out .println ("Created column family: " + COLUMN_FAMILY_2 );
234241 } catch (AlreadyExistsException e ) {
235242 System .err .println (
@@ -264,7 +271,7 @@ public void addFamilyWithUnionRule() {
264271 .setId (COLUMN_FAMILY_3 )
265272 .setCreate (ColumnFamily .newBuilder ().setGcRule (unionRule )))
266273 .build ();
267- adminClient .getBaseClient (). modifyColumnFamilies (request );
274+ adminClient .modifyColumnFamilies (request );
268275 System .out .println ("Created column family: " + COLUMN_FAMILY_3 );
269276 } catch (AlreadyExistsException e ) {
270277 System .err .println (
@@ -298,7 +305,7 @@ public void addFamilyWithIntersectionRule() {
298305 .setId (COLUMN_FAMILY_4 )
299306 .setCreate (ColumnFamily .newBuilder ().setGcRule (intersectionRule )))
300307 .build ();
301- adminClient .getBaseClient (). modifyColumnFamilies (request );
308+ adminClient .modifyColumnFamilies (request );
302309 System .out .println ("Created column family: " + COLUMN_FAMILY_4 );
303310 } catch (AlreadyExistsException e ) {
304311 System .err .println (
@@ -335,7 +342,7 @@ public void addFamilyWithNestedRule() {
335342 .setId (COLUMN_FAMILY_5 )
336343 .setCreate (ColumnFamily .newBuilder ().setGcRule (unionRule )))
337344 .build ();
338- adminClient .getBaseClient (). modifyColumnFamilies (request );
345+ adminClient .modifyColumnFamilies (request );
339346 System .out .println ("Created column family: " + COLUMN_FAMILY_5 );
340347 } catch (AlreadyExistsException e ) {
341348 System .err .println (
@@ -352,7 +359,7 @@ public void listColumnFamilies() {
352359 try {
353360 String tableName =
354361 "projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId ;
355- Table table = adminClient .getBaseClient (). getTable (tableName );
362+ Table table = adminClient .getTable (tableName );
356363 for (java .util .Map .Entry <String , ColumnFamily > entry :
357364 table .getColumnFamiliesMap ().entrySet ()) {
358365 System .out .printf (
@@ -383,7 +390,7 @@ public void modifyColumnFamilyRule() {
383390 .setId (COLUMN_FAMILY_1 )
384391 .setUpdate (ColumnFamily .newBuilder ().setGcRule (versionRule )))
385392 .build ();
386- adminClient .getBaseClient (). modifyColumnFamilies (request );
393+ adminClient .modifyColumnFamilies (request );
387394 System .out .printf ("Column family %s GC rule updated%n" , COLUMN_FAMILY_1 );
388395 } catch (NotFoundException e ) {
389396 System .err .println ("Failed to modify a non-existent column family: " + e .getMessage ());
@@ -398,7 +405,7 @@ public void printModifiedColumnFamily() {
398405 try {
399406 String tableName =
400407 "projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId ;
401- Table table = adminClient .getBaseClient (). getTable (tableName );
408+ Table table = adminClient .getTable (tableName );
402409 if (table .containsColumnFamilies (COLUMN_FAMILY_1 )) {
403410 System .out .printf (
404411 "Column family: %s%nGC Rule: %s%n" ,
@@ -426,7 +433,7 @@ public void deleteColumnFamily() {
426433 .setId (COLUMN_FAMILY_2 )
427434 .setDrop (true ))
428435 .build ();
429- adminClient .getBaseClient (). modifyColumnFamilies (request );
436+ adminClient .modifyColumnFamilies (request );
430437 System .out .printf ("Column family %s deleted successfully%n" , COLUMN_FAMILY_2 );
431438 } catch (NotFoundException e ) {
432439 System .err .println ("Failed to delete a non-existent column family: " + e .getMessage ());
@@ -442,7 +449,7 @@ public void deleteTable() {
442449 try {
443450 String tableName =
444451 "projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId ;
445- adminClient .getBaseClient (). deleteTable (tableName );
452+ adminClient .deleteTable (tableName );
446453 System .out .printf ("Table: %s deleted successfully%n" , tableId );
447454 } catch (NotFoundException e ) {
448455 System .err .println ("Failed to delete a non-existent table: " + e .getMessage ());
0 commit comments