【发布时间】:2018-01-23 17:02:40
【问题描述】:
我想知道如何创建一个 Python 脚本来访问 BigQuery 数据库。 我找到了很多脚本,但不是一个完整的脚本。
所以,我想要一个标准脚本来连接项目并在特定表上进行查询并从中创建一个 csv 文件。
感谢您的帮助。 杰罗姆。
#!/usr/bin/python
from google.cloud import bigquery
import pprint
import argparse
import sys
from apiclient.discovery import build
def export_data_to_gcs(dataset_id, table_id, destination):
bigquery_client = bigquery.Client(project='XXXXXXXX-web-data')
dataset_ref = bigquery_client.dataset(dataset_id)
table_ref = dataset_ref.table(table_id)
job = bigquery_client.extract_table(table_ref, destination)
job.result() # Waits for job to complete
print('Exported {}:{} to {}'.format(
dataset_id, table_id, destination))
export_data_to_gcs('2XXXX842', 'ga_sessions_201XXXXX', 'gs://analytics-to- deci/file-name.json')
【问题讨论】:
-
好的,那么到目前为止你尝试了什么?
-
#!/usr/bin/python from google.cloud import bigquery import pprint import argparse import sys from apiclient.discovery import build def export_data_to_gcs(dataset_id, table_id, destination): bigquery_client = bigquery.Client( ) dataset_ref = bigquery_client.dataset(dataset_id) table_ref = dataset_ref.table(table_id) job = bigquery_client.extract_table(table_ref, destination) job.result() # 等待作业完成 print('Exported {}:{} to {} '.format( dataset_id, table_id, destination)) export_data_to_gcs('2XYZERT42', 'ga_sesXYZ_20180121', 'gs://x')
-
很抱歉很痛苦,但您的问题应该尽可能独立。请将代码(相关部分)放入问题本身
-
你读过docs吗?他们非常好,并且有很多例子。
-
我找到了如何添加project_id。现在我有一个错误 400:Bad request operation cannot be executed on a nested schema。字段:总计
标签: database python-3.x python-requests google-bigquery