在使用 Bigquery 和 Google Sheet 进行数据导入时,可能会出现权限问题,导致数据不能正确导入。解决方法是将 Bigquery 和 Google Sheet 的访问权限设置正确。
具体步骤如下:
示例代码:
import os
from google.oauth2 import service_account
from google.cloud import bigquery
from googleapiclient.discovery import build
# 读取 Service Account 密钥文件
credentials = service_account.Credentials.from_service_account_file(
'/path/to/service_account_key.json')
# 在 BigQuery 中导入数据
client = bigquery.Client(credentials=credentials, project=project_id)
job_config = bigquery.LoadJobConfig()
job_config.autodetect = True
table_ref = client.dataset(dataset_id).table(table_name)
load_job = client.load_table_from_uri(uri, table_ref, job_config=job_config)
load_job.result()
# 在 Google Sheet 中导入数据
service = build('sheets', 'v4', credentials=credentials)
result = service.spreadsheets().values().update(
spreadsheetId=spreadsheet_id, range=range_name,
valueInputOption='USER_ENTERED', body=value_range).execute()
print('{0} cells updated.'.format(result.get('updatedCells')))