Module: BigQuery::Client::Query

Included in:
BigQuery::Client
Defined in:
lib/big_query/client/query.rb

Instance Method Summary collapse

Instance Method Details

#each_row(q, options = {}, &block) ⇒ Object

perform a query synchronously fetch all result rows, even when that takes >1 query invoke /block/ once for each row, passing the row

Parameters:

  • q (String)

    query to be executed

  • options (Hash) (defaults to: {})

    query options

Options Hash (options):

  • timeout (Integer) — default: 90 * 1000

    timeout in miliseconds



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/big_query/client/query.rb', line 28

def each_row(q, options = {}, &block)
  current_row = 0
  # repeatedly fetch results, starting from current_row
  # invoke the block on each one, then grab next page if there is one
  # it'll terminate when res has no 'rows' key or when we've done enough rows
  # perform query...
  res = query(q, options)
  job_id = res['jobReference']['jobId']
  # call the block on the first page of results
  if( res && res['rows'] )
    res['rows'].each(&block)
    current_row += res['rows'].size
  end
  # keep grabbing pages from the API and calling the block on each row
  while(( res = get_query_results(job_id, :startIndex => current_row) ) && res['rows'] && current_row < res['totalRows'].to_i ) do
    res['rows'].each(&block)
    current_row += res['rows'].size
  end
end

#query(given_query, options = {}) ⇒ Hash

Performs the given query in the bigquery api

Parameters:

  • given_query (String)

    query to perform

  • options (Hash) (defaults to: {})

    query options

Options Hash (options):

  • timeout (Integer) — default: 90 * 1000

    timeout in miliseconds

Returns:

  • (Hash)

    json api response



10
11
12
13
14
15
16
17
18
19
# File 'lib/big_query/client/query.rb', line 10

def query(given_query, options={})
  timeout = options.fetch(:timeout, 90 * 1000)
  response = api(
    api_method: @bq.jobs.query,
    body_object: { 'query' => given_query,
                   'timeoutMs' => timeout}
  )

  response
end