2.42.0 (latest) 2.41.0 2.40.0 2.39.0 2.38.0 2.37.0 2.36.0 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.1 2.29.0 2.28.1 2.26.0 2.25.0 2.24.0 2.23.1 2.22.0 2.21.0 2.20.0 2.19.0 2.18.1 2.17.0 2.16.0 2.15.0 2.14.1 2.13.2 2.12.0 2.11.3 2.10.1 2.9.0 2.8.1 2.7.1 2.6.0 2.5.2 2.4.0 2.3.3 2.2.0 2.1.0 2.0.0 1.7.3 1.6.1 1.5.1 1.4.0 1.3.0 1.2.1 1.1.0 1.0.0 0.34.0

    MutationsBatcher(
        table,
        flush_count=100,
        max_row_bytes=20971520,
        flush_interval=1,
        batch_completed_callback=None,
    )

A MutationsBatcher is used in batch cases where the number of mutations
is large or unknown. It will store `DirectRow` in memory until one of the
size limits is reached, or an explicit call to `flush()` is performed. When
a flush event occurs, the `DirectRow` in memory will be sent to Cloud
Bigtable. Batching mutations is more efficient than sending individual
request.

This class is not suited for usage in systems where each mutation
must be guaranteed to be sent, since calling `mutate()` may only
result in an in-memory change. Rows are only sent to the service when a size
limit is reached, when `flush()` is called explicitly, or when the
batcher is closed (`close()` is also registered to run at interpreter
exit). There is no time-based background flush. As a result, if the process
terminates abruptly -- e.g. a crash, `SIGKILL`, or `os._exit` where the
`atexit` handler never runs -- any `DirectRow` still buffered in
memory is silently dropped and never sent, even after `mutate()`
returned.

Note on thread safety: The same `MutationBatcher` cannot be shared by multiple end-user threads.

| ## Parameters ||
|---|---|
| **Name** | **Description** |
| `table` | `class` class:`https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable.table.Table`. |
| `flush_count` | `int` (Optional) Max number of rows to flush. If it reaches the max number of rows it calls finish_batch() to mutate the current row batch. Default is FLUSH_COUNT (1000 rows). |
| `max_row_bytes` | `int` (Optional) Max number of row mutations size to flush. If it reaches the max number of row mutations size it calls finish_batch() to mutate the current row batch. Default is MAX_ROW_BYTES (5 MB). |
| `flush_interval` | `float` (Deprecated) No longer used. Retained only for backwards compatibility. There is no time-based background flush; see the class docstring for when rows are sent. |
| `batch_completed_callback` | ``Callable[list:[`google.rpc.status_pb2.Status`]] = None`` (Optional) A callable for handling responses after the current batch is sent. The callable function expect a list of grpc Status. |

## Methods

### __enter__

    __enter__()

Starting the MutationsBatcher as a context manager

### __exit__

    __exit__(exc_type, exc_value, exc_traceback)

Clean up resources. Flush and shutdown the ThreadPoolExecutor.

### close

    close()

Clean up resources. Flush and shutdown the ThreadPoolExecutor.
Any errors will be raised.

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
|   | `.batcherMutationsBatchError` if there's any error in the mutations. |

### flush

    flush()

Sends the current batch to Cloud Bigtable synchronously.
For example:

.. literalinclude:: snippets_table.py
:start-after: \[START bigtable_api_batcher_flush\]
:end-before: \[END bigtable_api_batcher_flush\]
:dedent: 4

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
|   | `.batcherMutationsBatchError` if there's any error in the mutations. |

### mutate

    mutate(row)

Add a row to the batch. If the current batch meets one of the size
limits, the batch is sent asynchronously.

For example:

.. literalinclude:: snippets_table.py
:start-after: \[START bigtable_api_batcher_mutate\]
:end-before: \[END bigtable_api_batcher_mutate\]
:dedent: 4

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `row` | `class` [DirectRow](https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable.row.DirectRow). |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `On` | of the following: \* `.table._BigtableRetryableError` if any row returned a transient error. \* `RuntimeError` if the number of responses doesn't match the number of rows that were retried |

### mutate_rows

    mutate_rows(rows)

Add multiple rows to the batch. If the current batch meets one of the size
limits, the batch is sent asynchronously.

For example:

.. literalinclude:: snippets_table.py
:start-after: \[START bigtable_api_batcher_mutate_rows\]
:end-before: \[END bigtable_api_batcher_mutate_rows\]
:dedent: 4

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `rows` | ``list:[`https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable.row.DirectRow`]`` list:\[`https://docs.cloud.google.com/python/docs/reference/bigtable/latest/google.cloud.bigtable.row.DirectRow`\]. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `On` | of the following: \* `.table._BigtableRetryableError` if any row returned a transient error. \* `RuntimeError` if the number of responses doesn't match the number of rows that were retried |