Class: Kura::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kura/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(default_project_id: nil, email_address: nil, private_key: nil, http_options: {timeout: 60}, default_retries: 5) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kura/client.rb', line 11

def initialize(default_project_id: nil, email_address: nil, private_key: nil, http_options: {timeout: 60}, default_retries: 5)
  @default_project_id = default_project_id
  @scope = "https://www.googleapis.com/auth/bigquery"
  @email_address = email_address
  @private_key = private_key
  if @email_address and @private_key
    auth = Signet::OAuth2::Client.new(
      token_credential_uri: "https://accounts.google.com/o/oauth2/token",
      audience: "https://accounts.google.com/o/oauth2/token",
      scope: @scope,
      issuer: @email_address,
      signing_key: @private_key)
    # MEMO: signet-0.6.1 depend on Farady.default_connection
    Faraday.default_connection.options.timeout = 60
    auth.fetch_access_token!
  else
    auth = Google::Auth.get_application_default([@scope])
    auth.fetch_access_token!
  end
  Google::Apis::RequestOptions.default.retries = default_retries
  Google::Apis::RequestOptions.default.timeout_sec = http_options[:timeout]
  @api = Google::Apis::BigqueryV2::BigqueryService.new
  @api.authorization = auth

  if @default_project_id.nil?
    @default_project_id = self.projects.first.id
  end
end

Instance Method Details

#_convert_tabledata_field(x, field_info) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/kura/client.rb', line 232

def _convert_tabledata_field(x, field_info)
  if x.nil? and field_info["mode"] == "NULLABLE"
    return nil
  end
  case field_info["type"]
  when "STRING"
    x.to_s
  when "INTEGER"
    Integer(x)
  when "FLOAT"
    Float(x)
  when "BOOLEAN"
    x.to_s == "true"
  when "RECORD"
    _convert_tabledata_row(x, field_info["fields"])
  else
    x
  end
end

#_convert_tabledata_row(row, schema) ⇒ Object



252
253
254
255
256
257
258
259
260
261
# File 'lib/kura/client.rb', line 252

def _convert_tabledata_row(row, schema)
  (row.respond_to?(:f) ? row.f : row["f"]).zip(schema).each_with_object({}) do |(v, s), tbl|
    v = JSON.parse(v.to_json)
    if s["mode"] == "REPEATED"
      tbl[s["name"]] = v["v"].map{|c| _convert_tabledata_field(c["v"], s) }
    else
      tbl[s["name"]] = _convert_tabledata_field(v["v"], s)
    end
  end
end

#batchObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/kura/client.rb', line 67

def batch
  @api.batch do |api|
    original_api, @api = @api, api
    begin
      yield
    ensure
      @api = original_api
    end
  end
end

#cancel_job(job, project_id: @default_project_id, &blk) ⇒ Object



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/kura/client.rb', line 571

def cancel_job(job, project_id: @default_project_id, &blk)
  case job
  when String
    jobid = job
  when Google::Apis::BigqueryV2::Job
    project_id = job.job_reference.project_id
    jobid = job.job_reference.job_id
  else
    raise TypeError, "Kura::Client#cancel_job accept String(job-id) or Google::Apis::BigqueryV2::Job"
  end
  if blk
    @api.cancel_job(project_id, jobid) do |r, e|
      r.job.kura_api = self if r.job
      blk.call(r.job, e)
    end
  else
    @api.cancel_job(project_id, jobid).job.tap{|j| j.kura_api = self if j }
  end
end

#copy(src_dataset_id, src_table_id, dest_dataset_id, dest_table_id, mode: :truncate, src_project_id: @default_project_id, dest_project_id: @default_project_id, job_project_id: @default_project_id, job_id: nil, wait: nil, dry_run: false, &blk) ⇒ Object



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/kura/client.rb', line 526

def copy(src_dataset_id, src_table_id, dest_dataset_id, dest_table_id,
         mode: :truncate,
         src_project_id: @default_project_id,
         dest_project_id: @default_project_id,
         job_project_id: @default_project_id,
         job_id: nil,
         wait: nil,
         dry_run: false,
         &blk)
  write_disposition = mode_to_write_disposition(mode)
  configuration = Google::Apis::BigqueryV2::JobConfiguration.new({
    copy: Google::Apis::BigqueryV2::JobConfigurationTableCopy.new({
      destination_table: Google::Apis::BigqueryV2::TableReference.new({
        project_id: dest_project_id,
        dataset_id: dest_dataset_id,
        table_id: dest_table_id,
      }),
      source_table: Google::Apis::BigqueryV2::TableReference.new({
        project_id: src_project_id,
        dataset_id: src_dataset_id,
        table_id: src_table_id,
      }),
      write_disposition: write_disposition,
    })
  })
  if dry_run
    configuration.dry_run = true
    wait = nil
  end
  insert_job(configuration, wait: wait, job_id: job_id, project_id: job_project_id, &blk)
end

#dataset(dataset_id, project_id: @default_project_id, &blk) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/kura/client.rb', line 107

def dataset(dataset_id, project_id: @default_project_id, &blk)
  if blk
    @api.get_dataset(project_id, dataset_id) do |result, err|
      if err.respond_to?(:status_code) and err.status_code == 404
        result = nil
        err = nil
      end
      blk.call(result, err)
    end
  else
    @api.get_dataset(project_id, dataset_id)
  end
rescue
  return nil if $!.respond_to?(:status_code) and $!.status_code == 404
  process_error($!)
end

#datasets(project_id: @default_project_id, all: false, limit: 1000, &blk) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kura/client.rb', line 92

def datasets(project_id: @default_project_id, all: false, limit: 1000, &blk)
  all = normalize_parameter(all)
  if blk
    @api.list_datasets(project_id, all: all, max_results: limit) do |result, err|
      result &&= (result.datasets || [])
      blk.call(result, err)
    end
  else
    result = @api.list_datasets(project_id, all: all, max_results: limit)
    result.datasets || []
  end
rescue
  process_error($!)
end

#delete_dataset(dataset_id, project_id: @default_project_id, delete_contents: false, &blk) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/kura/client.rb', line 131

def delete_dataset(dataset_id, project_id: @default_project_id, delete_contents: false, &blk)
  delete_contents = normalize_parameter(delete_contents)
  @api.delete_dataset(project_id, dataset_id, delete_contents: delete_contents, &blk)
rescue
  return nil if $!.respond_to?(:status_code) and $!.status_code == 404
  process_error($!)
end

#delete_table(dataset_id, table_id, project_id: @default_project_id, &blk) ⇒ Object



225
226
227
228
229
230
# File 'lib/kura/client.rb', line 225

def delete_table(dataset_id, table_id, project_id: @default_project_id, &blk)
  @api.delete_table(project_id, dataset_id, table_id, &blk)
rescue
  return nil if $!.respond_to?(:status_code) and $!.status_code == 404
  process_error($!)
end

#extract(dataset_id, table_id, dest_uris, compression: "NONE", destination_format: "CSV", field_delimiter: ",", print_header: true, project_id: @default_project_id, job_project_id: @default_project_id, job_id: nil, wait: nil, dry_run: false, &blk) ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/kura/client.rb', line 491

def extract(dataset_id, table_id, dest_uris,
            compression: "NONE",
            destination_format: "CSV",
            field_delimiter: ",",
            print_header: true,
            project_id: @default_project_id,
            job_project_id: @default_project_id,
            job_id: nil,
            wait: nil,
            dry_run: false,
            &blk)
  dest_uris = [ dest_uris ] if dest_uris.is_a?(String)
  configuration = Google::Apis::BigqueryV2::JobConfiguration.new({
    extract: Google::Apis::BigqueryV2::JobConfigurationExtract.new({
      compression: compression,
      destination_format: destination_format,
      source_table: Google::Apis::BigqueryV2::TableReference.new({
        project_id: project_id,
        dataset_id: dataset_id,
        table_id: table_id,
      }),
      destination_uris: dest_uris,
    })
  })
  if dry_run
    configuration.dry_run = true
    wait = nil
  end
  if destination_format == "CSV"
    configuration.extract.field_delimiter = field_delimiter
    configuration.extract.print_header = normalize_parameter(print_header)
  end
  insert_job(configuration, wait: wait, job_id: job_id, project_id: job_project_id, &blk)
end

#insert_dataset(dataset_id, project_id: @default_project_id, &blk) ⇒ Object



124
125
126
127
128
129
# File 'lib/kura/client.rb', line 124

def insert_dataset(dataset_id, project_id: @default_project_id, &blk)
  obj = Google::Apis::BigqueryV2::Dataset.new(dataset_reference: Google::Apis::BigqueryV2::DatasetReference.new(project_id: project_id, dataset_id: dataset_id))
  @api.insert_dataset(project_id, obj, &blk)
rescue
  process_error($!)
end

#insert_job(configuration, job_id: nil, project_id: @default_project_id, media: nil, wait: nil, &blk) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/kura/client.rb', line 331

def insert_job(configuration, job_id: nil, project_id: @default_project_id, media: nil, wait: nil, &blk)
  job_object = Google::Apis::BigqueryV2::Job.new
  job_object.configuration = configuration
  if job_id
    job_object.job_reference = Google::Apis::BigqueryV2::JobReference.new
    job_object.job_reference.project_id = project_id
    job_object.job_reference.job_id = job_id
  end
  if wait
    job = @api.insert_job(project_id, job_object, upload_source: media)
    job.kura_api = self
    wait_job(job, wait, &blk)
  else
    if blk
      @api.insert_job(project_id, job_object, upload_source: media) do |r, err|
        if r
          r.kura_api = self
        end
        blk.call(r, err)
      end
    else
      job = @api.insert_job(project_id, job_object, upload_source: media)
      job.kura_api = self
      job
    end
  end
rescue
  process_error($!)
end

#insert_table(dataset_id, table_id, project_id: @default_project_id, expiration_time: nil, friendly_name: nil, schema: nil, description: nil, query: nil, external_data_configuration: nil, use_legacy_sql: true, time_partitioning: nil, &blk) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/kura/client.rb', line 181

def insert_table(dataset_id, table_id, project_id: @default_project_id, expiration_time: nil,
                 friendly_name: nil, schema: nil, description: nil,
                 query: nil, external_data_configuration: nil,
                 use_legacy_sql: true,
                 time_partitioning: nil,
                 &blk)
  if expiration_time
    expiration_time = (expiration_time.to_f * 1000.0).to_i
  end
  if query
    view = { query: query, use_legacy_sql: !!use_legacy_sql }
  elsif external_data_configuration
  elsif schema
    schema = { fields: normalize_schema(schema) }
  end
  table = Google::Apis::BigqueryV2::Table.new(
    table_reference: {project_id: project_id, dataset_id: dataset_id, table_id: table_id},
    friendly_name: friendly_name,
    description: description,
    schema: schema,
    expiration_time: expiration_time,
    view: view,
    external_data_configuration: external_data_configuration)
  if time_partitioning
    table.time_partitioning = Google::Apis::BigqueryV2::TimePartitioning.new(time_partitioning)
  end
  @api.insert_table(project_id, dataset_id, table, &blk)
rescue
  process_error($!)
end

#insert_tabledata(dataset_id, table_id, rows, project_id: @default_project_id, ignore_unknown_values: false, skip_invalid_rows: false, template_suffix: nil) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/kura/client.rb', line 293

def insert_tabledata(dataset_id, table_id, rows, project_id: @default_project_id, ignore_unknown_values: false, skip_invalid_rows: false, template_suffix: nil)
  request = Google::Apis::BigqueryV2::InsertAllTableDataRequest.new
  request.ignore_unknown_values = ignore_unknown_values
  request.skip_invalid_rows = skip_invalid_rows
  if template_suffix
    request.template_suffix = template_suffix
  end
  request.rows = rows.map do |r|
    case r
    when Google::Apis::BigqueryV2::InsertAllTableDataRequest::Row
      r
    when Hash
      row = Google::Apis::BigqueryV2::InsertAllTableDataRequest::Row.new
      if r.keys.map(&:to_s) == %w{ insert_id json }
        row.insert_id = r[:insert_id] || r["insert_id"]
        row.json = r[:json] || r["json"]
      else
        row.json = r
      end
      row
    else
      raise ArgumentError, "invalid row for BigQuery tabledata.insertAll #{r.inspect}"
    end
  end

  r = @api.insert_all_table_data(project_id, dataset_id, table_id, request)
rescue
  process_error($!)
end

#job(job_id, project_id: @default_project_id, &blk) ⇒ Object



558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/kura/client.rb', line 558

def job(job_id, project_id: @default_project_id, &blk)
  if blk
    @api.get_job(project_id, job_id) do |j, e|
      j.kura_api = self if j
      blk.call(j, e)
    end
  else
    @api.get_job(project_id, job_id).tap{|j| j.kura_api = self if j }
  end
rescue
  process_error($!)
end

#job_finished?(r) ⇒ Boolean

Returns:

  • (Boolean)


591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/kura/client.rb', line 591

def job_finished?(r)
  if r.status.state == "DONE"
    if r.status.error_result
      raise Kura::ApiError.new(r.status.errors.map(&:reason).join(","),
                               r.status.errors.map{|e|
                                 msg = "reason=#{e.reason} message=#{e.message}"
                                 msg += " location=#{e.location}" if e.location
                                 msg += " debug_infoo=#{e.debug_info}" if e.debug_info
                                 msg
                               }.join("\n"))
    end
    return true
  end
  return false
end

#list_tabledata(dataset_id, table_id, project_id: @default_project_id, start_index: 0, max_result: 100, page_token: nil, schema: nil, &blk) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/kura/client.rb', line 274

def list_tabledata(dataset_id, table_id, project_id: @default_project_id, start_index: 0, max_result: 100, page_token: nil, schema: nil, &blk)
  schema ||= table(dataset_id, table_id, project_id: project_id).schema.fields
  schema = schema.map{|s| JSON.parse(s.to_json) }

  if blk
    @api.list_table_data(project_id, dataset_id, table_id, max_results: max_result, start_index: start_index, page_token: page_token) do |r, err|
      if r
        r = format_tabledata(r, schema)
      end
      blk.call(r, err)
    end
  else
    r = @api.list_table_data(project_id, dataset_id, table_id, max_results: max_result, start_index: start_index, page_token: page_token)
    format_tabledata(r, schema)
  end
rescue
  process_error($!)
end

#load(dataset_id, table_id, source_uris = nil, schema: nil, delimiter: ",", field_delimiter: delimiter, mode: :append, allow_jagged_rows: false, max_bad_records: 0, ignore_unknown_values: false, allow_quoted_newlines: false, quote: '"', skip_leading_rows: 0, source_format: "CSV", project_id: @default_project_id, job_project_id: @default_project_id, job_id: nil, file: nil, wait: nil, dry_run: false, &blk) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/kura/client.rb', line 443

def load(dataset_id, table_id, source_uris=nil,
         schema: nil, delimiter: ",", field_delimiter: delimiter, mode: :append,
         allow_jagged_rows: false, max_bad_records: 0,
         ignore_unknown_values: false,
         allow_quoted_newlines: false,
         quote: '"', skip_leading_rows: 0,
         source_format: "CSV",
         project_id: @default_project_id,
         job_project_id: @default_project_id,
         job_id: nil,
         file: nil, wait: nil,
         dry_run: false,
         &blk)
  write_disposition = mode_to_write_disposition(mode)
  source_uris = [source_uris] if source_uris.is_a?(String)
  configuration = Google::Apis::BigqueryV2::JobConfiguration.new({
    load: Google::Apis::BigqueryV2::JobConfigurationLoad.new({
      destination_table: Google::Apis::BigqueryV2::TableReference.new({
        project_id: project_id,
        dataset_id: dataset_id,
        table_id: table_id,
      }),
      write_disposition: write_disposition,
      allow_jagged_rows: normalize_parameter(allow_jagged_rows),
      max_bad_records: max_bad_records,
      ignore_unknown_values: normalize_parameter(ignore_unknown_values),
      source_format: source_format,
    })
  })
  if dry_run
    configuration.dry_run = true
    wait = nil
  end
  if schema
    configuration.load.schema = Google::Apis::BigqueryV2::TableSchema.new({ fields: normalize_schema(schema) })
  end
  if source_format == "CSV"
    configuration.load.field_delimiter = field_delimiter
    configuration.load.allow_quoted_newlines = normalize_parameter(allow_quoted_newlines)
    configuration.load.quote = quote
    configuration.load.skip_leading_rows = skip_leading_rows
  end
  unless file
    configuration.load.source_uris = source_uris
  end
  insert_job(configuration, media: file, wait: wait, job_id: job_id, project_id: job_project_id, &blk)
end

#normalize_parameter(v) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/kura/client.rb', line 40

def normalize_parameter(v)
  case v
  when nil
    nil
  else
    v.to_s
  end
end

#patch_dataset(dataset_id, project_id: @default_project_id, access: nil, description: :na, default_table_expiration_ms: :na, friendly_name: :na, &blk) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/kura/client.rb', line 139

def patch_dataset(dataset_id, project_id: @default_project_id, access: nil, description: :na, default_table_expiration_ms: :na, friendly_name: :na, &blk)
  obj = Google::Apis::BigqueryV2::Dataset.new(dataset_reference: Google::Apis::BigqueryV2::DatasetReference.new(project_id: project_id, dataset_id: dataset_id))
  obj.access = access if access
  obj.default_table_expiration_ms = default_table_expiration_ms if default_table_expiration_ms != :na
  obj.description = description if description != :na
  obj.friendly_name = friendly_name if friendly_name != :na
  @api.patch_dataset(project_id, dataset_id, obj, &blk)
rescue
  process_error($!)
end

#patch_table(dataset_id, table_id, project_id: @default_project_id, expiration_time: :na, friendly_name: :na, description: :na, &blk) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/kura/client.rb', line 212

def patch_table(dataset_id, table_id, project_id: @default_project_id, expiration_time: :na, friendly_name: :na, description: :na, &blk)
  if expiration_time != :na and not(expiration_time.nil?)
    expiration_time = (expiration_time.to_f * 1000.0).to_i
  end
  table = Google::Apis::BigqueryV2::Table.new(table_reference: {project_id: project_id, dataset_id: dataset_id, table_id: table_id})
  table.friendly_name = friendly_name if friendly_name != :na
  table.description = description if description != :na
  table.expiration_time = expiration_time if expiration_time != :na
  @api.patch_table(project_id, dataset_id, table_id, table, &blk)
rescue
  process_error($!)
end

#projects(limit: 1000, &blk) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kura/client.rb', line 78

def projects(limit: 1000, &blk)
  if blk
    @api.list_projects(max_results: limit) do |result, err|
      result &&= result.projects
      blk.call(result, err)
    end
  else
    result = @api.list_projects(max_results: limit)
    result.projects
  end
rescue
  process_error($!)
end

#query(sql, mode: :truncate, dataset_id: nil, table_id: nil, allow_large_result: true, allow_large_results: allow_large_result, flatten_results: true, priority: "INTERACTIVE", use_query_cache: true, user_defined_function_resources: nil, use_legacy_sql: true, maximum_billing_tier: nil, maximum_bytes_billed: nil, project_id: @default_project_id, job_project_id: @default_project_id, job_id: nil, wait: nil, dry_run: false, &blk) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/kura/client.rb', line 361

def query(sql, mode: :truncate,
          dataset_id: nil, table_id: nil,
          allow_large_result: true, # for backward compatibility
          allow_large_results: allow_large_result,
          flatten_results: true,
          priority: "INTERACTIVE",
          use_query_cache: true,
          user_defined_function_resources: nil,
          use_legacy_sql: true,
          maximum_billing_tier: nil,
          maximum_bytes_billed: nil,
          project_id: @default_project_id,
          job_project_id: @default_project_id,
          job_id: nil,
          wait: nil,
          dry_run: false,
          &blk)
  configuration = Google::Apis::BigqueryV2::JobConfiguration.new({
    query: Google::Apis::BigqueryV2::JobConfigurationQuery.new({
      query: sql,
      allow_large_results: normalize_parameter(allow_large_results),
      flatten_results: normalize_parameter(flatten_results),
      priority: priority,
      use_query_cache: normalize_parameter(use_query_cache),
      use_legacy_sql: use_legacy_sql,
    })
  })
  if mode
    configuration.query.write_disposition = mode_to_write_disposition(mode)
  end
  if dry_run
    configuration.dry_run = true
    wait = nil
  end
  if maximum_billing_tier
    configuration.query.maximum_billing_tier = maximum_billing_tier
  end
  if maximum_bytes_billed
    configuration.query.maximum_bytes_billed = maximum_bytes_billed
  end
  if dataset_id and table_id
    configuration.query.destination_table = Google::Apis::BigqueryV2::TableReference.new({ project_id: project_id, dataset_id: dataset_id, table_id: table_id })
  end
  if user_defined_function_resources
    configuration.query.user_defined_function_resources = Array(user_defined_function_resources).map do |r|
      r = r.to_s
      if r.start_with?("gs://")
        Google::Apis::BigqueryV2::UserDefinedFunctionResource.new({ resource_uri: r })
      else
        Google::Apis::BigqueryV2::UserDefinedFunctionResource.new({ inline_code: r })
      end
    end
  end
  insert_job(configuration, wait: wait, job_id: job_id, project_id: job_project_id, &blk)
end

#table(dataset_id, table_id, project_id: @default_project_id, &blk) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/kura/client.rb', line 164

def table(dataset_id, table_id, project_id: @default_project_id, &blk)
  if blk
    @api.get_table(project_id, dataset_id, table_id) do |result, err|
      if err.respond_to?(:status_code) and err.status_code == 404
        result = nil
        err = nil
      end
      blk.call(result, err)
    end
  else
    @api.get_table(project_id, dataset_id, table_id)
  end
rescue
  return nil if $!.respond_to?(:status_code) and $!.status_code == 404
  process_error($!)
end

#tables(dataset_id, project_id: @default_project_id, limit: 1000, page_token: nil, &blk) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/kura/client.rb', line 150

def tables(dataset_id, project_id: @default_project_id, limit: 1000, page_token: nil, &blk)
  if blk
    @api.list_tables(project_id, dataset_id, max_results: limit, page_token: page_token) do |result, err|
      result &&= (result.tables || [])
      blk.call(result, err)
    end
  else
    result = @api.list_tables(project_id, dataset_id, max_results: limit, page_token: page_token)
    result.tables || []
  end
rescue
  process_error($!)
end

#wait_job(job, timeout = 60*10, project_id: @default_project_id) ⇒ Object

Raises:



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/kura/client.rb', line 607

def wait_job(job, timeout=60*10, project_id: @default_project_id)
  case job
  when String
    job_id = job
  when Google::Apis::BigqueryV2::Job
    project_id = job.job_reference.project_id
    job_id = job.job_reference.job_id
  else
    raise TypeError, "Kura::Client#wait_job accept String(job-id) or Google::Apis::BigqueryV2::Job"
  end
  expire = Time.now + timeout
  while expire > Time.now
    j = job(job_id, project_id: project_id)
    if job_finished?(j)
      return j
    end
    if block_given?
      yield j
    end
    sleep 1
  end
  raise Kura::TimeoutError, "wait job timeout"
end