class Google::Cloud::Bigquery::InsertResponse

InsertResponse

Represents the response from BigQuery when data is inserted into a table for near-immediate querying, without the need to complete a load operation before the data can appear in query results. See {Dataset#insert} and {Table#insert}.

@see cloud.google.com/bigquery/streaming-data-into-bigquery

Streaming Data Into BigQuery

@example

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"

rows = [
  { "first_name" => "Alice", "age" => 21 },
  { "first_name" => "Bob", "age" => 22 }
]

insert_response = dataset.insert "my_table", rows

Public Class Methods

from_gapi(rows, gapi) click to toggle source

@private New InsertResponse from the inserted rows and a Google::Apis::BigqueryV2::InsertAllTableDataResponse object.

# File lib/google/cloud/bigquery/insert_response.rb, line 149
def self.from_gapi rows, gapi
  new rows, gapi
end
new(rows, gapi) click to toggle source

@private

# File lib/google/cloud/bigquery/insert_response.rb, line 47
def initialize rows, gapi
  @rows = rows
  @gapi = gapi
end

Public Instance Methods

error_count() click to toggle source

The count of errors for rows that were not inserted.

@return [Integer] The number of errors.

# File lib/google/cloud/bigquery/insert_response.rb, line 78
def error_count
  Array(@gapi.insert_errors).count
end
error_rows() click to toggle source

The rows that were not inserted.

@return [Array<Hash>] An array of hash objects containing the row

data.
# File lib/google/cloud/bigquery/insert_response.rb, line 101
def error_rows
  Array(@gapi.insert_errors).map { |ie| @rows[ie.index] }
end
errors_for(row) click to toggle source

Returns the error hashes for a row that was not inserted. Each error hash contains the following keys: `reason`, `location`, `debugInfo`, and `message`.

@param [Hash] row A hash containing the data for a row.

@return [Array<Hash>, nil] An array of error hashes, or `nil` if no

errors are found in the response for the row.
# File lib/google/cloud/bigquery/insert_response.rb, line 127
def errors_for row
  ie = insert_error_for row
  return ie.errors if ie
  []
end
index_for(row) click to toggle source

Returns the index for a row that was not inserted.

@param [Hash] row A hash containing the data for a row.

@return [Integer, nil] An error object, or `nil` if no error is

found in the response for the row.
# File lib/google/cloud/bigquery/insert_response.rb, line 141
def index_for row
  ie = insert_error_for row
  return ie.index if ie
  nil
end
insert_count() click to toggle source

The count of rows in the response, minus the count of errors for rows that were not inserted.

@return [Integer] The number of rows inserted.

# File lib/google/cloud/bigquery/insert_response.rb, line 69
def insert_count
  @rows.count - error_count
end
insert_error_for(row) click to toggle source

Returns the error object for a row that was not inserted.

@param [Hash] row A hash containing the data for a row.

@return [InsertError, nil] An error object, or `nil` if no error is

found in the response for the row.
# File lib/google/cloud/bigquery/insert_response.rb, line 113
def insert_error_for row
  insert_errors.detect { |e| e.row == row }
end
insert_errors() click to toggle source

The error objects for rows that were not inserted.

@return [Array<InsertError>] An array containing error objects.

# File lib/google/cloud/bigquery/insert_response.rb, line 87
def insert_errors
  Array(@gapi.insert_errors).map do |ie|
    row = @rows[ie.index]
    errors = ie.errors.map { |e| JSON.parse e.to_json }
    InsertError.new ie.index, row, errors
  end
end
success?() click to toggle source

Checks if the error count is zero, meaning that all of the rows were inserted. Use {#insert_errors} to access the errors.

@return [Boolean] `true` when the error count is zero, `false`

otherwise.
# File lib/google/cloud/bigquery/insert_response.rb, line 59
def success?
  error_count.zero?
end