如何在Python中运行BigQuery查询

W. *_*ens 4 python google-bigquery

这是我在BigQuery中运行的查询,我想在我的python脚本中运行.我将如何更改此/我必须添加什么才能在Python中运行它.

#standardSQL
SELECT
  Serial,
  MAX(createdAt) AS Latest_Use,
  SUM(ConnectionTime/3600) as Total_Hours,
  COUNT(DISTINCT DeviceID) AS Devices_Connected
FROM `dataworks-356fa.FirebaseArchive.testf`
WHERE Model = "BlueBox-pH"
GROUP BY Serial
ORDER BY Serial
LIMIT 1000;
Run Code Online (Sandbox Code Playgroud)

从我一直在研究的是,我不能将这个查询保存为使用Python的永久表.真的吗?如果是真的,是否仍然可以导出临时表?

Gra*_*ley 9

您需要使用BigQuery Python客户端库,然后这样的事情可以启动并运行:

from google.cloud import bigquery
client = bigquery.Client(project='PROJECT_ID')
query = "SELECT...."
dataset = client.dataset('dataset')
table = dataset.table(name='table')
job = client.run_async_query('my-job', query)
job.destination = table
job.write_disposition= 'WRITE_TRUNCATE'
job.begin()
Run Code Online (Sandbox Code Playgroud)

https://googlecloudplatform.github.io/google-cloud-python/stable/bigquery-usage.html

查看当前的BigQuery Python客户端教程.


Azi*_*lto 5

这是使用服务帐户的 JSON 文件的另一种方法:

>>> from google.cloud import bigquery
>>>
>>> CREDS = 'test_service_account.json'
>>> client = bigquery.Client.from_service_account_json(json_credentials_path=CREDS)
>>> job = client.query('select * from dataset1.mytable')
>>> for row in job.result():
...     print(row)
Run Code Online (Sandbox Code Playgroud)