Skip to main content

python读取bigquery

安装客户端

pip install --upgrade google-cloud-bigquery[bqstorage,pandas]

导入工具包,创建客户端对象

from google.cloud import bigquery

client = bigquery.Client()

运行查询

query_job = 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""")

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

转成dataframe

df = results.to_dataframe()

显示查询结果

df.iloc[:,:5] #显示dataframe