Bigtable Row
User-friendly container for Google Cloud Bigtable Row.
class google.cloud.bigtable.row.AppendRow(row_key, table)
Bases: google.cloud.bigtable.row.Row
Google Cloud Bigtable Row for sending append mutations.
These mutations are intended to augment the value of an existing cell and uses the methods:
append_cell_value()
increment_cell_value()
The first works by appending bytes and the second by incrementing an integer (stored in the cell as 8 bytes). In either case, if the cell is empty, assumes the default empty value (empty string for bytes or 0 for integer).
Parameters
append_cell_value(column_family_id, column, value)
Appends a value to an existing cell.
NOTE: This method adds a read-modify rule protobuf to the accumulated
read-modify rules on this row, but does not make an API
request. To actually send an API request (with the rules) to the
Google Cloud Bigtable API, call commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1, append=True)
cell_val2 = b"2"
row.append_cell_value(COLUMN_FAMILY_ID, COL_NAME1, cell_val2)
Parameters
column_family_id (str) – The column family that contains the column. Must be of the form
[_a-zA-Z0-9][-_.a-zA-Z0-9]\*
.column (bytes) – The column within the column family where the cell is located.
value (bytes) – The value to append to the existing value in the cell. If the targeted cell is unset, it will be treated as containing the empty string.
clear()
Removes all currently accumulated modifications on current row.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_1"
row_obj = table.row(row_key)
row_obj.set_cell(COLUMN_FAMILY_ID, COL_NAME1, b"cell-val")
row_obj.clear()
commit()
Makes a ReadModifyWriteRow
API request.
This commits modifications made by append_cell_value()
and
increment_cell_value()
. If no modifications were made, makes
no API request and just returns {}
.
Modifies a row atomically, reading the latest existing timestamp / value from the specified columns and writing a new value by appending / incrementing. The new cell created uses either the current server time or the highest timestamp of a cell in that column (if it exceeds the server time).
After committing the accumulated mutations, resets the local mutations.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_2"
cell_val = b"cell-val"
row_obj = table.row(row_key)
row_obj.set_cell(COLUMN_FAMILY_ID, COL_NAME1, cell_val)
row_obj.commit()
Return type
Returns
The new contents of all modified cells. Returned as a dictionary of column families, each of which holds a dictionary of columns. Each column contains a list of cells modified. Each cell is represented with a two-tuple with the value (in bytes) and the timestamp for the cell.
Raises
ValueError
if the number of mutations exceeds theMAX_MUTATIONS
.
increment_cell_value(column_family_id, column, int_value)
Increments a value in an existing cell.
Assumes the value in the cell is stored as a 64 bit integer serialized to bytes.
NOTE: This method adds a read-modify rule protobuf to the accumulated
read-modify rules on this row, but does not make an API
request. To actually send an API request (with the rules) to the
Google Cloud Bigtable API, call commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY2, append=True)
int_val = 3
row.increment_cell_value(COLUMN_FAMILY_ID, COL_NAME1, int_val)
Parameters
column_family_id (str) – The column family that contains the column. Must be of the form
[_a-zA-Z0-9][-_.a-zA-Z0-9]\*
.column (bytes) – The column within the column family where the cell is located.
int_value (int) – The value to increment the existing value in the cell by. If the targeted cell is unset, it will be treated as containing a zero. Otherwise, the targeted cell must contain an 8-byte value (interpreted as a 64-bit big-endian signed integer), or the entire request will fail.
property row_key()
Row key.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
row_key = row.row_key
Return type
Returns
The key for the current row.
property table()
Row table.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
table1 = row.table
Return type
table:
Table
Returns
table: The table that owns the row.
class google.cloud.bigtable.row.ConditionalRow(row_key, table, filter_)
Bases: google.cloud.bigtable.row._SetDeleteRow
Google Cloud Bigtable Row for sending mutations conditionally.
Each mutation has an associated state: True
or False
.
When commit()
-ed, the mutations for the True
state will be applied if the filter matches any cells in
the row, otherwise the False
state will be applied.
A ConditionalRow
accumulates mutations in the same way a
DirectRow
does:
set_cell()
delete()
delete_cell()
delete_cells()
with the only change the extra state
parameter:
>>> row_cond = table.row(b'row-key2', filter_=row_filter)
>>> row_cond.set_cell(u'fam', b'col', b'cell-val', state=True)
>>> row_cond.delete_cell(u'fam', b'col', state=False)
NOTE: As with DirectRow
, to actually send these mutations to the
Google Cloud Bigtable API, you must call commit()
.
Parameters
clear()
Removes all currently accumulated mutations on the current row.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_1"
row_obj = table.row(row_key)
row_obj.set_cell(COLUMN_FAMILY_ID, COL_NAME1, b"cell-val")
row_obj.clear()
commit()
Makes a CheckAndMutateRow
API request.
If no mutations have been created in the row, no request is made.
The mutations will be applied conditionally, based on whether the
filter matches any cells in the ConditionalRow
or not. (Each
method which adds a mutation has a state
parameter for this
purpose.)
Mutations are applied atomically and in order, meaning that earlier mutations can be masked / negated by later ones. Cells already present in the row are left unchanged unless explicitly changed by a mutation.
After committing the accumulated mutations, resets the local mutations.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_2"
cell_val = b"cell-val"
row_obj = table.row(row_key)
row_obj.set_cell(COLUMN_FAMILY_ID, COL_NAME1, cell_val)
row_obj.commit()
Return type
Returns
Flag indicating if the filter was matched (which also indicates which set of mutations were applied by the server).
Raises
ValueError
if the number of mutations exceeds theMAX_MUTATIONS
.
delete(state=True)
Deletes this row from the table.
NOTE: This method adds a mutation to the accumulated mutations on this
row, but does not make an API request. To actually
send an API request (with the mutations) to the Google Cloud
Bigtable API, call commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_1"
row_obj = table.row(row_key)
row_obj.delete()
row_obj.commit()
Parameters
state (bool) – (Optional) The state that the mutation should be applied in. Defaults to
True
.
delete_cell(column_family_id, column, time_range=None, state=True)
Deletes cell in this row.
NOTE: > This method adds a mutation to the accumulated mutations on this
row, but does not make an API request. To actually send an API request (with the mutations) to the Google Cloud Bigtable API, call
commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_1"
row_obj = table.row(row_key)
row_obj.delete_cell(COLUMN_FAMILY_ID, COL_NAME1)
row_obj.commit()
Parameters
column_family_id (str) – The column family that contains the column or columns with cells being deleted. Must be of the form
[_a-zA-Z0-9][-_.a-zA-Z0-9]\*
.column (bytes) – The column within the column family that will have a cell deleted.
time_range (
TimestampRange
) – (Optional) The range of time within which cells should be deleted.state (bool) – (Optional) The state that the mutation should be applied in. Defaults to
True
.
delete_cells(column_family_id, columns, time_range=None, state=True)
Deletes cells in this row.
NOTE: This method adds a mutation to the accumulated mutations on this
row, but does not make an API request. To actually
send an API request (with the mutations) to the Google Cloud
Bigtable API, call commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_1"
row_obj = table.row(row_key)
row_obj.delete_cells(COLUMN_FAMILY_ID, [COL_NAME1, COL_NAME2])
row_obj.commit()
Parameters
column_family_id (str) – The column family that contains the column or columns with cells being deleted. Must be of the form
[_a-zA-Z0-9][-_.a-zA-Z0-9]\*
.columns (
list
ofstr
/unicode
, orobject
) – The columns within the column family that will have cells deleted. IfALL_COLUMNS
is used then the entire column family will be deleted from the row.time_range (
TimestampRange
) – (Optional) The range of time within which cells should be deleted.state (bool) – (Optional) The state that the mutation should be applied in. Defaults to
True
.
property row_key()
Row key.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
row_key = row.row_key
Return type
Returns
The key for the current row.
set_cell(column_family_id, column, value, timestamp=None, state=True)
Sets a value in this row.
The cell is determined by the row_key
of this
ConditionalRow
and the column
. The column
must be in
an existing ColumnFamily
(as determined by
column_family_id
).
NOTE: This method adds a mutation to the accumulated mutations on this
row, but does not make an API request. To actually
send an API request (with the mutations) to the Google Cloud
Bigtable API, call commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
cell_val = b"cell-val"
row.set_cell(
COLUMN_FAMILY_ID, COL_NAME1, cell_val, timestamp=datetime.datetime.utcnow()
)
Parameters
column_family_id (str) – The column family that contains the column. Must be of the form
[_a-zA-Z0-9][-_.a-zA-Z0-9]\*
.column (bytes) – The column within the column family where the cell is located.
value (bytes or
int
) – The value to set in the cell. If an integer is used, will be interpreted as a 64-bit big-endian signed integer (8 bytes).timestamp (
datetime.datetime
) – (Optional) The timestamp of the operation.state (bool) – (Optional) The state that the mutation should be applied in. Defaults to
True
.
property table()
Row table.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
table1 = row.table
Return type
table:
Table
Returns
table: The table that owns the row.
class google.cloud.bigtable.row.DirectRow(row_key, table=None)
Bases: google.cloud.bigtable.row._SetDeleteRow
Google Cloud Bigtable Row for sending “direct” mutations.
These mutations directly set or delete cell contents:
set_cell()
delete()
delete_cell()
delete_cells()
These methods can be used directly:
>>> row = table.row(b'row-key1')
>>> row.set_cell(u'fam', b'col1', b'cell-val')
>>> row.delete_cell(u'fam', b'col2')
NOTE: A DirectRow
accumulates mutations locally via the
set_cell()
, delete()
, delete_cell()
and
delete_cells()
methods. To actually send these mutations to the
Google Cloud Bigtable API, you must call commit()
.
Parameters
row_key (bytes) – The key for the current row.
table (
Table
) – (Optional) The table that owns the row. This is used for the :meth: commit only. Alternatively, DirectRows can be persisted viamutate_rows()
.
clear()
Removes all currently accumulated mutations on the current row.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_1"
row_obj = table.row(row_key)
row_obj.set_cell(COLUMN_FAMILY_ID, COL_NAME1, b"cell-val")
row_obj.clear()
commit()
Makes a MutateRow
API request.
If no mutations have been created in the row, no request is made.
Mutations are applied atomically and in order, meaning that earlier mutations can be masked / negated by later ones. Cells already present in the row are left unchanged unless explicitly changed by a mutation.
After committing the accumulated mutations, resets the local mutations to an empty list.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_2"
cell_val = b"cell-val"
row_obj = table.row(row_key)
row_obj.set_cell(COLUMN_FAMILY_ID, COL_NAME1, cell_val)
row_obj.commit()
Return type
Status
Returns
A response status (google.rpc.status_pb2.Status) representing success or failure of the row committed.
Raises
TooManyMutationsError
if the number of mutations is greater than 100,000.
delete()
Deletes this row from the table.
NOTE: This method adds a mutation to the accumulated mutations on this
row, but does not make an API request. To actually
send an API request (with the mutations) to the Google Cloud
Bigtable API, call commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_1"
row_obj = table.row(row_key)
row_obj.delete()
row_obj.commit()
delete_cell(column_family_id, column, time_range=None)
Deletes cell in this row.
NOTE: This method adds a mutation to the accumulated mutations on this
row, but does not make an API request. To actually
send an API request (with the mutations) to the Google Cloud
Bigtable API, call commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_1"
row_obj = table.row(row_key)
row_obj.delete_cell(COLUMN_FAMILY_ID, COL_NAME1)
row_obj.commit()
Parameters
column_family_id (str) – The column family that contains the column or columns with cells being deleted. Must be of the form
[_a-zA-Z0-9][-_.a-zA-Z0-9]\*
.column (bytes) – The column within the column family that will have a cell deleted.
time_range (
TimestampRange
) – (Optional) The range of time within which cells should be deleted.
delete_cells(column_family_id, columns, time_range=None)
Deletes cells in this row.
NOTE: This method adds a mutation to the accumulated mutations on this
row, but does not make an API request. To actually
send an API request (with the mutations) to the Google Cloud
Bigtable API, call commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key = b"row_key_1"
row_obj = table.row(row_key)
row_obj.delete_cells(COLUMN_FAMILY_ID, [COL_NAME1, COL_NAME2])
row_obj.commit()
Parameters
column_family_id (str) – The column family that contains the column or columns with cells being deleted. Must be of the form
[_a-zA-Z0-9][-_.a-zA-Z0-9]\*
.columns (
list
ofstr
/unicode
, orobject
) – The columns within the column family that will have cells deleted. IfALL_COLUMNS
is used then the entire column family will be deleted from the row.time_range (
TimestampRange
) – (Optional) The range of time within which cells should be deleted.
get_mutations_size()
Gets the total mutations size for current row
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row_key_id = b"row_key_1"
row_obj = table.row(row_key_id)
mutation_size = row_obj.get_mutations_size()
property row_key()
Row key.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
row_key = row.row_key
Return type
Returns
The key for the current row.
set_cell(column_family_id, column, value, timestamp=None)
Sets a value in this row.
The cell is determined by the row_key
of this DirectRow
and the column
. The column
must be in an existing
ColumnFamily
(as determined by column_family_id
).
NOTE: This method adds a mutation to the accumulated mutations on this
row, but does not make an API request. To actually
send an API request (with the mutations) to the Google Cloud
Bigtable API, call commit()
.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
cell_val = b"cell-val"
row.set_cell(
COLUMN_FAMILY_ID, COL_NAME1, cell_val, timestamp=datetime.datetime.utcnow()
)
Parameters
column_family_id (str) – The column family that contains the column. Must be of the form
[_a-zA-Z0-9][-_.a-zA-Z0-9]\*
.column (bytes) – The column within the column family where the cell is located.
value (bytes or
int
) – The value to set in the cell. If an integer is used, will be interpreted as a 64-bit big-endian signed integer (8 bytes).timestamp (
datetime.datetime
) – (Optional) The timestamp of the operation.
property table()
Row table.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
table1 = row.table
Return type
table:
Table
Returns
table: The table that owns the row.
google.cloud.bigtable.row.MAX_MUTATIONS( = 10000 )
The maximum number of mutations that a row can accumulate.
class google.cloud.bigtable.row.Row(row_key, table=None)
Bases: object
Base representation of a Google Cloud Bigtable Row.
This class has three subclasses corresponding to the three RPC methods for sending row mutations:
DirectRow
forMutateRow
ConditionalRow
forCheckAndMutateRow
AppendRow
forReadModifyWriteRow
Parameters
property row_key()
Row key.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
row_key = row.row_key
Return type
Returns
The key for the current row.
property table()
Row table.
For example:
from google.cloud.bigtable import Client
client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)
row = table.row(ROW_KEY1)
table1 = row.table
Return type
table:
Table
Returns
table: The table that owns the row.