我们正在尝试为我们的 Google BigQuery 用户设置权限。
此代码在 Jupyter 笔记本中运行时有效 - 但未在 App 引擎中运行。
from google.cloud import bigquery
client = bigquery.Client.from_service_account_json(credentials, project=project)
dataset = client.get_dataset(client.dataset(dataset_name))
代码给出了这个错误:
'Client' object has no attribute 'get_dataset'
我们假设这是因为 App Engine 可能正在使用没有该方法的旧版本 BigQuery - 但是当我们添加以下行时...
print "Version: ", bigquery.__version__
...,显示的版本是 0.30,据我们了解,它应该支持 get_dataset 方法。
我们缺少什么?是什么导致我们的“Client”对象没有属性“get_dataset”错误?
附录:我们从 dir(client) 中看到的方法是:
> ['SCOPE', '_SET_PROJECT', '__class__', '__delattr__', '__dict__',
> '__doc__', '__format__', '__getattribute__', '__getstate__',
> '__hash__', '__init__', '__module__', '__new__', '__reduce__',
> '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
> '__subclasshook__', '__weakref__', '_connection', '_credentials',
> '_determine_default', '_http', '_http_internal', 'copy_table',
> 'dataset', 'extract_table_to_storage', 'from_service_account_json',
> 'job_from_resource', 'list_datasets', 'list_jobs', 'list_projects',
> 'load_table_from_storage', 'project', 'run_async_query',
> 'run_sync_query']
请您参考如下方法:
库的行为就好像它是 google-cloud-bigquery<=0.27.0。您可以使用早期的库方法,例如:
v0.30.0:
from google.cloud import bigquery
client = bigquery.Client.from_service_account_json(credentials, project=project)
dataset = client.get_dataset(client.dataset(dataset_name))
entries = list(dataset.access_entries)
entry = bigquery.AccessEntry(
role=role,
entity_type=entity_type,
entity_id=entity_id)
entries.append(entry)
dataset.access_entries = entries
dataset = client.update_dataset(dataset, ['access_entries'])
v0.27.0:
from google.cloud import bigquery
client = bigquery.Client.from_service_account_json(credentials, project=project)
dataset = client.dataset(dataset_name)
dataset.reload()
entries = list(dataset.access_grants)
entry = bigquery.AccessGrant(
role=role,
entity_type=entity_type,
entity_id=entity_id)
entries.append(entry)
dataset.access_grants = entries
dataset.update()