Module: BigQuery::Client::Tables
- Included in:
- BigQuery::Client
- Defined in:
- lib/big_query/client/tables.rb
Constant Summary collapse
- ALLOWED_FIELD_TYPES =
['STRING', 'INTEGER', 'FLOAT', 'BOOLEAN', 'RECORD', 'TIMESTAMP']
- ALLOWED_FIELD_MODES =
['NULLABLE', 'REQUIRED', 'REPEATED']
Instance Method Summary collapse
-
#create_table(tableId, schema = {}) ⇒ Object
Creating a new table.
-
#delete_table(tableId) ⇒ Object
Deletes the given tableId.
-
#describe_table(tableId, dataset = @dataset) ⇒ Hash
Describe the schema of the given tableId.
-
#insert(tableId, opts) ⇒ Hash
insert row into table.
-
#patch_table(tableId, schema = {}) ⇒ Object
Patching a exsiting table.
-
#table_data(tableId, dataset = @dataset) ⇒ Hash
Returns all rows of table data.
-
#tables(dataset = @dataset) ⇒ Hash
Lists the tables.
-
#tables_formatted(dataset = @dataset) ⇒ Hash
Lists the tables returnning only the tableId.
-
#update_table(tableId, schema = {}) ⇒ Object
Updating a exsiting table.
Instance Method Details
#create_table(tableId, schema = {}) ⇒ Object
Creating a new table
examples:
@bq.create_table(‘new_table’, id: { type: ‘INTEGER’, mode: ‘required’ }) @bq.create_table(‘new_table’, price: { type: ‘FLOAT’ })
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/big_query/client/tables.rb', line 72 def create_table(tableId, schema={}) api( api_method: @bq.tables.insert, parameters: { "datasetId" => @dataset }, body_object: { "tableReference" => { "tableId" => tableId, "projectId" => @project_id, "datasetId" => @dataset }, "schema" => { "fields" => validate_schema(schema) } } ) end |
#delete_table(tableId) ⇒ Object
Deletes the given tableId
91 92 93 94 95 96 |
# File 'lib/big_query/client/tables.rb', line 91 def delete_table(tableId) api(api_method: @bq.tables.delete, parameters: { 'tableId' => tableId, 'datasetId' => @dataset } ) end |
#describe_table(tableId, dataset = @dataset) ⇒ Hash
Describe the schema of the given tableId
157 158 159 160 161 162 163 |
# File 'lib/big_query/client/tables.rb', line 157 def describe_table(tableId, dataset = @dataset) api( api_method: @bq.tables.get, parameters: { 'tableId' => tableId, 'datasetId' => @dataset } ) end |
#insert(tableId, opts) ⇒ Hash
insert row into table
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/big_query/client/tables.rb', line 48 def insert(tableId, opts) if opts.class == Array body = { 'rows' => opts = opts.map{|x| {"json" => x}} } else body = { 'rows' => [{ 'json' => opts }] } end api( api_method: @bq.tabledata.insert_all, parameters: { 'tableId' => tableId, 'datasetId' => @dataset }, body_object: body ) end |
#patch_table(tableId, schema = {}) ⇒ Object
Patching a exsiting table
examples:
@bq.patch_table(‘existing_table’, id: { type: ‘INTEGER’, mode: ‘required’ }, price: { type: ‘FLOAT’ }) It should be provide entire schema including the difference between the existing schema Otherwise ‘BigQuery::Errors::BigQueryError: Provided Schema does not match Table’ occur
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/big_query/client/tables.rb', line 108 def patch_table(tableId, schema={}) api( api_method: @bq.tables.patch, parameters: { 'tableId' => tableId, 'datasetId' => @dataset }, body_object: { 'tableReference' => { 'tableId' => tableId, 'projectId' => @project_id, 'datasetId' => @dataset }, 'schema' => { 'fields' => validate_schema(schema) } } ) end |
#table_data(tableId, dataset = @dataset) ⇒ Hash
Returns all rows of table data
36 37 38 39 40 41 |
# File 'lib/big_query/client/tables.rb', line 36 def table_data(tableId, dataset = @dataset) response = api(api_method: @bq.tabledata.list, parameters: { 'datasetId' => dataset, 'tableId' => tableId }) response['rows'] || [] end |
#tables(dataset = @dataset) ⇒ Hash
Lists the tables
14 15 16 17 18 19 20 21 |
# File 'lib/big_query/client/tables.rb', line 14 def tables(dataset = @dataset) response = api({ :api_method => @bq.tables.list, :parameters => {"datasetId" => dataset} }) response['tables'] || [] end |
#tables_formatted(dataset = @dataset) ⇒ Hash
Lists the tables returnning only the tableId
27 28 29 |
# File 'lib/big_query/client/tables.rb', line 27 def tables_formatted(dataset = @dataset) tables(dataset).map { |t| t['tableReference']['tableId'] } end |
#update_table(tableId, schema = {}) ⇒ Object
Updating a exsiting table
examples:
@bq.update_table(‘existing_table’, id: { type: ‘INTEGER’, mode: ‘required’ }, price: { type: ‘FLOAT’ }) It should be provide entire schema including the difference between the existing schema Otherwise ‘BigQuery::Errors::BigQueryError: Provided Schema does not match Table’ occur
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/big_query/client/tables.rb', line 135 def update_table(tableId, schema={}) api( api_method: @bq.tables.update, parameters: { 'tableId' => tableId, 'datasetId' => @dataset }, body_object: { 'tableReference' => { 'tableId' => tableId, 'projectId' => @project_id, 'datasetId' => @dataset }, 'schema' => { 'fields' => validate_schema(schema) } } ) end |