From 7167a8d516d7309832299a082cdc3e186f5021e6 Mon Sep 17 00:00:00 2001 From: Nash Yeung Date: Thu, 10 Mar 2016 21:05:05 +0800 Subject: [PATCH 1/2] Support insertId and other optional params in Tabledata.insertAll --- lib/big_query/client/tables.rb | 32 ++++++++++++++++++++++++++++++++ test/bigquery.rb | 14 +++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) 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" From 19738cdd6038da66bac8df59f01e88927e5bf1bf Mon Sep 17 00:00:00 2001 From: Nash Yeung Date: Wed, 4 May 2016 15:13:22 +0800 Subject: [PATCH 2/2] handle empty response body peacefully In VCR, empty response body may be converted to an empty string, which raises error when we call resp.to_h. Check the response for nil and empty string, and converts it to an empty hash manually. --- lib/big_query/client.rb | 1 + 1 file changed, 1 insertion(+) 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