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 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 diff --git a/lib/big_query/client/tables.rb b/lib/big_query/client/tables.rb index 03ce151..fd1aae5 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,29 +69,39 @@ 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(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" => { - "fields" => validate_schema(schema) - } + "schema" => schema + # "schema" => { + # "fields" => validate_schema(schema) + # "fields" => validate_schema(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 - def delete_table(tableId) + def delete_table(datasetId, tableId) api(api_method: @bq.tables.delete, parameters: { 'tableId' => tableId, - 'datasetId' => @dataset } + 'datasetId' => datasetId } ) end @@ -100,11 +110,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