From a12400fbc5facaec4c102d14d14d5ef8edd26f93 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Fri, 20 Nov 2015 22:21:50 +0100 Subject: [PATCH 1/8] Update tables.rb Fixed typo. --- lib/big_query/client/tables.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/big_query/client/tables.rb b/lib/big_query/client/tables.rb index 03ce151..34f87d7 100644 --- a/lib/big_query/client/tables.rb +++ b/lib/big_query/client/tables.rb @@ -104,7 +104,7 @@ def describe_table(tableId, dataset = @dataset) api( api_method: @bq.tables.get, parameters: { 'tableId' => tableId, - 'datasetId' => @dataset } + 'datasetId' => dataset } ) end From c9dc704f4ede4445d228eed385dcf481395c6375 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Fri, 20 Nov 2015 22:49:37 +0100 Subject: [PATCH 2/8] Update tables.rb Removed the prop schema handling... --- lib/big_query/client/tables.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/big_query/client/tables.rb b/lib/big_query/client/tables.rb index 34f87d7..5687f81 100644 --- a/lib/big_query/client/tables.rb +++ b/lib/big_query/client/tables.rb @@ -78,9 +78,11 @@ def create_table(tableId, schema={}) "projectId" => @project_id, "datasetId" => @dataset }, - "schema" => { - "fields" => validate_schema(schema) - } + "schema" => schema + # "schema" => { + # "fields" => validate_schema(schema) + # "fields" => validate_schema(schema) + # } } ) end From 17bfd0fe9ee3e0e8f85bccf6c5eead4a4320fff2 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Fri, 20 Nov 2015 22:51:42 +0100 Subject: [PATCH 3/8] Update tables.rb Require dataset parameter in create_table --- lib/big_query/client/tables.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/big_query/client/tables.rb b/lib/big_query/client/tables.rb index 5687f81..102d9fd 100644 --- a/lib/big_query/client/tables.rb +++ b/lib/big_query/client/tables.rb @@ -69,14 +69,14 @@ def insert(tableId, opts) # # @bq.create_table('new_table', id: { type: 'INTEGER', mode: 'required' }) # @bq.create_table('new_table', price: { type: 'FLOAT' }) - def create_table(tableId, schema={}) + def create_table(tableId, dataset, schema={}) api( api_method: @bq.tables.insert, - parameters: { "datasetId" => @dataset }, + parameters: { "datasetId" => dataset }, body_object: { "tableReference" => { "tableId" => tableId, "projectId" => @project_id, - "datasetId" => @dataset + "datasetId" => dataset }, "schema" => schema # "schema" => { From 5ad0b5259d70846cdb883db0ae08d0b74dee6c13 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Fri, 20 Nov 2015 23:33:54 +0100 Subject: [PATCH 4/8] Update tables.rb --- lib/big_query/client/tables.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/big_query/client/tables.rb b/lib/big_query/client/tables.rb index 102d9fd..1bdc43e 100644 --- a/lib/big_query/client/tables.rb +++ b/lib/big_query/client/tables.rb @@ -69,7 +69,7 @@ def insert(tableId, opts) # # @bq.create_table('new_table', id: { type: 'INTEGER', mode: 'required' }) # @bq.create_table('new_table', price: { type: 'FLOAT' }) - def create_table(tableId, dataset, schema={}) + def create_table(tableId, schema, dataset = @dataset) api( api_method: @bq.tables.insert, parameters: { "datasetId" => dataset }, From 2464da8fdf763125cb5de31b02eb2306dc7ac114 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Sat, 21 Nov 2015 14:10:43 +0100 Subject: [PATCH 5/8] Update tables.rb --- lib/big_query/client/tables.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/big_query/client/tables.rb b/lib/big_query/client/tables.rb index 1bdc43e..355fa65 100644 --- a/lib/big_query/client/tables.rb +++ b/lib/big_query/client/tables.rb @@ -11,10 +11,10 @@ module Tables # # @param dataset [String] dataset to look for # @return [Hash] json api response - def tables(dataset = @dataset) + def tables(datasetId) response = api({ :api_method => @bq.tables.list, - :parameters => {"datasetId" => dataset} + :parameters => {"datasetId" => datasetId} }) response['tables'] || [] @@ -24,8 +24,8 @@ def tables(dataset = @dataset) # # @param dataset [String] dataset to look for # @return [Hash] json api response - def tables_formatted(dataset = @dataset) - tables(dataset).map { |t| t['tableReference']['tableId'] } + def tables_formatted(datasetId) + tables(datasetId).map { |t| t['tableReference']['tableId'] } end # Returns all rows of table data @@ -33,9 +33,9 @@ def tables_formatted(dataset = @dataset) # @param tableId [String] id of the table to look for # @param dataset [String] dataset to look for # @return [Hash] json api response - def table_data(tableId, dataset = @dataset) + def table_data(datasetId, tableId) response = api(api_method: @bq.tabledata.list, - parameters: { 'datasetId' => dataset, + parameters: { 'datasetId' => datasetId, 'tableId' => tableId }) response['rows'] || [] end @@ -45,7 +45,7 @@ def table_data(tableId, dataset = @dataset) # @param tableId [String] table id to insert into # @param opts [Hash] field value hash to be inserted # @return [Hash] - def insert(tableId, opts) + def insert(datasetId, tableId, opts) if opts.class == Array body = { 'rows' => opts = opts.map{|x| {"json" => x}} } else @@ -55,7 +55,7 @@ def insert(tableId, opts) api( api_method: @bq.tabledata.insert_all, parameters: { 'tableId' => tableId, - 'datasetId' => @dataset }, + 'datasetId' => datasetId }, body_object: body ) end @@ -69,14 +69,14 @@ def insert(tableId, opts) # # @bq.create_table('new_table', id: { type: 'INTEGER', mode: 'required' }) # @bq.create_table('new_table', price: { type: 'FLOAT' }) - def create_table(tableId, schema, dataset = @dataset) + def create_table(datasetId, tableId, schema={}) api( api_method: @bq.tables.insert, - parameters: { "datasetId" => dataset }, + parameters: { "datasetId" => datasetId }, body_object: { "tableReference" => { "tableId" => tableId, "projectId" => @project_id, - "datasetId" => dataset + "datasetId" => datasetId }, "schema" => schema # "schema" => { @@ -90,10 +90,10 @@ def create_table(tableId, schema, dataset = @dataset) # Deletes the given tableId # # @param tableId [String] table id to insert into - def delete_table(tableId) + def delete_table(datasetId, tableId) api(api_method: @bq.tables.delete, parameters: { 'tableId' => tableId, - 'datasetId' => @dataset } + 'datasetId' => datasetId } ) end @@ -102,11 +102,11 @@ def delete_table(tableId) # @param tableId [String] table id to describe # @param dataset [String] dataset to look for # @return [Hash] json api response - def describe_table(tableId, dataset = @dataset) + def describe_table(datasetId, tableId) api( api_method: @bq.tables.get, parameters: { 'tableId' => tableId, - 'datasetId' => dataset } + 'datasetId' => datasetId } ) end From 0e7d285fd95c7f72efbbde02237d5c96266adae3 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Sat, 21 Nov 2015 21:00:48 +0100 Subject: [PATCH 6/8] Update client.rb --- lib/big_query/client.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/big_query/client.rb b/lib/big_query/client.rb index 88f4836..ae51131 100644 --- a/lib/big_query/client.rb +++ b/lib/big_query/client.rb @@ -2,6 +2,7 @@ require 'big_query/client/query' require 'big_query/client/jobs' require 'big_query/client/tables' +require 'big_query/client/datasets' require 'big_query/client/load' module BigQuery @@ -10,6 +11,7 @@ class Client include BigQuery::Client::Query include BigQuery::Client::Jobs include BigQuery::Client::Tables + include BigQuery::Client::Datasets include BigQuery::Client::Insert attr_accessor :dataset, :project_id From c432332eca320786323941d2272dd089b94dbc68 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Sat, 21 Nov 2015 21:09:50 +0100 Subject: [PATCH 7/8] Create datasets.rb --- lib/big_query/client/datasets.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/big_query/client/datasets.rb diff --git a/lib/big_query/client/datasets.rb b/lib/big_query/client/datasets.rb new file mode 100644 index 0000000..2ffab3b --- /dev/null +++ b/lib/big_query/client/datasets.rb @@ -0,0 +1,29 @@ + +# Module to handle dataset actions +# https://developers.google.com/bigquery/docs/datasets +module BigQuery + class Client + module Datasets + + # Creating a new dataset + # + # @param datasetId [String] dataset id to create + # + # examples: + # + # @bq.create_dataset('new_dataset') + def create_dataset(datasetId) + api( + api_method: @bq.datasets.insert, + parameters: { "projectId" => @project_id }, + body_object: { "datasetReference" => { + "projectId" => @project_id, + "datasetId" => datasetId + } + } + ) + end + + end + end +end From 137b7b4bd8657699a1d72e8b6bb0678091ae381b Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Mon, 4 Jul 2016 00:21:12 +0200 Subject: [PATCH 8/8] Update tables.rb --- lib/big_query/client/tables.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/big_query/client/tables.rb b/lib/big_query/client/tables.rb index 355fa65..fd1aae5 100644 --- a/lib/big_query/client/tables.rb +++ b/lib/big_query/client/tables.rb @@ -87,6 +87,14 @@ def create_table(datasetId, tableId, schema={}) ) end + def create_table2(datasetId, config={}) + api( + api_method: @bq.tables.insert, + parameters: { "datasetId" => datasetId }, + body_object: config + ) + end + # Deletes the given tableId # # @param tableId [String] table id to insert into