Class: Google::Cloud::Spanner::BatchClient

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/spanner/batch_client.rb

Overview

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.

Examples:

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

Instance Method Summary collapse

Instance Method Details

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

Returns a Google::Cloud::Spanner::BatchSnapshot 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.

Examples:

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

Parameters:

  • strong (true, false)

    Read at a timestamp where all previously committed transactions are visible.

  • timestamp (Time, DateTime)

    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.)

  • read_timestamp (Time, DateTime)

    Same as timestamp.

  • staleness (Numeric)

    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.)

  • exact_staleness (Numeric)

    Same as staleness.

Yields:

  • (snapshot)

    The block for reading and writing data.

Yield Parameters:

Returns:



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/google/cloud/spanner/batch_client.rb', line 177

def batch_snapshot strong: nil, timestamp: nil, read_timestamp: nil,
                   staleness: nil, exact_staleness: nil
  validate_snapshot_args! strong: strong, timestamp: timestamp,
                          read_timestamp: read_timestamp,
                          staleness: staleness,
                          exact_staleness: exact_staleness

  ensure_service!
  snp_session = session
  snp_grpc = @project.service.create_snapshot \
    snp_session.path, strong: strong,
                      timestamp: (timestamp || read_timestamp),
                      staleness: (staleness || exact_staleness)
  BatchSnapshot.from_grpc snp_grpc, snp_session
end

#databaseDatabase

The Spanner database connected to.

Returns:



106
107
108
# File 'lib/google/cloud/spanner/batch_client.rb', line 106

def database
  @project.database instance_id, database_id
end

#database_idString

The unique identifier for the database.

Returns:

  • (String)


88
89
90
# File 'lib/google/cloud/spanner/batch_client.rb', line 88

def database_id
  @database_id
end

#instanceInstance

The Spanner instance connected to.

Returns:



100
101
102
# File 'lib/google/cloud/spanner/batch_client.rb', line 100

def instance
  @project.instance instance_id
end

#instance_idString

The unique identifier for the instance.

Returns:

  • (String)


82
83
84
# File 'lib/google/cloud/spanner/batch_client.rb', line 82

def instance_id
  @instance_id
end

#load_batch_snapshot(serialized_snapshot) ⇒ Google::Cloud::Spanner::BatchSnapshot

Returns a Google::Cloud::Spanner::BatchSnapshot 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.

Examples:

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

Parameters:

Returns:



230
231
232
233
234
# File 'lib/google/cloud/spanner/batch_client.rb', line 230

def load_batch_snapshot serialized_snapshot
  ensure_service!

  BatchSnapshot.load serialized_snapshot, service: @project.service
end

#load_partition(serialized_partition) ⇒ Google::Cloud::Spanner::Partition

Returns a Partition from a serialized representation. See Partition.load.

Examples:

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

Parameters:

  • serialized_partition (String)

    The serialized representation of an existing batch partition. See Partition#dump.

Returns:



271
272
273
# File 'lib/google/cloud/spanner/batch_client.rb', line 271

def load_partition serialized_partition
  Partition.load serialized_partition
end

#projectProject

The Spanner project connected to.

Returns:



94
95
96
# File 'lib/google/cloud/spanner/batch_client.rb', line 94

def project
  @project
end

#project_idString

The unique identifier for the project.

Returns:

  • (String)


76
77
78
# File 'lib/google/cloud/spanner/batch_client.rb', line 76

def project_id
  @project.service.project
end

#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.

Examples:

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

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)

    Determines if the range excludes its beginning value. Default is false.

  • exclude_end (Boolean)

    Determines if the range excludes its ending value. Default is false.

Returns:



302
303
304
305
306
# File 'lib/google/cloud/spanner/batch_client.rb', line 302

def range beginning, ending, exclude_begin: false, exclude_end: false
  Range.new beginning, ending,
            exclude_begin: exclude_begin,
            exclude_end: exclude_end
end