Skip to content
This repository has been archived by the owner on Jun 12, 2019. It is now read-only.

Update tables.rb #31

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions lib/big_query/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions lib/big_query/client/datasets.rb
Original file line number Diff line number Diff line change
@@ -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
46 changes: 28 additions & 18 deletions lib/big_query/client/tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'] || []
Expand All @@ -24,18 +24,18 @@ 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
#
# @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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand Down