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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/big_query/client/query.rb', line 36

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( current_row < res['totalRows'].to_i && ( res = get_query_results(job_id, :startIndex => current_row) ) && res['rows'] ) 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)

    or timeoutMs (90 * 1000) timeout in miliseconds

  • dryRun (Boolean)

    Don’t actually run this job

  • maxResults (Integer)

    The maximum number of rows of data to return per page of results.

  • useQueryCache (Boolean)

    Whether to look for the result in the query cache.

Returns:

  • (Hash)

    json api response

See Also:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/big_query/client/query.rb', line 14

def query(given_query, options={})
  body_object = { 'query' => given_query }
  body_object['timeoutMs']     = options[:timeout] || options[:timeoutMs] || 90 * 1000
  body_object['maxResults']    = options[:maxResults] if options[:maxResults]
  body_object['dryRun']        = options[:dryRun] if options.has_key?(:dryRun)
  body_object['useQueryCache'] = options[:useQueryCache] if options.has_key?(:useQueryCache)

  response = api(
    api_method: @bq.jobs.query,
    body_object: body_object,
  )

  response
end