Class: Google::Cloud::Bigquery::Table
- Inherits:
-
Object
- Object
- Google::Cloud::Bigquery::Table
- Defined in:
- lib/google/cloud/bigquery/table.rb,
lib/google/cloud/bigquery/table/list.rb
Overview
Table
A named resource representing a BigQuery table that holds zero or more records. Every table is defined by a schema that may contain nested and repeated fields.
Direct Known Subclasses
Defined Under Namespace
Attributes collapse
-
#api_url ⇒ Object
A URL that can be used to access the dataset using the REST API.
-
#created_at ⇒ Object
The time when this table was created.
-
#dataset_id ⇒ Object
The ID of the
Dataset
containing this table. -
#description ⇒ Object
The description of the table.
-
#description=(new_description) ⇒ Object
Updates the description of the table.
-
#etag ⇒ Object
A string hash of the dataset.
-
#expires_at ⇒ Object
The time when this table expires.
-
#fields ⇒ Object
The fields of the table.
-
#headers ⇒ Object
The names of the columns in the table.
-
#id ⇒ Object
The combined Project ID, Dataset ID, and Table ID for this table, in the format specified by the Query Reference:
project_name:datasetId.tableId
. -
#location ⇒ Object
The geographic location where the table should reside.
-
#modified_at ⇒ Object
The date when this table was last modified.
-
#name ⇒ Object
The name of the table.
-
#name=(new_name) ⇒ Object
Updates the name of the table.
-
#project_id ⇒ Object
The ID of the
Project
containing this table. -
#query_id ⇒ Object
The value returned by #id, wrapped in square brackets if the Project ID contains dashes, as specified by the Query Reference.
-
#schema(replace: false) {|schema| ... } ⇒ Google::Cloud::Bigquery::Schema
Returns the table's schema.
-
#table? ⇒ Boolean
Checks if the table's type is "TABLE".
-
#table_id ⇒ Object
A unique ID for this table.
-
#view? ⇒ Boolean
Checks if the table's type is "VIEW".
Data collapse
-
#bytes_count ⇒ Object
The number of bytes in the table.
-
#copy(destination_table, create: nil, write: nil, dryrun: nil) ⇒ Google::Cloud::Bigquery::CopyJob
Copies the data from the table to another table.
-
#data(token: nil, max: nil, start: nil) ⇒ Google::Cloud::Bigquery::Data
Retrieves data from the table.
-
#extract(extract_url, format: nil, compression: nil, delimiter: nil, header: nil, dryrun: nil) ⇒ Google::Cloud::Bigquery::ExtractJob
Extract the data from the table to a Google Cloud Storage file.
-
#insert(rows, skip_invalid: nil, ignore_unknown: nil) ⇒ Google::Cloud::Bigquery::InsertResponse
Inserts data into the table for near-immediate querying, without the need to complete a #load operation before the data can appear in query results.
-
#load(file, format: nil, create: nil, write: nil, projection_fields: nil, jagged_rows: nil, quoted_newlines: nil, encoding: nil, delimiter: nil, ignore_unknown: nil, max_bad_records: nil, quote: nil, skip_leading: nil, dryrun: nil) ⇒ Google::Cloud::Bigquery::LoadJob
Loads data into the table.
-
#rows_count ⇒ Object
The number of rows in the table.
Lifecycle collapse
-
#delete ⇒ Boolean
Permanently deletes the table.
-
#reload! ⇒ Object
(also: #refresh!)
Reloads the table with current data from the BigQuery service.
Instance Method Details
#api_url ⇒ Object
A URL that can be used to access the dataset using the REST API.
191 192 193 194 |
# File 'lib/google/cloud/bigquery/table.rb', line 191 def api_url ensure_full_data! @gapi.self_link end |
#bytes_count ⇒ Object
The number of bytes in the table.
221 222 223 224 225 226 227 228 |
# File 'lib/google/cloud/bigquery/table.rb', line 221 def bytes_count ensure_full_data! begin Integer @gapi.num_bytes rescue nil end end |
#copy(destination_table, create: nil, write: nil, dryrun: nil) ⇒ Google::Cloud::Bigquery::CopyJob
Copies the data from the table to another table. The destination table
argument can also be a string identifier as specified by the Query
Reference:
project_name:datasetId.tableId
. This is useful for referencing
tables in other projects and datasets.
488 489 490 491 492 493 494 495 |
# File 'lib/google/cloud/bigquery/table.rb', line 488 def copy destination_table, create: nil, write: nil, dryrun: nil ensure_service! = { create: create, write: write, dryrun: dryrun } gapi = service.copy_table table_ref, get_table_ref(destination_table), Job.from_gapi gapi, service end |
#created_at ⇒ Object
The time when this table was created.
249 250 251 252 253 254 255 256 |
# File 'lib/google/cloud/bigquery/table.rb', line 249 def created_at ensure_full_data! begin Time.at(Integer(@gapi.creation_time) / 1000.0) rescue nil end end |
#data(token: nil, max: nil, start: nil) ⇒ Google::Cloud::Bigquery::Data
Retrieves data from the table.
429 430 431 432 433 434 |
# File 'lib/google/cloud/bigquery/table.rb', line 429 def data token: nil, max: nil, start: nil ensure_service! = { token: token, max: max, start: start } gapi = service.list_tabledata dataset_id, table_id, Data.from_gapi gapi, self end |
#dataset_id ⇒ Object
The ID of the Dataset
containing this table.
100 101 102 |
# File 'lib/google/cloud/bigquery/table.rb', line 100 def dataset_id @gapi.table_reference.dataset_id end |
#delete ⇒ Boolean
Permanently deletes the table.
752 753 754 755 756 |
# File 'lib/google/cloud/bigquery/table.rb', line 752 def delete ensure_service! service.delete_table dataset_id, table_id true end |
#description ⇒ Object
The description of the table.
201 202 203 204 |
# File 'lib/google/cloud/bigquery/table.rb', line 201 def description ensure_full_data! @gapi.description end |
#description=(new_description) ⇒ Object
Updates the description of the table.
211 212 213 214 |
# File 'lib/google/cloud/bigquery/table.rb', line 211 def description= new_description @gapi.update! description: new_description patch_gapi! :description end |
#etag ⇒ Object
A string hash of the dataset.
181 182 183 184 |
# File 'lib/google/cloud/bigquery/table.rb', line 181 def etag ensure_full_data! @gapi.etag end |
#expires_at ⇒ Object
The time when this table expires. If not present, the table will persist indefinitely. Expired tables will be deleted and their storage reclaimed.
265 266 267 268 269 270 271 272 |
# File 'lib/google/cloud/bigquery/table.rb', line 265 def expires_at ensure_full_data! begin Time.at(Integer(@gapi.expiration_time) / 1000.0) rescue nil end end |
#extract(extract_url, format: nil, compression: nil, delimiter: nil, header: nil, dryrun: nil) ⇒ Google::Cloud::Bigquery::ExtractJob
Extract the data from the table to a Google Cloud Storage file.
538 539 540 541 542 543 544 545 |
# File 'lib/google/cloud/bigquery/table.rb', line 538 def extract extract_url, format: nil, compression: nil, delimiter: nil, header: nil, dryrun: nil ensure_service! = { format: format, compression: compression, delimiter: delimiter, header: header, dryrun: dryrun } gapi = service.extract_table table_ref, extract_url, Job.from_gapi gapi, service end |
#fields ⇒ Object
The fields of the table.
374 375 376 |
# File 'lib/google/cloud/bigquery/table.rb', line 374 def fields schema.fields end |
#headers ⇒ Object
The names of the columns in the table.
383 384 385 |
# File 'lib/google/cloud/bigquery/table.rb', line 383 def headers fields.map(&:name) end |
#id ⇒ Object
The combined Project ID, Dataset ID, and Table ID for this table, in
the format specified by the Query
Reference:
project_name:datasetId.tableId
. To use this value in queries see
#query_id.
131 132 133 |
# File 'lib/google/cloud/bigquery/table.rb', line 131 def id @gapi.id end |
#insert(rows, skip_invalid: nil, ignore_unknown: nil) ⇒ Google::Cloud::Bigquery::InsertResponse
Inserts data into the table for near-immediate querying, without the need to complete a #load operation before the data can appear in query results.
726 727 728 729 730 731 732 733 |
# File 'lib/google/cloud/bigquery/table.rb', line 726 def insert rows, skip_invalid: nil, ignore_unknown: nil rows = [rows] if rows.is_a? Hash ensure_service! = { skip_invalid: skip_invalid, ignore_unknown: ignore_unknown } gapi = service.insert_tabledata dataset_id, table_id, rows, InsertResponse.from_gapi rows, gapi end |
#load(file, format: nil, create: nil, write: nil, projection_fields: nil, jagged_rows: nil, quoted_newlines: nil, encoding: nil, delimiter: nil, ignore_unknown: nil, max_bad_records: nil, quote: nil, skip_leading: nil, dryrun: nil) ⇒ Google::Cloud::Bigquery::LoadJob
Loads data into the table. You can pass a google-cloud storage file path or a google-cloud storage file instance. Or, you can upload a file directly. See Loading Data with a POST Request.
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
# File 'lib/google/cloud/bigquery/table.rb', line 673 def load file, format: nil, create: nil, write: nil, projection_fields: nil, jagged_rows: nil, quoted_newlines: nil, encoding: nil, delimiter: nil, ignore_unknown: nil, max_bad_records: nil, quote: nil, skip_leading: nil, dryrun: nil ensure_service! = { format: format, create: create, write: write, projection_fields: projection_fields, jagged_rows: jagged_rows, quoted_newlines: quoted_newlines, encoding: encoding, delimiter: delimiter, ignore_unknown: ignore_unknown, max_bad_records: max_bad_records, quote: quote, skip_leading: skip_leading, dryrun: dryrun } return load_storage(file, ) if storage_url? file return load_local(file, ) if local_file? file fail Google::Cloud::Error, "Don't know how to load #{file}" end |
#location ⇒ Object
The geographic location where the table should reside. Possible values include EU and US. The default value is US.
312 313 314 315 |
# File 'lib/google/cloud/bigquery/table.rb', line 312 def location ensure_full_data! @gapi.location end |
#modified_at ⇒ Object
The date when this table was last modified.
279 280 281 282 283 284 285 286 |
# File 'lib/google/cloud/bigquery/table.rb', line 279 def modified_at ensure_full_data! begin Time.at(Integer(@gapi.last_modified_time) / 1000.0) rescue nil end end |
#name ⇒ Object
The name of the table.
162 163 164 |
# File 'lib/google/cloud/bigquery/table.rb', line 162 def name @gapi.friendly_name end |
#name=(new_name) ⇒ Object
Updates the name of the table.
171 172 173 174 |
# File 'lib/google/cloud/bigquery/table.rb', line 171 def name= new_name @gapi.update! friendly_name: new_name patch_gapi! :friendly_name end |
#project_id ⇒ Object
The ID of the Project
containing this table.
109 110 111 |
# File 'lib/google/cloud/bigquery/table.rb', line 109 def project_id @gapi.table_reference.project_id end |
#query_id ⇒ Object
The value returned by #id, wrapped in square brackets if the Project ID contains dashes, as specified by the Query Reference. Useful in queries.
153 154 155 |
# File 'lib/google/cloud/bigquery/table.rb', line 153 def query_id project_id["-"] ? "[#{id}]" : id end |
#reload! ⇒ Object Also known as: refresh!
Reloads the table with current data from the BigQuery service.
763 764 765 766 767 |
# File 'lib/google/cloud/bigquery/table.rb', line 763 def reload! ensure_service! gapi = service.get_table dataset_id, table_id @gapi = gapi end |
#rows_count ⇒ Object
The number of rows in the table.
235 236 237 238 239 240 241 242 |
# File 'lib/google/cloud/bigquery/table.rb', line 235 def rows_count ensure_full_data! begin Integer @gapi.num_rows rescue nil end end |
#schema(replace: false) {|schema| ... } ⇒ Google::Cloud::Bigquery::Schema
Returns the table's schema. This method can also be used to set, replace, or add to the schema by passing a block. See Schema for available methods.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/google/cloud/bigquery/table.rb', line 350 def schema replace: false ensure_full_data! schema_builder = Schema.from_gapi @gapi.schema if block_given? if replace empty_schema = Google::Apis::BigqueryV2::TableSchema.new( fields: []) schema_builder = Schema.from_gapi empty_schema end yield schema_builder schema_builder.check_for_mutated_schema! if schema_builder.changed? @gapi.schema = schema_builder.to_gapi patch_gapi! :schema end end schema_builder.freeze end |
#table? ⇒ Boolean
Checks if the table's type is "TABLE".
293 294 295 |
# File 'lib/google/cloud/bigquery/table.rb', line 293 def table? @gapi.type == "TABLE" end |
#table_id ⇒ Object
A unique ID for this table. The ID must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_). The maximum length is 1,024 characters.
91 92 93 |
# File 'lib/google/cloud/bigquery/table.rb', line 91 def table_id @gapi.table_reference.table_id end |
#view? ⇒ Boolean
Checks if the table's type is "VIEW".
302 303 304 |
# File 'lib/google/cloud/bigquery/table.rb', line 302 def view? @gapi.type == "VIEW" end |