您好,我克隆了以下存储库:
https://github.com/tylertreat/BigQuery-Python
我正在阅读有关创建表的文档,如下所示:
schema = [
{'name': 'foo', 'type': 'STRING', 'mode': 'nullable'},
{'name': 'bar', 'type': 'FLOAT', 'mode': 'nullable'}
]
created = client.create_table('dataset', 'my_table', schema)
我尝试在控制台中执行以下操作:
>>> schema = [
... {'name': 'foo', 'type': 'STRING', 'mode': 'nullable'},
... {'name': 'bar', 'type': 'FLOAT', 'mode': 'nullable'}
... ]
>>> created = client.create_table('data_set_course', 'my_table_testing', schema)
我只得到'False',并且当我检查可视界面时没有表格。
>>> print(created)
False
>>>
我对这个问题一无所知,我真的很沮丧,因为我只是要创建一个表,所以我非常感谢能克服这一任务的支持。
答案 0 :(得分:1)
我已经测试了这段代码,并且对我有用:
from bigquery import get_client
# JSON key provided by Google
json_key = 'key.json'
client = get_client(json_key_file=json_key, readonly=False)
schema = [
{'name': 'foo', 'type': 'STRING', 'mode': 'nullable'},
{'name': 'bar', 'type': 'FLOAT', 'mode': 'nullable'}
]
created = client.create_table(dataset='yourDataset', table='tableToCreate', schema=schema)
您需要检查两件事:
readonly
方法中的 get_client
参数设置为False
。否则,BigQuery访问权限是只读的。
key.json
文件中提供的服务帐户或用户已被permissions to create a BigQuery table授予。至少它必须具有授予的roles/bigquery.dataEditor
角色。为此,请运行以下命令:
gcloud projects add-iam-policy-binding [PROJECT_ID] --member "serviceAccount:[NAME]@[PROJECT_ID].iam.gserviceaccount.com" --role "roles/bigquery.dataEditor"
无论如何,我建议您使用官方的Python Client for Google BigQuery,您将获得更好的文档和功能。以及BigQuery official documentation中的大量代码示例。