【问题标题】:'QueryResults' object has no attribute 'result''QueryResults' 对象没有属性 'result'
【发布时间】:2018-06-30 01:06:48
【问题描述】:
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))

我尝试在 jupyter notebook 中运行以上代码,但系统给出“AttributeError: 'QueryResults' object has no attribute 'result'”

我找不到替换结果对象的文档。

(现有文档 https://cloud.google.com/bigquery/create-simple-app-api 已过时,因为 run_sync_query 也正在替换 run_query) 你能帮帮我吗?

【问题讨论】:

标签: sql google-bigquery jupyter-notebook


【解决方案1】:

我使用您提供的第二个示例进行了一些更改以适应它,它对我有用。代码:

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

然后输出:

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})

【讨论】:

  • 感谢您的帮助,我复制了您的代码并在我的 jupyter 中运行,但仍然得到“'Client' object has no attribute 'query'”
  • 可能与库版本有关。 google-cloud-bigquery 使用的是哪一个(使用 !pip freeze 来查看)。我的是 0.28.0,但我从 Cloud Shell 运行它。你在使用 python2/3 吗?如果您使用的是 Datalab,您可以尝试 this 示例。 Code - result
猜你喜欢
  • 2022-11-03
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多