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.



80
81
82
# File 'lib/google/cloud/bigquery/insert_response.rb', line 80

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.



103
104
105
106
107
# File 'lib/google/cloud/bigquery/insert_response.rb', line 103

def error_rows
  Array(@gapi.insert_errors).map do |ie|
    @rows[ie.index]
  end
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.



131
132
133
134
135
# File 'lib/google/cloud/bigquery/insert_response.rb', line 131

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.



145
146
147
148
149
# File 'lib/google/cloud/bigquery/insert_response.rb', line 145

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.



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

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.



117
118
119
# File 'lib/google/cloud/bigquery/insert_response.rb', line 117

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.



89
90
91
92
93
94
95
# File 'lib/google/cloud/bigquery/insert_response.rb', line 89

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