Module: BigQuery::Client::Query
- Included in:
- BigQuery::Client
- Defined in:
- lib/big_query/client/query.rb
Instance Method Summary collapse
-
#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.
-
#query(given_query, options = {}) ⇒ Hash
Performs the given query in the bigquery api.
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
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, = {}, &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, ) 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
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, ={}) body_object = { 'query' => given_query } body_object['timeoutMs'] = [:timeout] || [:timeoutMs] || 90 * 1000 body_object['maxResults'] = [:maxResults] if [:maxResults] body_object['dryRun'] = [:dryRun] if .has_key?(:dryRun) body_object['useQueryCache'] = [:useQueryCache] if .has_key?(:useQueryCache) response = api( api_method: @bq.jobs.query, body_object: body_object, ) response end |