Class: Google::Cloud::Bigquery::Service
- Inherits:
-
Object
- Object
- Google::Cloud::Bigquery::Service
- Defined in:
- lib/google/cloud/bigquery/service.rb
Constant Summary collapse
- API =
Alias to the Google Client API module
Google::Apis::BigqueryV2
Instance Attribute Summary collapse
- #credentials ⇒ Object
-
#mocked_service ⇒ Object
Returns the value of attribute mocked_service.
- #project ⇒ Object
Class Method Summary collapse
-
.table_ref_from_s(str, default_table_ref) ⇒ Object
Extracts at least
tblgroup, and possiblydtsandprjgroups, from strings in the formats: “my_table”, “my_dataset.my_table”, or “my-project:my_dataset.my_table”.
Instance Method Summary collapse
- #copy_table(source, target, options = {}) ⇒ Object
-
#delete_dataset(dataset_id, force = nil) ⇒ Object
Deletes the dataset specified by the datasetId value.
-
#delete_table(dataset_id, table_id) ⇒ Object
Deletes the table specified by tableId from the dataset.
- #extract_table(table, storage_files, options = {}) ⇒ Object
-
#get_dataset(dataset_id) ⇒ Object
Returns the dataset specified by datasetID.
-
#get_job(job_id) ⇒ Object
Returns the job specified by jobID.
- #get_project_table(project_id, dataset_id, table_id) ⇒ Object
-
#get_table(dataset_id, table_id) ⇒ Object
Gets the specified table resource by table ID.
-
#initialize(project, credentials, retries: nil, timeout: nil) ⇒ Service
constructor
Creates a new Service instance.
-
#insert_dataset(new_dataset_gapi) ⇒ Object
Creates a new empty dataset.
- #insert_job(config) ⇒ Object
-
#insert_table(dataset_id, new_table_gapi) ⇒ Object
Creates a new, empty table in the dataset.
- #insert_tabledata(dataset_id, table_id, rows, options = {}) ⇒ Object
- #inspect ⇒ Object
-
#job_query_results(job_id, options = {}) ⇒ Object
Returns the query data for the job.
-
#list_datasets(options = {}) ⇒ Object
Lists all datasets in the specified project to which you have been granted the READER dataset role.
-
#list_jobs(options = {}) ⇒ Object
Lists all jobs in the specified project to which you have been granted the READER job role.
-
#list_tabledata(dataset_id, table_id, options = {}) ⇒ Object
Retrieves data from the table.
-
#list_tables(dataset_id, options = {}) ⇒ Object
Lists all tables in the specified dataset.
- #load_table_file(dataset_id, table_id, file, options = {}) ⇒ Object
- #load_table_gs_url(dataset_id, table_id, url, options = {}) ⇒ Object
-
#patch_dataset(dataset_id, patched_dataset_gapi) ⇒ Object
Updates information in an existing dataset, only replacing fields that are provided in the submitted dataset resource.
-
#patch_table(dataset_id, table_id, patched_table_gapi) ⇒ Object
Updates information in an existing table, replacing fields that are provided in the submitted table resource.
- #query(query, options = {}) ⇒ Object
- #query_job(query, options = {}) ⇒ Object
- #service ⇒ Object
Constructor Details
#initialize(project, credentials, retries: nil, timeout: nil) ⇒ Service
Creates a new Service instance.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/google/cloud/bigquery/service.rb', line 41 def initialize project, credentials, retries: nil, timeout: nil @project = project @credentials = credentials @credentials = credentials @service = API::BigqueryService.new @service..application_name = "google-cloud-bigquery" @service..application_version = \ Google::Cloud::Bigquery::VERSION @service..retries = retries || 3 @service..timeout_sec = timeout if timeout @service. = @credentials.client end |
Instance Attribute Details
#credentials ⇒ Object
37 38 39 |
# File 'lib/google/cloud/bigquery/service.rb', line 37 def credentials @credentials end |
#mocked_service ⇒ Object
Returns the value of attribute mocked_service.
58 59 60 |
# File 'lib/google/cloud/bigquery/service.rb', line 58 def mocked_service @mocked_service end |
#project ⇒ Object
34 35 36 |
# File 'lib/google/cloud/bigquery/service.rb', line 34 def project @project end |
Class Method Details
.table_ref_from_s(str, default_table_ref) ⇒ Object
Extracts at least tbl group, and possibly dts and prj groups, from strings in the formats: “my_table”, “my_dataset.my_table”, or “my-project:my_dataset.my_table”. Then merges project_id and dataset_id from the default table if they are missing.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/google/cloud/bigquery/service.rb', line 265 def self.table_ref_from_s str, default_table_ref str = str.to_s m = /\A(((?<prj>\S*):)?(?<dts>\S*)\.)?(?<tbl>\S*)\z/.match str unless m fail ArgumentError, "unable to identify table from #{str.inspect}" end str_table_ref_hash = { project_id: m["prj"], dataset_id: m["dts"], table_id: m["tbl"] }.delete_if { |_, v| v.nil? } new_table_ref_hash = default_table_ref.to_h.merge str_table_ref_hash Google::Apis::BigqueryV2::TableReference.new new_table_ref_hash end |
Instance Method Details
#copy_table(source, target, options = {}) ⇒ Object
229 230 231 232 233 234 |
# File 'lib/google/cloud/bigquery/service.rb', line 229 def copy_table source, target, = {} execute do service.insert_job @project, copy_table_config( source, target, ) end end |
#delete_dataset(dataset_id, force = nil) ⇒ Object
Deletes the dataset specified by the datasetId value. Before you can delete a dataset, you must delete all its tables, either manually or by specifying force: true in options. Immediately after deletion, you can create another dataset with the same name.
98 99 100 101 102 |
# File 'lib/google/cloud/bigquery/service.rb', line 98 def delete_dataset dataset_id, force = nil execute do service.delete_dataset @project, dataset_id, delete_contents: force end end |
#delete_table(dataset_id, table_id) ⇒ Object
Deletes the table specified by tableId from the dataset. If the table contains data, all the data will be deleted.
147 148 149 |
# File 'lib/google/cloud/bigquery/service.rb', line 147 def delete_table dataset_id, table_id execute { service.delete_table @project, dataset_id, table_id } end |
#extract_table(table, storage_files, options = {}) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/google/cloud/bigquery/service.rb', line 236 def extract_table table, storage_files, = {} execute do service.insert_job \ @project, extract_table_config(table, storage_files, ) end end |
#get_dataset(dataset_id) ⇒ Object
Returns the dataset specified by datasetID.
73 74 75 |
# File 'lib/google/cloud/bigquery/service.rb', line 73 def get_dataset dataset_id execute { service.get_dataset @project, dataset_id } end |
#get_job(job_id) ⇒ Object
Returns the job specified by jobID.
196 197 198 |
# File 'lib/google/cloud/bigquery/service.rb', line 196 def get_job job_id execute { service.get_job @project, job_id } end |
#get_project_table(project_id, dataset_id, table_id) ⇒ Object
115 116 117 |
# File 'lib/google/cloud/bigquery/service.rb', line 115 def get_project_table project_id, dataset_id, table_id execute { service.get_table project_id, dataset_id, table_id } end |
#get_table(dataset_id, table_id) ⇒ Object
Gets the specified table resource by table ID. This method does not return the data in the table, it only returns the table resource, which describes the structure of this table.
124 125 126 |
# File 'lib/google/cloud/bigquery/service.rb', line 124 def get_table dataset_id, table_id execute { get_project_table @project, dataset_id, table_id } end |
#insert_dataset(new_dataset_gapi) ⇒ Object
Creates a new empty dataset.
79 80 81 |
# File 'lib/google/cloud/bigquery/service.rb', line 79 def insert_dataset new_dataset_gapi execute { service.insert_dataset @project, new_dataset_gapi } end |
#insert_job(config) ⇒ Object
200 201 202 203 204 205 |
# File 'lib/google/cloud/bigquery/service.rb', line 200 def insert_job config job_object = API::Job.new( configuration: config ) execute { service.insert_job @project, job_object } end |
#insert_table(dataset_id, new_table_gapi) ⇒ Object
Creates a new, empty table in the dataset.
130 131 132 |
# File 'lib/google/cloud/bigquery/service.rb', line 130 def insert_table dataset_id, new_table_gapi execute { service.insert_table @project, dataset_id, new_table_gapi } end |
#insert_tabledata(dataset_id, table_id, rows, options = {}) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/google/cloud/bigquery/service.rb', line 162 def insert_tabledata dataset_id, table_id, rows, = {} insert_rows = Array(rows).map do |row| Google::Apis::BigqueryV2::InsertAllTableDataRequest::Row.new( insert_id: Digest::MD5.base64digest(row.inspect), # Hash[row.map{|(k,v)| [k.to_s,v]}] for Hash<String,Object> json: row ) end insert_req = Google::Apis::BigqueryV2::InsertAllTableDataRequest.new( rows: insert_rows, ignore_unknown_values: [:ignore_unknown], skip_invalid_rows: [:skip_invalid] ) execute do service.insert_all_table_data( @project, dataset_id, table_id, insert_req) end end |
#inspect ⇒ Object
280 281 282 |
# File 'lib/google/cloud/bigquery/service.rb', line 280 def inspect "#{self.class}(#{@project})" end |
#job_query_results(job_id, options = {}) ⇒ Object
Returns the query data for the job
218 219 220 221 222 223 224 225 226 227 |
# File 'lib/google/cloud/bigquery/service.rb', line 218 def job_query_results job_id, = {} execute do service.get_job_query_results @project, job_id, max_results: .delete(:max), page_token: .delete(:token), start_index: .delete(:start), timeout_ms: .delete(:timeout) end end |
#list_datasets(options = {}) ⇒ Object
Lists all datasets in the specified project to which you have been granted the READER dataset role.
63 64 65 66 67 68 69 |
# File 'lib/google/cloud/bigquery/service.rb', line 63 def list_datasets = {} execute do service.list_datasets \ @project, all: [:all], max_results: [:max], page_token: [:token] end end |
#list_jobs(options = {}) ⇒ Object
Lists all jobs in the specified project to which you have been granted the READER job role.
185 186 187 188 189 190 191 192 |
# File 'lib/google/cloud/bigquery/service.rb', line 185 def list_jobs = {} execute do service.list_jobs \ @project, all_users: [:all], max_results: [:max], page_token: [:token], projection: "full", state_filter: [:filter] end end |
#list_tabledata(dataset_id, table_id, options = {}) ⇒ Object
Retrieves data from the table.
153 154 155 156 157 158 159 160 |
# File 'lib/google/cloud/bigquery/service.rb', line 153 def list_tabledata dataset_id, table_id, = {} execute do service.list_table_data @project, dataset_id, table_id, max_results: .delete(:max), page_token: .delete(:token), start_index: .delete(:start) end end |
#list_tables(dataset_id, options = {}) ⇒ Object
Lists all tables in the specified dataset. Requires the READER dataset role.
107 108 109 110 111 112 113 |
# File 'lib/google/cloud/bigquery/service.rb', line 107 def list_tables dataset_id, = {} execute do service.list_tables @project, dataset_id, max_results: [:max], page_token: [:token] end end |
#load_table_file(dataset_id, table_id, file, options = {}) ⇒ Object
251 252 253 254 255 256 257 258 |
# File 'lib/google/cloud/bigquery/service.rb', line 251 def load_table_file dataset_id, table_id, file, = {} execute do service.insert_job \ @project, load_table_file_config( dataset_id, table_id, file, ), upload_source: file, content_type: mime_type_for(file) end end |
#load_table_gs_url(dataset_id, table_id, url, options = {}) ⇒ Object
243 244 245 246 247 248 249 |
# File 'lib/google/cloud/bigquery/service.rb', line 243 def load_table_gs_url dataset_id, table_id, url, = {} execute do service.insert_job \ @project, load_table_url_config(dataset_id, table_id, url, ) end end |
#patch_dataset(dataset_id, patched_dataset_gapi) ⇒ Object
Updates information in an existing dataset, only replacing fields that are provided in the submitted dataset resource.
86 87 88 89 90 |
# File 'lib/google/cloud/bigquery/service.rb', line 86 def patch_dataset dataset_id, patched_dataset_gapi execute do service.patch_dataset @project, dataset_id, patched_dataset_gapi end end |
#patch_table(dataset_id, table_id, patched_table_gapi) ⇒ Object
Updates information in an existing table, replacing fields that are provided in the submitted table resource.
137 138 139 140 141 142 |
# File 'lib/google/cloud/bigquery/service.rb', line 137 def patch_table dataset_id, table_id, patched_table_gapi execute do service.patch_table @project, dataset_id, table_id, patched_table_gapi end end |
#query(query, options = {}) ⇒ Object
212 213 214 |
# File 'lib/google/cloud/bigquery/service.rb', line 212 def query query, = {} execute { service.query_job @project, query_config(query, ) } end |
#query_job(query, options = {}) ⇒ Object
207 208 209 210 |
# File 'lib/google/cloud/bigquery/service.rb', line 207 def query_job query, = {} config = query_table_config(query, ) execute { service.insert_job @project, config } end |
#service ⇒ Object
54 55 56 57 |
# File 'lib/google/cloud/bigquery/service.rb', line 54 def service return mocked_service if mocked_service @service end |