Class: Google::Cloud::Bigquery::Project
- Inherits:
-
Object
- Object
- Google::Cloud::Bigquery::Project
- Defined in:
- lib/google/cloud/bigquery/project.rb,
lib/google/cloud/bigquery/project/list.rb
Overview
Project
Projects are top-level containers in Google Cloud Platform. They store information about billing and authorized users, and they contain BigQuery data. Each project has a friendly name and a unique ID.
Google::Cloud::Bigquery::Project is the main object for interacting with Google BigQuery. Dataset objects are created, accessed, and deleted by Google::Cloud::Bigquery::Project.
Defined Under Namespace
Classes: List
Instance Attribute Summary collapse
-
#name ⇒ String?
readonly
The descriptive name of the project.
-
#numeric_id ⇒ Integer?
readonly
The numeric ID of the project.
Instance Method Summary collapse
-
#create_dataset(dataset_id, name: nil, description: nil, expiration: nil, location: nil) {|access| ... } ⇒ Google::Cloud::Bigquery::Dataset
Creates a new dataset.
-
#dataset(dataset_id, skip_lookup: nil) ⇒ Google::Cloud::Bigquery::Dataset?
Retrieves an existing dataset by ID.
-
#datasets(all: nil, filter: nil, token: nil, max: nil) ⇒ Array<Google::Cloud::Bigquery::Dataset>
Retrieves the list of datasets belonging to the project.
-
#external(url, format: nil) {|ext| ... } ⇒ External::DataSource
Creates a new External::DataSource (or subclass) object that represents the external data source that can be queried from directly, even though the data is not stored in BigQuery.
-
#initialize(service) ⇒ Project
constructor
Creates a new Service instance.
-
#job(job_id) ⇒ Google::Cloud::Bigquery::Job?
Retrieves an existing job by ID.
-
#jobs(all: nil, token: nil, max: nil, filter: nil) ⇒ Array<Google::Cloud::Bigquery::Job>
Retrieves the list of jobs belonging to the project.
-
#project_id ⇒ Object
(also: #project)
The BigQuery project connected to.
-
#projects(token: nil, max: nil) ⇒ Array<Google::Cloud::Bigquery::Project>
Retrieves the list of all projects for which the currently authorized account has been granted any project role.
-
#query(query, params: nil, external: nil, max: nil, cache: true, dataset: nil, project: nil, standard_sql: nil, legacy_sql: nil) ⇒ Google::Cloud::Bigquery::Data
Queries data and waits for the results.
-
#query_job(query, params: nil, external: nil, priority: "INTERACTIVE", cache: true, table: nil, create: nil, write: nil, dataset: nil, project: nil, standard_sql: nil, legacy_sql: nil, large_results: nil, flatten: nil, maximum_billing_tier: nil, maximum_bytes_billed: nil, job_id: nil, prefix: nil, labels: nil, udfs: nil) ⇒ Google::Cloud::Bigquery::QueryJob
Queries data by creating a query job.
-
#schema {|schema| ... } ⇒ Google::Cloud::Bigquery::Schema
Creates a new schema instance.
-
#time(hour, minute, second) ⇒ Bigquery::Time
Creates a Bigquery::Time object to represent a time, independent of a specific date.
Constructor Details
#initialize(service) ⇒ Project
Creates a new Service instance.
66 67 68 |
# File 'lib/google/cloud/bigquery/project.rb', line 66 def initialize service @service = service end |
Instance Attribute Details
#name ⇒ String? (readonly)
The descriptive name of the project. Can only be present if the project was retrieved with #projects.
55 56 57 |
# File 'lib/google/cloud/bigquery/project.rb', line 55 def name @name end |
#numeric_id ⇒ Integer? (readonly)
The numeric ID of the project. Can only be present if the project was retrieved with #projects.
55 56 57 |
# File 'lib/google/cloud/bigquery/project.rb', line 55 def numeric_id @numeric_id end |
Instance Method Details
#create_dataset(dataset_id, name: nil, description: nil, expiration: nil, location: nil) {|access| ... } ⇒ Google::Cloud::Bigquery::Dataset
Creates a new dataset.
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
# File 'lib/google/cloud/bigquery/project.rb', line 664 def create_dataset dataset_id, name: nil, description: nil, expiration: nil, location: nil ensure_service! new_ds = Google::Apis::BigqueryV2::Dataset.new( dataset_reference: Google::Apis::BigqueryV2::DatasetReference.new( project_id: project, dataset_id: dataset_id)) # Can set location only on creation, no Dataset#location method new_ds.update! location: location unless location.nil? updater = Dataset::Updater.new(new_ds).tap do |b| b.name = name unless name.nil? b.description = description unless description.nil? b.default_expiration = expiration unless expiration.nil? end if block_given? yield updater updater.check_for_mutated_access! end gapi = service.insert_dataset new_ds Dataset.from_gapi gapi, service end |
#dataset(dataset_id, skip_lookup: nil) ⇒ Google::Cloud::Bigquery::Dataset?
Retrieves an existing dataset by ID.
607 608 609 610 611 612 613 614 615 616 |
# File 'lib/google/cloud/bigquery/project.rb', line 607 def dataset dataset_id, skip_lookup: nil ensure_service! if skip_lookup return Dataset.new_reference project, dataset_id, service end gapi = service.get_dataset dataset_id Dataset.from_gapi gapi, service rescue Google::Cloud::NotFoundError nil end |
#datasets(all: nil, filter: nil, token: nil, max: nil) ⇒ Array<Google::Cloud::Bigquery::Dataset>
Retrieves the list of datasets belonging to the project.
734 735 736 737 738 739 |
# File 'lib/google/cloud/bigquery/project.rb', line 734 def datasets all: nil, filter: nil, token: nil, max: nil ensure_service! = { all: all, filter: filter, token: token, max: max } gapi = service.list_datasets Dataset::List.from_gapi gapi, service, all, filter, max end |
#external(url, format: nil) {|ext| ... } ⇒ External::DataSource
Creates a new External::DataSource (or subclass) object that represents the external data source that can be queried from directly, even though the data is not stored in BigQuery. Instead of loading or streaming the data, this object references the external data source.
574 575 576 577 578 |
# File 'lib/google/cloud/bigquery/project.rb', line 574 def external url, format: nil ext = External.from_urls url, format yield ext if block_given? ext end |
#job(job_id) ⇒ Google::Cloud::Bigquery::Job?
Retrieves an existing job by ID.
756 757 758 759 760 761 762 |
# File 'lib/google/cloud/bigquery/project.rb', line 756 def job job_id ensure_service! gapi = service.get_job job_id Job.from_gapi gapi, service rescue Google::Cloud::NotFoundError nil end |
#jobs(all: nil, token: nil, max: nil, filter: nil) ⇒ Array<Google::Cloud::Bigquery::Job>
Retrieves the list of jobs belonging to the project.
813 814 815 816 817 818 |
# File 'lib/google/cloud/bigquery/project.rb', line 813 def jobs all: nil, token: nil, max: nil, filter: nil ensure_service! = { all: all, token: token, max: max, filter: filter } gapi = service.list_jobs Job::List.from_gapi gapi, service, all, max, filter end |
#project_id ⇒ Object Also known as: project
The BigQuery project connected to.
83 84 85 |
# File 'lib/google/cloud/bigquery/project.rb', line 83 def project_id service.project end |
#projects(token: nil, max: nil) ⇒ Array<Google::Cloud::Bigquery::Project>
Retrieves the list of all projects for which the currently authorized account has been granted any project role. The returned project instances share the same credentials as the project used to retrieve them, but lazily create a new API connection for interactions with the BigQuery service.
861 862 863 864 865 866 |
# File 'lib/google/cloud/bigquery/project.rb', line 861 def projects token: nil, max: nil ensure_service! = { token: token, max: max } gapi = service.list_projects Project::List.from_gapi gapi, service, max end |
#query(query, params: nil, external: nil, max: nil, cache: true, dataset: nil, project: nil, standard_sql: nil, legacy_sql: nil) ⇒ Google::Cloud::Bigquery::Data
Queries data and waits for the results. In this method, a QueryJob is created and its results are saved to a temporary table, then read from the table. Timeouts and transient errors are generally handled as needed to complete the query.
When using standard SQL and passing arguments using params
, Ruby
types are mapped to BigQuery types as follows:
BigQuery | Ruby | Notes |
---|---|---|
BOOL |
true /false |
|
INT64 |
Integer |
|
FLOAT64 |
Float |
|
STRING |
STRING |
|
DATETIME |
DateTime |
DATETIME does not support time zone. |
DATE |
Date |
|
TIMESTAMP |
Time |
|
TIME |
Google::Cloud::BigQuery::Time |
|
BYTES |
File , IO , StringIO , or similar |
|
ARRAY |
Array |
Nested arrays, nil values are not supported. |
STRUCT |
Hash |
Hash keys may be strings or symbols. |
See Data Types for an overview of each BigQuery data type, including allowed values.
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/google/cloud/bigquery/project.rb', line 506 def query query, params: nil, external: nil, max: nil, cache: true, dataset: nil, project: nil, standard_sql: nil, legacy_sql: nil ensure_service! = { cache: cache, dataset: dataset, project: project, legacy_sql: legacy_sql, standard_sql: standard_sql, params: params, external: external } job = query_job query, job.wait_until_done! if job.failed? begin # raise to activate ruby exception cause handling fail job.gapi_error rescue => e # wrap Google::Apis::Error with Google::Cloud::Error raise Google::Cloud::Error.from_error(e) end end job.data max: max end |
#query_job(query, params: nil, external: nil, priority: "INTERACTIVE", cache: true, table: nil, create: nil, write: nil, dataset: nil, project: nil, standard_sql: nil, legacy_sql: nil, large_results: nil, flatten: nil, maximum_billing_tier: nil, maximum_bytes_billed: nil, job_id: nil, prefix: nil, labels: nil, udfs: nil) ⇒ Google::Cloud::Bigquery::QueryJob
Queries data by creating a query job.
When using standard SQL and passing arguments using params
, Ruby
types are mapped to BigQuery types as follows:
BigQuery | Ruby | Notes |
---|---|---|
BOOL |
true /false |
|
INT64 |
Integer |
|
FLOAT64 |
Float |
|
STRING |
STRING |
|
DATETIME |
DateTime |
DATETIME does not support time zone. |
DATE |
Date |
|
TIMESTAMP |
Time |
|
TIME |
Google::Cloud::BigQuery::Time |
|
BYTES |
File , IO , StringIO , or similar |
|
ARRAY |
Array |
Nested arrays, nil values are not supported. |
STRUCT |
Hash |
Hash keys may be strings or symbols. |
See Data Types for an overview of each BigQuery data type, including allowed values.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/google/cloud/bigquery/project.rb', line 321 def query_job query, params: nil, external: nil, priority: "INTERACTIVE", cache: true, table: nil, create: nil, write: nil, dataset: nil, project: nil, standard_sql: nil, legacy_sql: nil, large_results: nil, flatten: nil, maximum_billing_tier: nil, maximum_bytes_billed: nil, job_id: nil, prefix: nil, labels: nil, udfs: nil ensure_service! = { priority: priority, cache: cache, table: table, create: create, write: write, large_results: large_results, flatten: flatten, dataset: dataset, project: project, legacy_sql: legacy_sql, standard_sql: standard_sql, maximum_billing_tier: maximum_billing_tier, maximum_bytes_billed: maximum_bytes_billed, params: params, external: external, labels: labels, job_id: job_id, prefix: prefix, udfs: udfs } gapi = service.query_job query, Job.from_gapi gapi, service end |
#schema {|schema| ... } ⇒ Google::Cloud::Bigquery::Schema
Creates a new schema instance. An optional block may be given to configure the schema, otherwise the schema is returned empty and may be configured directly.
The returned schema can be passed to Dataset#load using the
schema
option. However, for most use cases, the block yielded by
Dataset#load is a more convenient way to configure the schema
for the destination table.
946 947 948 949 950 |
# File 'lib/google/cloud/bigquery/project.rb', line 946 def schema s = Schema.from_gapi yield s if block_given? s end |
#time(hour, minute, second) ⇒ Bigquery::Time
Creates a Bigquery::Time object to represent a time, independent of a specific date.
909 910 911 |
# File 'lib/google/cloud/bigquery/project.rb', line 909 def time hour, minute, second Bigquery::Time.new "#{hour}:#{minute}:#{second}" end |