1
from google.cloud import bigquery
import datalab.bigquery as bq

query_job = client.run_sync_query("""
    SELECT
      CONCAT(
        'https://stackoverflow.com/questions/',
        CAST(id as STRING)) as url,
       view_count
    FROM `bigquery-public-data.stackoverflow.posts_questions`
    WHERE tags like '%google-bigquery%'
ORDER BY view_count DESC
LIMIT 10""")

results = query_job.result()  # Waits for job to complete.

for row in results:
    print("{}".format(row.start_time))

I tried to run above code in jupyter notebook but the system gave "AttributeError: 'QueryResults' object has no attribute 'result'"

I cannot find the document to replace the result object.

(the existing document https://cloud.google.com/bigquery/create-simple-app-api is outdated, as run_sync_query is replacing run_query as well) Can you help me?

1 Answers1

2

I made some changes to adapt it using the second example you provided and it's working for me. Code:

from google.cloud import bigquery


client = bigquery.Client()

QUERY = ("""
    SELECT
      CONCAT(
        'https://stackoverflow.com/questions/',
        CAST(id as STRING)) as url,
       view_count
    FROM `bigquery-public-data.stackoverflow.posts_questions`
    WHERE tags like '%google-bigquery%'
ORDER BY view_count DESC
LIMIT 10""")
query_job = client.query(QUERY)

iterator = query_job.result()
rows = list(iterator)

for row in rows:
    print row

And output:

Row((u'https://stackoverflow.com/questions/13530967', 42898), {u'url': 0, u'view_count': 1})
Row((u'https://stackoverflow.com/questions/13221978', 30824), {u'url': 0, u'view_count': 1})
Row((u'https://stackoverflow.com/questions/6607552', 24524), {u'url': 0, u'view_count': 1})
Row((u'https://stackoverflow.com/questions/22004216', 22368), {u'url': 0, u'view_count': 1})
Row((u'https://stackoverflow.com/questions/22879669', 20879), {u'url': 0, u'view_count': 1})
Row((u'https://stackoverflow.com/questions/16609219', 18607), {u'url': 0, u'view_count': 1})
Row((u'https://stackoverflow.com/questions/10644993', 17978), {u'url': 0, u'view_count': 1})
Row((u'https://stackoverflow.com/questions/10604135', 15308), {u'url': 0, u'view_count': 1})
Row((u'https://stackoverflow.com/questions/13468933', 14944), {u'url': 0, u'view_count': 1})
Row((u'https://stackoverflow.com/questions/35159967', 11373), {u'url': 0, u'view_count': 1})
Guillem Xercavins
  • 6,938
  • 1
  • 16
  • 35
  • Thanks for your help, I copied your code and ran in my jupyter, but still i got "'Client' object has no attribute 'query'" – New to Python and Django Jan 26 '18 at 01:06
  • Might be related to the library version. Which one are you using for `google-cloud-bigquery` (use `!pip freeze` to see). Mine was 0.28.0 but I ran it from Cloud Shell. Are you using python2/3? If you are using Datalab you can try [this](https://cloud.google.com/bigquery/docs/visualize-datalab) example. [Code](https://storage.googleapis.com/query-results-48364000/query.txt) - [result](https://storage.googleapis.com/query-results-48364000/result.PNG) – Guillem Xercavins Jan 26 '18 at 09:18