# Cloud Spanner Client - Class BatchClient (2.9.0)

2.9.0 (latest) 2.8.1 2.7.0 2.6.1 2.5.1 2.4.1 2.3.0 2.2.0 2.1.0 2.0.1-RC1 1.106.0 1.105.1 1.104.1 1.103.0 1.102.0 1.101.0 1.100.0 1.98.0 1.97.0 1.96.0 1.95.0 1.94.0 1.93.1 1.92.1 1.91.0 1.90.0 1.89.0 1.88.0 1.87.0 1.86.0 1.85.0 1.84.0 1.83.0 1.82.0 1.81.0 1.80.0 1.79.0 1.78.0 1.77.0 1.76.1 1.68.0 1.67.0 1.66.0 1.65.0 1.64.0 1.63.2 1.62.1 1.61.0 1.60.0 1.59.0 1.58.4 1.57.0 1.56.0 1.55.0 1.54.2 Reference documentation and code samples for the Cloud Spanner Client class BatchClient.

Provides Batch APIs used to read data from a Cloud Spanner database.

Batch Clients are useful when one wants to read or query a large amount of
data from Cloud Spanner across multiple processes, even across multiple
machines. It allows creation of partitions of a Cloud Spanner database in
order to facilitate reading or querying of each partition independently at
the same snapshot.

Example:

    use Google\Cloud\Spanner\SpannerClient;

    $spanner = new SpannerClient(['projectId' => 'my-project']);
    $batch = $spanner->batch('instance-id', 'database-id');

    // Using Cloud Pub/Sub to share partitions with workers
    use Google\Cloud\PubSub\PubSubClient;

    $pubsub = new PubSubClient();
    $topic = $pubsub->topic('partition-queries');

    $snapshot = $batch->snapshot();

    // Executing a partition query will return a list of Partitions.
    $partitions = $snapshot->partitionQuery('SELECT * FROM Users WHERE firstName = %s AND location = %s', [
        'parameters' => [
            'firstName' => 'John',
            'location' => 'USA'
        ]
    ]);

    // Each partition is published to Cloud Pub/Sub, where it can be executed by
    // a worker.
    foreach ($partitions as $partition) {
        $topic->publish([
            'attributes' => [
                'snapshot' => $snapshot->serialize(),
                'partition' => $partition->serialize()
            ]
        ]);
    }

    // Once all workers have finished, we will close the snapshot.
    // The logic to determine whether the snapshot is no longer needed will vary
    // and is not implemented here.
    do {
        $finished = areWorkersDone();
    } while(!$finished);

    // Using Cloud Pub/Sub to consume a partition and return a result.
    use Google\Cloud\PubSub\PubSubClient;

    $pubsub = new PubSubClient();
    $subscription = $pubsub->subscription('partition-query-consumer');

    $messages = $subscription->pull([
        'returnImmediately' => true,
        'maxMessages' => 1
    ]);

    if ($messages) {
        $message = $messages[0];
        $snapshot = $batch->snapshotFromString($message->attribute('snapshot'));
        $partition = $batch->partitionFromString($message->attribute('partition'));

        // Do something with the query result.
        processResult($snapshot->executePartition($partition));
    }

## Namespace

Google \\ Cloud \\ Spanner \\ Batch

## Methods

### __construct

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `operation` | `Google\Cloud\Spanner\Operation` A Cloud Spanner Operations wrapper. |
| `session` | `Google\Cloud\Spanner\Session\SessionCache` The session to which the batch client instance is scoped. |

### snapshot

Create a batch snapshot.

Example:

    $snapshot = $batch->snapshot();

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `options` | `array` Configuration Options See [ReadOnly](https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.TransactionOptions.ReadOnly) for detailed description of available options. |
| `↳ transactionOptions.strong` | `bool` Read at a timestamp where all previously committed transactions are visible. |
| `↳ transactionOptions.readTimestamp` | `https://docs.cloud.google.com/php/docs/reference/cloud-spanner/latest/Timestamp` Executes all reads at the given timestamp. |
| `↳ transactionOptions.exactStaleness` | `Duration` Represents a number of seconds. Executes all reads at a timestamp that is $exactStaleness old. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-spanner/latest/Batch.BatchSnapshot` |   |

### snapshotFromString

Create a [BatchSnapshot](https://docs.cloud.google.com/php/docs/reference/cloud-spanner/latest/Batch.BatchSnapshot) from a snapshot
identifier.

This method can be used to deserialize a snapshot which is
shared across multiple servers or processes.

Example:

    $snapshot = $batch->snapshotFromString($snapshotString);

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `identifier` | `string` A stringified representation of [BatchSnapshot](https://docs.cloud.google.com/php/docs/reference/cloud-spanner/latest/Batch.BatchSnapshot). |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-spanner/latest/Batch.BatchSnapshot` |   |

### partitionFromString

Create a [PartitionInterface](https://docs.cloud.google.com/php/docs/reference/cloud-spanner/latest/Batch.PartitionInterface) instance.

This method can be used to deserialize a partition which is
shared across multiple servers or processes.

Example:

    $partition = $batch->partitionFromString($partitionString);

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `partition` | `string` Partition data |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/php/docs/reference/cloud-spanner/latest/Batch.PartitionInterface` |   |

## Constants

### PARTITION_TYPE_KEY

    Value: '__partitionTypeName'