Skip to content
This repository was archived by the owner on Jun 12, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/big_query/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def refresh_auth
private

def api(resp)
resp = nil if resp == ''
data = deep_stringify_keys(resp.to_h)
handle_error(data) if data && is_error?(data)
data
Expand Down
32 changes: 32 additions & 0 deletions lib/big_query/client/tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ def insert(table_id, opts)
)
end

# insert multiple row into table with extra options
#
# @param table_id [String] table id to insert into
# @param data [Array] array of hashes, or array of <hash, insert_id> arrays
# @param opts [Hash] extra options
# @return [Hash]
def insert_all(table_id, data, opts = {})
rows = data.map do |record|
row = Google::Apis::BigqueryV2::InsertAllTableDataRequest::Row.new

if record.class == Hash
row.json = record
elsif record.class == Array
row.json = record[0]
row.insert_id = record[1]
end

row
end

request = Google::Apis::BigqueryV2::InsertAllTableDataRequest.new({rows: rows}.merge(opts))

api(
@client.insert_all_table_data(
@project_id,
@dataset,
table_id,
request
)
)
end

# Creating a new table
#
# @param tableId [String] table id to insert into
Expand Down
14 changes: 13 additions & 1 deletion test/bigquery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,19 @@ def test_for_insert_array
{"id" => 321, "type" => "Other task"}
]

result = @bq.insert('test' , data)
result = @bq.insert('test', data)

assert_equal result['kind'], "bigquery#tableDataInsertAllResponse"
end

def test_for_insert_array_with_insert_id
data = [
[{"id" => 123, "type" => "Task"}, '12345'],
{"id" => 456, "type" => "Task 2"},
[{"id" => 321, "type" => "Other task"}, '67890']
]

result = @bq.insert_all('test', data, skip_invalid_rows: true, ignore_unknown_values: false)

assert_equal result['kind'], "bigquery#tableDataInsertAllResponse"

Expand Down