spanner_batch_client project_id:, instance_id:, database_id:

in spanner/spanner_samples.rb [1017:1073]


def spanner_batch_client project_id:, instance_id:, database_id:
  
  
  
  

  require "google/cloud/spanner"

  
  processor_count  = Concurrent.processor_count
  thread_pool      = Concurrent::FixedThreadPool.new processor_count

  
  total_records = Concurrent::AtomicFixnum.new

  
  spanner        = Google::Cloud::Spanner.new project: project_id
  batch_client   = spanner.batch_client instance_id, database_id

  
  batch_snapshot = batch_client.batch_snapshot strong: true

  
  
  
  partitions       = batch_snapshot.partition_query "SELECT SingerId, FirstName, LastName FROM Singers", data_boost_enabled: true
  total_partitions = partitions.size

  
  partitions.each_with_index do |partition, _partition_index|
    thread_pool.post do
      
      batch_snapshot.execute_partition(partition).rows.each do |_row|
        total_records.increment
      end
    end
  end

  
  thread_pool.shutdown
  thread_pool.wait_for_termination

  
  batch_snapshot.close

  
  average_records_per_partition = 0.0
  if total_partitions != 0
    average_records_per_partition = total_records.value / total_partitions.to_f
  end

  puts "Total Partitions: #{total_partitions}"
  puts "Total Records: #{total_records.value}"
  puts "Average records per Partition: #{average_records_per_partition}"
  
end