Module: BigQuery::Jobs

Included in:
Client
Defined in:
lib/bigquery-client/jobs.rb

Instance Method Summary collapse

Instance Method Details

#fetch_job(id, options = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/bigquery-client/jobs.rb', line 49

def fetch_job(id, options = {})
  access_api(
    api_method: bigquery.jobs.get,
    parameters: { jobId: id }.merge(options)
  )
end

#jobs(options = {}) ⇒ Object



42
43
44
45
46
47
# File 'lib/bigquery-client/jobs.rb', line 42

def jobs(options = {})
  access_api(
    api_method: bigquery.jobs.list,
    parameters: options
  )
end

#jobs_query(query, options = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/bigquery-client/jobs.rb', line 23

def jobs_query(query, options = {})
  default = { query: query, timeoutMs: 600_000 }
  access_api(
    api_method: bigquery.jobs.query,
    body_object: default.merge(options)
  )
end

#load(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/bigquery-client/jobs.rb', line 31

def load(options = {})
  access_api(
    api_method: bigquery.jobs.insert,
    body_object: {
      configuration: {
        load: options
      }
    }
  )
end

#query_results(id, options = {}) ⇒ Object



56
57
58
59
60
61
# File 'lib/bigquery-client/jobs.rb', line 56

def query_results(id, options = {})
  access_api(
    api_method: bigquery.jobs.get_query_results,
    parameters: { jobId: id }.merge(options)
  )
end

#sql(query, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bigquery-client/jobs.rb', line 5

def sql(query, options = {})
  jobs_query_response = jobs_query(query, options)
  fields = jobs_query_response['schema']['fields']
  names = fields.map {|field| field['name'] }
  types = fields.map {|field| field['type'] }
  records = extract_records(jobs_query_response)
  job_id = jobs_query_response['jobReference']['jobId']
  page_token = jobs_query_response['pageToken']

  while page_token
    query_results_response = query_results(job_id, { pageToken: page_token }.merge(options))
    records += extract_records(query_results_response)
    page_token = query_results_response['pageToken']
  end

  convert(records, types).map { |values| [names, values].transpose.to_h }
end