This page provides examples of how to set and update garbage-collection policies
when you use Cloud Bigtable client libraries or
the cbt
command-line tool. Before you read this page, you should be
familiar with garbage collection.
Before you use the cbt
tool, follow the setup instructions,
including the steps to create a .cbtrc
file.
Viewing current garbage-collection policies
You can look up the current garbage collection policies for a given
table by running the following command, substituting a valid table ID for
TABLE_ID
:
cbt ls TABLE_ID
The console displays a list of the table's column families along with each column family's garbage-collection policy.
Garbage collecting based on age
The following code samples show how to set the maximum age for data in a column family.
cbt
This example creates a column family named cf1
, then sets the maximum age
for data in the column family to five days. This tells the garbage collector
to remove all data with timestamps older than five days in all columns in this
column family.
cbt createfamily your-table cf1
cbt setgcpolicy your-table cf1 maxage=5d
Go
HBase
This code sample is coming soon.
Java
Python
C#
C++
Node.js
PHP
Ruby
Garbage collecting based on number of versions
The following code samples show how to create a column family with a garbage-collection policy that states the number of versions to keep.
cbt
This example creates a column family named cf2
, then sets the number of
versions to keep in the column family to two. This tells the garbage collector
to remove all but the two most recent cells in all columns in this
column family.
cbt createfamily your-table cf2
cbt setgcpolicy your-table cf2 maxversions=2
This example sets a policy to remove all cells except the newest one.
cbt setgcpolicy your-table cf2 maxversions=1
Go
HBase
This code sample is coming soon.
Java
Python
C#
C++
Node.js
PHP
Ruby
Garbage collecting based on multiple criteria
The following code samples demonstrate how to create a column family with an intersection garbage-collection policy.
cbt
This example creates a column family named cf4
, then sets a garbage
collection policy that removes cells that are older than five days and
older than the two most recent cells for all columns in the column family.
Cells must meet both criteria to be removed.
cbt createfamily your-table cf4
cbt setgcpolicy your-table cf4 maxage=5d and maxversions=2
This example sets a policy that removes all cells more than 14 days old except the newest one.
cbt setgcpolicy your-table cf4 maxage=14d and maxversion=1
Go
HBase
This code sample is coming soon.
Java
Python
C#
C++
Node.js
PHP
Ruby
The following code samples show how to create a column family that has a union garbage-collection policy.
cbt
This example creates a column family named cf3
, then sets a garbage
collection policy that removes cells that are more than five days old or
are older than the two most recent cells. Cells must meet either conditions
to be removed.
cbt createfamily your-table cf3
cbt setgcpolicy your-table cf3 maxage=5d or maxversions=2
This example sets a policy that removes all cell values except the newest one, but removes even the newest cell if it is more than one second old.
cbt setgcpolicy your-table cf3 maxversions=1 or maxage=1s
Go
HBase
This code sample is coming soon.
Java
Python
C#
C++
Node.js
PHP
Ruby
The following code samples show how to create a column family that has a nested garbage-collection policy. A nested garbage-collection policy has a combination of union and intersection rules.
cbt
This example creates a column family named cf5
, then sets a garbage
collection policy that removes cells in the column family that meet
either of the following conditions:
- Older than the ten most recent cells
- More than one minute old and older than the two most recent cells
In other words, this policy keeps the two newest cells even if they are more than one minute old, or keeps the ten newest cells as long as they are less than one minute old.
cbt createfamily your-table cf5
cbt setgcpolicy your-table cf5 "(maxage=1m and maxversions=2) or
maxversions=10"
Go
HBase
This code sample is coming soon.
Java
Python
C#
C++
Node.js
PHP
Ruby
Updating a garbage-collection policy
The following code samples show how to modify an existing garbage-collection policy.
cbt
cbt setgcpolicy your-table cf1 maxage=1d
Go
This code sample is coming soon.
HBase
This code sample is coming soon.
Java
Python
C#
C++
Node.js
PHP
Ruby
What's next
- Learn how to design your schema.
- Look at additional code samples.
- Explore strategies to simulate cell-level TTL.
- Read about how timestamps that are sequential numbers affect garbage collection.
- Learn how to keep only the most recent value of a column.
- Learn more about the
cbt
command-line tool for Cloud Bigtable.