数据集没有csv格式,而且很大,所以使用接口来查询数据。

前置准备:

谷歌账号

过程:

https://cloud.google.com/bigquery/docs/reference/libraries#client-libraries-resources-python

主要在这里进行操作

 在环境变量里配置经常会莫名其妙出现错误

google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

解决方法有两个:

1、拼命老哥的方法:在pycharm里添加在pycharm虚拟环境设置环境变量的查错和解决经历_拼命先生的AI之旅-CSDN博客_pycharm虚拟环境设置

但是我按照上面的教程走,依然报错,而且重启pycharm也没有用

2、我自己的方法:

在系统高级设置里添加变量

然后 

 选择环境变量

新建里面添加变量和之前下载秘钥的位置(还要带上文件名一起)

 

在jupyter里重新使用:(pychram还是没有办法使用)

from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
query = """
    SELECT name, SUM(number) as total_people
    FROM `bigquery-public-data.usa_names.usa_1910_2013`
    WHERE state = 'TX'
    GROUP BY name, state
    ORDER BY total_people DESC
    LIMIT 20
"""
query_job = client.query(query)  # Make an API request.

print("The query data:")
for row in query_job:
    # Row values can be accessed by field name or index.
    print("name={}, count={}".format(row[0], row["total_people"]))

 然后可以运行了

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐