BigQuery无效数据集ID问题:无法修改ID时的解决办法
The error you're encountering stems from how you're passing the dataset ID to the BigQuery Python client. When you use 'mytest-0001:reports_test' as the dataset ID, the client automatically appends your default project ID (gcd-my-reporting) to it, creating an invalid dataset path like gcd-my-reporting:mytest-0001:reports_test—which violates BigQuery's dataset ID rules.
Since you can't modify the existing dataset ID, here's the straightforward fix for your code:
Modified Working Code
from google.cloud import bigquery client = bigquery.Client() # Split your combined dataset ID into separate project and dataset name components project_id = 'mytest-0001' dataset_name = 'reports_test' table_id = 'test_data' # Explicitly specify the project when creating the dataset reference dataset_ref = client.dataset(dataset_name, project=project_id) dataset = client.get_dataset(dataset_ref) table_ref = dataset_ref.table(table_id) table = client.get_table(table_ref) print('Dataset ID: {}:{}'.format(project_id, dataset_name)) print('Description: {}'.format(dataset.description)) print(table.schema) print(table.description) print(table.num_rows)
Why This Works
The client.dataset() method is designed to take the dataset name as its first parameter, with an optional project argument to specify which project hosts the dataset. By splitting your original colon-separated ID into distinct project and dataset values, you eliminate the client's automatic default project append. This ensures the client targets the correct dataset path: mytest-0001:reports_test.
内容的提问来源于stack exchange,提问作者Edwin Lai

