# Cloud Spanner API - Class Google::Cloud::Spanner::BatchClient (v2.36.0)

2.36.0 (latest) 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.0 2.24.0 2.23.0 2.22.0 2.21.0 2.20.0 2.19.1 2.18.1 2.17.0 2.16.1 2.15.0 2.14.0 2.13.0 2.12.1 2.11.0 Reference documentation and code samples for the Cloud Spanner API class Google::Cloud::Spanner::BatchClient.

## BatchClient

Provides a batch client that can be used to read data from a Cloud
Spanner database. An instance of this class is tied to a specific
database.

BatchClient is useful when one wants to read or query a large amount of
data from Cloud Spanner across multiple processes, even across different
machines. It allows to create partitions of Cloud Spanner database and
then read or query over each partition independently yet at the same
snapshot.

See [Project#batch_client](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Project#Google__Cloud__Spanner__Project_batch_client_instance_ "Google::Cloud::Spanner::Project#batch_client (method)").

## Inherits

- Object

## Example

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first

serialized_snapshot = batch_snapshot.dump
serialized_partition = partition.dump

# In a separate process
new_batch_snapshot = batch_client.load_batch_snapshot \
  serialized_snapshot

new_partition = batch_client.load_partition \
  serialized_partition

results = new_batch_snapshot.execute_partition \
  new_partition
```

## Methods

### #batch_snapshot

    def batch_snapshot(strong: nil, timestamp: nil, read_timestamp: nil, staleness: nil, exact_staleness: nil) { |snapshot| ... } -> Google::Cloud::Spanner::BatchSnapshot

Returns a [BatchSnapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot "Google::Cloud::Spanner::BatchSnapshot (class)") context in which multiple reads and/or
queries can be performed. All reads/queries will use the same
timestamp, and the timestamp can be inspected after this transaction
is created successfully. This is a blocking method since it waits to
finish the RPCs.
**Parameters**

- **strong** (true, false) *(defaults to: nil)* --- Read at a timestamp where all previously committed transactions are visible.
- **timestamp** (Time, DateTime) *(defaults to: nil)* --- Executes all reads at the given timestamp. Unlike other modes, reads at a specific timestamp are repeatable; the same read at the same timestamp always returns the same data. If the timestamp is in the future, the read will block until the specified timestamp, modulo the read's deadline.


  Useful for large scale consistent reads such as mapreduces, or for
  coordinating many reads against a consistent snapshot of the data.
  (See
  [TransactionOptions](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#transactionoptions).)
- **read_timestamp** (Time, DateTime) *(defaults to: nil)* --- Same as `timestamp`.
- **staleness** (Numeric) *(defaults to: nil)* --- Executes all reads at a timestamp that is `staleness` seconds old. For example, the number 10.1 is translated to 10 seconds and 100 milliseconds.


  Guarantees that all writes that have committed more than the
  specified number of seconds ago are visible. Because Cloud Spanner
  chooses the exact timestamp, this mode works even if the client's
  local clock is substantially skewed from Cloud Spanner commit
  timestamps.

  Useful for reading at nearby replicas without the distributed
  timestamp negotiation overhead of single-use `staleness`. (See
  [TransactionOptions](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#transactionoptions).)
- **exact_staleness** (Numeric) *(defaults to: nil)* --- Same as `staleness`.
**Yields**

- (snapshot) --- The block for reading and writing data.
**Yield Parameter**

- **snapshot** ([Google::Cloud::Spanner::Snapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Snapshot)) --- The Snapshot object.
**Returns**

- ([Google::Cloud::Spanner::BatchSnapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot))
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first

serialized_snapshot = batch_snapshot.dump
serialized_partition = partition.dump

# In a separate process
new_batch_snapshot = batch_client.load_batch_snapshot \
  serialized_snapshot

new_partition = batch_client.load_partition \
  serialized_partition

results = new_batch_snapshot.execute_partition \
  new_partition
```

### #database

    def database() -> ::Google::Cloud::Spanner::Database

The Spanner database connected to.
**Returns**

- ([::Google::Cloud::Spanner::Database](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Database))

### #database_id

    def database_id() -> String

The unique identifier for the database.
**Returns**

- (String)

### #directed_read_options

    def directed_read_options() -> Hash

A hash of values to specify the custom directed read options for executing
SQL query.
**Returns**

- (Hash)

### #fields

    def fields(types) -> Fields

Creates a configuration object ([Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)")) that may be provided to
queries or used to create STRUCT objects. (The STRUCT will be
represented by the [Data](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Data "Google::Cloud::Spanner::Data (class)") class.) See [Client#execute](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Client#Google__Cloud__Spanner__Client_execute_instance_ "Google::Cloud::Spanner::Client#execute (method)") and/or
[Fields#struct](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields#Google__Cloud__Spanner__Fields_struct_instance_ "Google::Cloud::Spanner::Fields#struct (method)").


<br />

For more information, see [Data Types - Constructing a
STRUCT](https://cloud.google.com/spanner/docs/data-types#constructing-a-struct).
**Parameter**

- **types** (Array, Hash) ---

  Accepts an array or hash types.

  Arrays can contain just the type value, or a sub-array of the
  field's name and type value. Hash keys must contain the field name
  as a `Symbol` or `String`, or the field position as an `Integer`.
  Hash values must contain the type value. If a Hash is used the
  fields will be created using the same order as the Hash keys.

  Supported type values incude:
  - `:BOOL`
  - `:BYTES`
  - `:DATE`
  - `:FLOAT64`
  - `:FLOAT32`
  - `:INT64`
  - `:STRING`
  - `:TIMESTAMP`
  - `:UUID`
  - `Array` - Lists are specified by providing the type code in an array. For example, an array of integers are specified as `[:INT64]`.
  - [Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields "Google::Cloud::Spanner::Fields (class)") - Nested Structs are specified by providing a Fields object.
**Returns**

- ([Fields](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Fields)) --- The fields of the given types.
**Examples**

Create a STRUCT value with named fields using Fields object:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

named_type = batch_client.fields(
  { id: :INT64, name: :STRING, active: :BOOL }
)
named_data = named_type.struct(
  { id: 42, name: nil, active: false }
)
```

Create a STRUCT value with anonymous field names:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

anon_type = batch_client.fields [:INT64, :STRING, :BOOL]
anon_data = anon_type.struct [42, nil, false]
```

Create a STRUCT value with duplicate field names:

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

dup_type = batch_client.fields(
  [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
)
dup_data = dup_type.struct [42, nil, false]
```

### #instance

    def instance() -> ::Google::Cloud::Spanner::Instance

The Spanner instance connected to.
**Returns**

- ([::Google::Cloud::Spanner::Instance](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance))

### #instance_id

    def instance_id() -> String

The unique identifier for the instance.
**Returns**

- (String)

### #load_batch_snapshot

    def load_batch_snapshot(serialized_snapshot) -> Google::Cloud::Spanner::BatchSnapshot

Returns a [BatchSnapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot "Google::Cloud::Spanner::BatchSnapshot (class)") context in which multiple reads and/or
queries can be performed. All reads/queries will use the same
timestamp, and the timestamp can be inspected after this transaction
is created successfully. This method does not perform an RPC.
**Parameter**

- **serialized_snapshot** (String) --- The serialized representation of an existing batch snapshot. See [Google::Cloud::Spanner::BatchSnapshot#dump](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot#Google__Cloud__Spanner__BatchSnapshot_dump_instance_ "Google::Cloud::Spanner::BatchSnapshot#dump (method)").
**Returns**

- ([Google::Cloud::Spanner::BatchSnapshot](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-BatchSnapshot))
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first

serialized_snapshot = batch_snapshot.dump
serialized_partition = partition.dump

# In a separate process
new_batch_snapshot = batch_client.load_batch_snapshot \
  serialized_snapshot

new_partition = batch_client.load_partition \
  serialized_partition

results = new_batch_snapshot.execute_partition \
  new_partition
```

### #load_partition

    def load_partition(serialized_partition) -> Google::Cloud::Spanner::Partition

Returns a [Partition](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Partition "Google::Cloud::Spanner::Partition (class)") from a serialized representation. See
[Partition.load](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Partition#Google__Cloud__Spanner__Partition_load_class_ "Google::Cloud::Spanner::Partition.load (method)").
**Parameter**

- **serialized_partition** (String) --- The serialized representation of an existing batch partition. See [Partition#dump](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Partition#Google__Cloud__Spanner__Partition_dump_instance_ "Google::Cloud::Spanner::Partition#dump (method)").
**Returns**

- ([Google::Cloud::Spanner::Partition](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Partition))
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"

batch_snapshot = batch_client.batch_snapshot

partitions = batch_snapshot.partition_read "users", [:id, :name]

partition = partitions.first

serialized_snapshot = batch_snapshot.dump
serialized_partition = partition.dump

# In a separate process
new_batch_snapshot = batch_client.load_batch_snapshot \
  serialized_snapshot

new_partition = batch_client.load_partition \
  serialized_partition

results = new_batch_snapshot.execute_partition \
  new_partition
```

### #project

    def project() -> Project

The Spanner project connected to.
**Returns**

- ([Project](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Project))

### #project_id

    def project_id() -> String

The unique identifier for the project.
**Returns**

- (String)

### #range

    def range(beginning, ending, exclude_begin: false, exclude_end: false) -> Google::Cloud::Spanner::Range

Creates a Spanner Range. This can be used in place of a Ruby Range
when needing to exclude the beginning value.
**Parameters**

- **beginning** (Object) --- The object that defines the beginning of the range.
- **ending** (Object) --- The object that defines the end of the range.
- **exclude_begin** (Boolean) *(defaults to: false)* --- Determines if the range excludes its beginning value. Default is `false`.
- **exclude_end** (Boolean) *(defaults to: false)* --- Determines if the range excludes its ending value. Default is `false`.
**Returns**

- ([Google::Cloud::Spanner::Range](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Range)) --- The new Range instance.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

batch_client = spanner.batch_client "my-instance", "my-database"
batch_snapshot = batch_client.batch_snapshot

key_range = batch_client.range 1, 100

partitions = batch_snapshot.partition_read "users", [:id, :name],
                                        keys: key_range
```