1515 */
1616package com .google .cloud .examples .bigtable ;
1717
18+ import static com .google .cloud .bigtable .admin .v2 .models .GCRules .GCRULES ;
19+
1820import com .google .api .gax .rpc .AlreadyExistsException ;
1921import com .google .api .gax .rpc .NotFoundException ;
2022import com .google .cloud .bigtable .admin .v2 .BigtableTableAdminClient ;
2123import com .google .cloud .bigtable .admin .v2 .BigtableTableAdminSettings ;
2224import com .google .cloud .bigtable .admin .v2 .models .ColumnFamily ;
2325import com .google .cloud .bigtable .admin .v2 .models .CreateTableRequest ;
24- import com .google .cloud .bigtable .admin .v2 .models .GCRules ;
26+ import com .google .cloud .bigtable .admin .v2 .models .GCRules .DurationRule ;
27+ import com .google .cloud .bigtable .admin .v2 .models .GCRules .IntersectionRule ;
28+ import com .google .cloud .bigtable .admin .v2 .models .GCRules .UnionRule ;
29+ import com .google .cloud .bigtable .admin .v2 .models .GCRules .VersionRule ;
2530import com .google .cloud .bigtable .admin .v2 .models .ModifyColumnFamiliesRequest ;
2631import com .google .cloud .bigtable .admin .v2 .models .Table ;
2732import java .io .IOException ;
@@ -94,11 +99,11 @@ public void run() {
9499 createTable ();
95100 listAllTables ();
96101 getTableMeta ();
97- maxAgeRule ();
98- maxVersionsRule ();
99- unionRule ();
100- intersectionRule ();
101- nestedRule ();
102+ addFamilyWithMaxAgeRule ();
103+ addFamilyWithMaxVersionsRule ();
104+ addFamilyWithUnionRule ();
105+ addFamilyWithIntersectionRule ();
106+ addFamilyWithNestedRule ();
102107 listColumnFamilies ();
103108 modifyColumnFamilyRule ();
104109 printModifiedColumnFamily ();
@@ -126,9 +131,9 @@ public void listAllTables() {
126131 // [START bigtable_list_tables]
127132 // Lists tables in the current instance.
128133 try {
129- List <String > listTables = adminClient .listTables ();
130- for (String tableName : listTables ) {
131- System .out .println (tableName );
134+ List <String > tableIds = adminClient .listTables ();
135+ for (String tableId : tableIds ) {
136+ System .out .println (tableId );
132137 }
133138 } catch (NotFoundException e ) {
134139 System .err .println ("Failed to list tables from a non-existent instance: " + e .getMessage ());
@@ -146,7 +151,9 @@ public void getTableMeta() {
146151 System .out .println ("Table: " + table .getId ());
147152 Collection <ColumnFamily > columnFamilies = table .getColumnFamilies ();
148153 for (ColumnFamily columnFamily : columnFamilies ) {
149- printColumnFamily (columnFamily );
154+ System .out .printf (
155+ "Column family: %s%nGC Rule: %s%n" ,
156+ columnFamily .getId (), columnFamily .getGCRule ().toString ());
150157 }
151158 } catch (NotFoundException e ) {
152159 System .err .println (
@@ -156,20 +163,22 @@ public void getTableMeta() {
156163 }
157164
158165 /** Demonstrates how to create a new instance of the DurationRule. */
159- public void maxAgeRule () {
166+ public void addFamilyWithMaxAgeRule () {
160167 System .out .printf ("%nCreating column family %s with max age GC rule%n" , COLUMN_FAMILY_1 );
161168 // [START bigtable_create_family_gc_max_age]
162169 // Creates a column family with GC policy : maximum age
163170 // where age = current time minus cell timestamp
164171
165172 // Defines the GC rule to retain data with max age of 5 days.
166- GCRules . DurationRule maxAgeRule1 = GCRules . GCRULES .maxAge (5 , TimeUnit .DAYS );
173+ DurationRule maxAgeRule = GCRULES .maxAge (5 , TimeUnit .DAYS );
167174
168175 // Creates column family with given GC rule.
169176 try {
170- ModifyColumnFamiliesRequest columnFamiliesRequest1 =
171- ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_1 , maxAgeRule1 );
172- adminClient .modifyFamilies (columnFamiliesRequest1 );
177+ // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
178+ // being used to add a family
179+ ModifyColumnFamiliesRequest columnFamiliesRequest =
180+ ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_1 , maxAgeRule );
181+ adminClient .modifyFamilies (columnFamiliesRequest );
173182 System .out .println ("Created column family: " + COLUMN_FAMILY_1 );
174183 } catch (AlreadyExistsException e ) {
175184 System .err .println (
@@ -179,20 +188,22 @@ public void maxAgeRule() {
179188 }
180189
181190 /** Demonstrates how to create a new instance of the VersionRule. */
182- public void maxVersionsRule () {
191+ public void addFamilyWithMaxVersionsRule () {
183192 System .out .printf ("%nCreating column family %s with max versions GC rule%n" , COLUMN_FAMILY_2 );
184193 // [START bigtable_create_family_gc_max_versions]
185194 // Creates a column family with GC policy : most recent N versions
186195 // where 1 = most recent version
187196
188197 // Defines the GC policy to retain only the most recent 2 versions.
189- GCRules . VersionRule versionRule1 = GCRules . GCRULES .maxVersions (2 );
198+ VersionRule versionRule = GCRULES .maxVersions (2 );
190199
191200 // Creates column family with given GC rule.
192201 try {
193- ModifyColumnFamiliesRequest columnFamiliesRequest2 =
194- ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_2 , versionRule1 );
195- adminClient .modifyFamilies (columnFamiliesRequest2 );
202+ // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
203+ // being used to add a family
204+ ModifyColumnFamiliesRequest columnFamiliesRequest =
205+ ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_2 , versionRule );
206+ adminClient .modifyFamilies (columnFamiliesRequest );
196207 System .out .println ("Created column family: " + COLUMN_FAMILY_2 );
197208 } catch (AlreadyExistsException e ) {
198209 System .err .println (
@@ -202,22 +213,23 @@ public void maxVersionsRule() {
202213 }
203214
204215 /** Demonstrates how to create a new instance of the UnionRule. */
205- public void unionRule () {
216+ public void addFamilyWithUnionRule () {
206217 System .out .printf ("%nCreating column family %s with union GC rule%n" , COLUMN_FAMILY_3 );
207218 // [START bigtable_create_family_gc_union]
208219 // Creates a column family with GC policy to drop data that matches at least one condition.
209220
210- // Defines a GC rule to drop cells older than 5 days OR not the most recent version.
211- GCRules .DurationRule maxAgeRule2 = GCRules .GCRULES .maxAge (5 , TimeUnit .DAYS );
212- GCRules .VersionRule versionRule2 = GCRules .GCRULES .maxVersions (1 );
213- // Add rules to union rule list
214- GCRules .UnionRule unionRule1 = GCRules .GCRULES .union ().rule (maxAgeRule2 ).rule (versionRule2 );
221+ // Defines a list of GC rules to drop cells older than 5 days OR not the most recent
222+ // version.
223+ UnionRule unionRule =
224+ GCRULES .union ().rule (GCRULES .maxAge (5 , TimeUnit .DAYS )).rule (GCRULES .maxVersions (1 ));
215225
216226 // Creates column family with given GC rule.
217227 try {
218- ModifyColumnFamiliesRequest columnFamiliesRequest3 =
219- ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_3 , unionRule1 );
220- adminClient .modifyFamilies (columnFamiliesRequest3 );
228+ // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
229+ // being used to add a family
230+ ModifyColumnFamiliesRequest columnFamiliesRequest =
231+ ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_3 , unionRule );
232+ adminClient .modifyFamilies (columnFamiliesRequest );
221233 System .out .println ("Created column family: " + COLUMN_FAMILY_3 );
222234 } catch (AlreadyExistsException e ) {
223235 System .err .println (
@@ -227,22 +239,23 @@ public void unionRule() {
227239 }
228240
229241 /** Demonstrates how to create a new instance of the IntersectionRule. */
230- public void intersectionRule () {
242+ public void addFamilyWithIntersectionRule () {
231243 System .out .printf ("%nCreating column family %s with intersection GC rule%n" , COLUMN_FAMILY_4 );
232244 // [START bigtable_create_family_gc_intersection]
233245 // Creates a column family with GC policy to drop data that matches all conditions.
234246
235247 // Defines a GC rule to drop cells older than 5 days AND older than the most recent 2 versions.
236- GCRules .DurationRule maxAgeRule3 = GCRules .GCRULES .maxAge (5 , TimeUnit .DAYS );
237- GCRules .VersionRule versionRule3 = GCRules .GCRULES .maxVersions (2 );
238- GCRules .IntersectionRule intersectionRule1 =
239- GCRules .GCRULES .intersection ().rule (maxAgeRule3 ).rule (versionRule3 );
248+ DurationRule maxAgeRule = GCRULES .maxAge (5 , TimeUnit .DAYS );
249+ VersionRule versionRule = GCRULES .maxVersions (2 );
250+ IntersectionRule intersectionRule = GCRULES .intersection ().rule (maxAgeRule ).rule (versionRule );
240251
241252 // Creates column family with given GC rule.
242253 try {
243- ModifyColumnFamiliesRequest columnFamiliesRequest4 =
244- ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_4 , intersectionRule1 );
245- adminClient .modifyFamilies (columnFamiliesRequest4 );
254+ // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
255+ // being used to add a family
256+ ModifyColumnFamiliesRequest columnFamiliesRequest =
257+ ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_4 , intersectionRule );
258+ adminClient .modifyFamilies (columnFamiliesRequest );
246259 System .out .println ("Created column family: " + COLUMN_FAMILY_4 );
247260 } catch (AlreadyExistsException e ) {
248261 System .err .println (
@@ -252,26 +265,26 @@ public void intersectionRule() {
252265 }
253266
254267 /** Demonstrates how to create a nested rule using the IntersectionRule and UnionRule. */
255- public void nestedRule () {
268+ public void addFamilyWithNestedRule () {
256269 System .out .printf ("%nCreating column family %s with a nested GC rule%n" , COLUMN_FAMILY_5 );
257270 // [START bigtable_create_family_gc_nested]
258271 // Creates a nested GC rule:
259272 // Drop cells that are either older than the 10 recent versions
260273 // OR
261274 // Drop cells that are older than a month AND older than the 2 recent versions
262- GCRules .VersionRule versionRule4 = GCRules .GCRULES .maxVersions (10 );
263- GCRules .DurationRule maxAgeRule4 = GCRules .GCRULES .maxAge (30 , TimeUnit .DAYS );
264- GCRules .VersionRule versionRule5 = GCRules .GCRULES .maxVersions (2 );
265- GCRules .IntersectionRule intersectionRule2 =
266- GCRules .GCRULES .intersection ().rule (maxAgeRule4 ).rule (versionRule5 );
267- GCRules .UnionRule unionRule2 =
268- GCRules .GCRULES .union ().rule (intersectionRule2 ).rule (versionRule4 );
275+ VersionRule versionRule1 = GCRULES .maxVersions (10 );
276+ VersionRule versionRule2 = GCRULES .maxVersions (2 );
277+ DurationRule maxAgeRule = GCRULES .maxAge (30 , TimeUnit .DAYS );
278+ IntersectionRule intersectionRule = GCRULES .intersection ().rule (maxAgeRule ).rule (versionRule2 );
279+ UnionRule unionRule = GCRULES .union ().rule (intersectionRule ).rule (versionRule1 );
269280
270281 // Creates column family with given GC rule.
271282 try {
272- ModifyColumnFamiliesRequest columnFamiliesRequest5 =
273- ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_5 , unionRule2 );
274- adminClient .modifyFamilies (columnFamiliesRequest5 );
283+ // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
284+ // being used to add a family
285+ ModifyColumnFamiliesRequest columnFamiliesRequest =
286+ ModifyColumnFamiliesRequest .of (tableId ).addFamily (COLUMN_FAMILY_5 , unionRule );
287+ adminClient .modifyFamilies (columnFamiliesRequest );
275288 System .out .println ("Created column family: " + COLUMN_FAMILY_5 );
276289 } catch (AlreadyExistsException e ) {
277290 System .err .println (
@@ -289,7 +302,9 @@ public void listColumnFamilies() {
289302 Table table = adminClient .getTable (tableId );
290303 Collection <ColumnFamily > columnFamilies = table .getColumnFamilies ();
291304 for (ColumnFamily columnFamily : columnFamilies ) {
292- printColumnFamily (columnFamily );
305+ System .out .printf (
306+ "Column family: %s%nGC Rule: %s%n" ,
307+ columnFamily .getId (), columnFamily .getGCRule ().toString ());
293308 }
294309 } catch (NotFoundException e ) {
295310 System .err .println (
@@ -304,11 +319,13 @@ public void modifyColumnFamilyRule() {
304319 // [START bigtable_update_gc_rule]
305320 // Updates the column family metadata to update the GC rule.
306321 // Updates a column family GC rule.
307- GCRules . VersionRule versionRule6 = GCRules . GCRULES .maxVersions (1 );
322+ VersionRule versionRule = GCRULES .maxVersions (1 );
308323 try {
324+ // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
325+ // being used to modify a family
309326 // Updates column family with given GC rule.
310327 ModifyColumnFamiliesRequest updateRequest =
311- ModifyColumnFamiliesRequest .of (tableId ).updateFamily (COLUMN_FAMILY_1 , versionRule6 );
328+ ModifyColumnFamiliesRequest .of (tableId ).updateFamily (COLUMN_FAMILY_1 , versionRule );
312329 adminClient .modifyFamilies (updateRequest );
313330 System .out .printf ("Column family %s GC rule updated%n" , COLUMN_FAMILY_1 );
314331 } catch (NotFoundException e ) {
@@ -326,7 +343,9 @@ public void printModifiedColumnFamily() {
326343 Collection <ColumnFamily > columnFamilies = table .getColumnFamilies ();
327344 for (ColumnFamily columnFamily : columnFamilies ) {
328345 if (columnFamily .getId ().equals (COLUMN_FAMILY_1 )) {
329- printColumnFamily (columnFamily );
346+ System .out .printf (
347+ "Column family: %s%nGC Rule: %s%n" ,
348+ columnFamily .getId (), columnFamily .getGCRule ().toString ());
330349 }
331350 }
332351 } catch (NotFoundException e ) {
@@ -364,10 +383,4 @@ public void deleteTable() {
364383 }
365384 // [END bigtable_delete_table]
366385 }
367-
368- private static void printColumnFamily (ColumnFamily columnFamily ) {
369- System .out .printf (
370- "Column family: %s%nGC Rule: %s%n" ,
371- columnFamily .getId (), columnFamily .getGCRule ().toString ());
372- }
373386}
0 commit comments