diff --git a/lib/big_query/client.rb b/lib/big_query/client.rb index d3723eb..bb076e7 100644 --- a/lib/big_query/client.rb +++ b/lib/big_query/client.rb @@ -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 diff --git a/lib/big_query/client/tables.rb b/lib/big_query/client/tables.rb index 1b97691..ef01f32 100644 --- a/lib/big_query/client/tables.rb +++ b/lib/big_query/client/tables.rb @@ -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 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 diff --git a/test/bigquery.rb b/test/bigquery.rb index da44aa0..3cbc7f3 100644 --- a/test/bigquery.rb +++ b/test/bigquery.rb @@ -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"