Skip to content

BigQuery: Add QueryResults.reload() function which calls getQueryResults #3506

Closed

Description

@tswast

https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/getQueryResults

One of the things I learned when talking to the BigQuery API folks is that the recommended way to run a query is to create a query job and then call getQueryResults() until the jobComplete property is true.

The reason for this is that a call to get the job resource will return immediately, whereas getQueryResults will return after a timeout or the query is complete, whichever comes first. With getQueryResults users get their data faster.

This means the code for waiting for a query job to complete would change from

while True:
   query_job.reload()
    if query_job.state == 'DONE':
        return
    time.sleep(1)

to something like

query_results = query_job.results()
while not query_results.complete:
    query_results.reload()
    # No need for sleep, since reload should wait for timeout

Activity

  1. tswast commented on Jun 15, 2017

    @tswast
    ContributorAuthor

    I figured out that I can force a reload with

        query_iterator = query_results.fetch_data()
        try:
           six.next(iter(query_iterator))
        except StopIteration:
            pass
    

    but obviously, that's not ideal for the purpose of waiting for a job to finish.

  2. tseaver commented on Jun 19, 2017

    @tseaver
    Contributor

    If you are going to iterate over the rows anyway, then that second example becomes:

    for row in query_results.fetch_data():
        do_something_with(row)

    That certainly seems like "idiomatic" usage. Does that accomplish what you need?

  3. tswast commented on Jun 19, 2017

    @tswast
    ContributorAuthor

    No. The results might not all be there. That's why I need a reload function to check if the query is complete or not. (A bit hard to test, since it requires a query that takes longer than the timeout value, which defaults to 10 seconds).

  4. changed the title [-]BigQuery: Add QueryResults.reload() function which calls getQueryResults[/-] [+]BigQuery: make QueryResults implement the futures interface[/+] on Aug 1, 2017
  5. tswast commented on Aug 1, 2017

    @tswast
    ContributorAuthor

    A reload function probably isn't the best solution. After thinking about this, I think QueryResults should also implement the futures interface.

  6. theacodes commented on Aug 1, 2017

    @theacodes
    Contributor

    I don't think QueryResults should implement the future interface, rather, I think that QueryJob (which already has the futures interface) should use the getQueryResults API method instead of getJob.

  7. added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    and removed
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Aug 7, 2017
  8. changed the title [-]BigQuery: make QueryResults implement the futures interface[/-] [+]BigQuery: Add QueryResults.reload() function which calls getQueryResults[/+] on Aug 11, 2017
  9. tswast commented on Aug 11, 2017

    @tswast
    ContributorAuthor

    After I've played around with some proposed designs for GA, I agree with you, Jon. QueryJob's implementation of futures interface will be sufficient.

    I've changed this request back to asking for .reload() method since there does need to be a more straightforward way to actually call getQueryResults.

    Closing this issue since I'm tracking internally in GA redesign doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the BigQuery API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions