Class: Google::Cloud::Bigquery::InsertResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/bigquery/insert_response.rb

Overview

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.

Examples:

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

See Also:

Defined Under Namespace

Classes: InsertError

Instance Method Summary collapse

Instance Method Details

#error_countInteger

The count of errors for rows that were not inserted.

Returns:

  • (Integer)

    The number of errors.



78
79
80
# File 'lib/google/cloud/bigquery/insert_response.rb', line 78

def error_count
  Array(@gapi.insert_errors).count
end

#error_rowsArray<Hash>

The rows that were not inserted.

Returns:

  • (Array<Hash>)

    An array of hash objects containing the row data.



101
102
103
# 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) ⇒ Array<Hash>?

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

Parameters:

  • row (Hash)

    A hash containing the data for a row.

Returns:

  • (Array<Hash>, nil)

    An array of error hashes, or nil if no errors are found in the response for the row.



127
128
129
130
131
# 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) ⇒ Integer?

Returns the index for a row that was not inserted.

Parameters:

  • row (Hash)

    A hash containing the data for a row.

Returns:

  • (Integer, nil)

    An error object, or nil if no error is found in the response for the row.



141
142
143
144
145
# 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_countInteger

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

Returns:

  • (Integer)

    The number of rows inserted.



69
70
71
# File 'lib/google/cloud/bigquery/insert_response.rb', line 69

def insert_count
  @rows.count - error_count
end

#insert_error_for(row) ⇒ InsertError?

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

Parameters:

  • row (Hash)

    A hash containing the data for a row.

Returns:

  • (InsertError, nil)

    An error object, or nil if no error is found in the response for the row.



113
114
115
# File 'lib/google/cloud/bigquery/insert_response.rb', line 113

def insert_error_for row
  insert_errors.detect { |e| e.row == row }
end

#insert_errorsArray<InsertError>

The error objects for rows that were not inserted.

Returns:

  • (Array<InsertError>)

    An array containing error objects.



87
88
89
90
91
92
93
# 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?Boolean

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

Returns:

  • (Boolean)

    true when the error count is zero, false otherwise.



59
60
61
# File 'lib/google/cloud/bigquery/insert_response.rb', line 59

def success?
  error_count.zero?
end