我刚刚使用以下代码使用Python 客户端库创建了一个视图:
from google.cloud import bigquery
client = bigquery.Client()
project = 'bigquery-samples'
source_dataset_id = 'reddit'
source_table_id = 'full'
shared_dataset_ref = client.dataset('my_dataset')
view_ref = shared_dataset_ref.table('my_shared_view')
view = bigquery.Table(view_ref)
sql_template = (
'SELECT DISTINCT author,subreddit_id FROM `{}.{}.{}` WHERE subreddit_id LIKE "%t5%"')
view.view_query = sql_template.format(
project, source_dataset_id, source_table_id)
view = client.create_table(view) # API request
print('Successfully created view at {}'.format(view.full_table_id))
我的视图有 1359016 行,通过执行以下操作进行验证:
SELECT COUNT(*) FROM `my_dataset.my_shared_view`
希望能帮助到你。