Google bigquery 如何为大查询中现有表列中的每个类别创建多个表?
我现有的表列中有category列,我需要为每个唯一的类别创建一个单独的表。如何在大查询中实现这一点?您可以使用python SDK来实现。以下是您可以使用的示例:Google bigquery 如何为大查询中现有表列中的每个类别创建多个表?,google-bigquery,Google Bigquery,我现有的表列中有category列,我需要为每个唯一的类别创建一个单独的表。如何在大查询中实现这一点?您可以使用python SDK来实现。以下是您可以使用的示例: from google.cloud import bigquery #Initiate the client client = bigquery.Client() #Load the dataset in which you are going to store the table dataset_ref = client.da
from google.cloud import bigquery
#Initiate the client
client = bigquery.Client()
#Load the dataset in which you are going to store the table
dataset_ref = client.dataset('{dataset}')
#Create the Query which is going to get the values
QUERY = (
'SELECT DISTINCT {column} FROM `{project-ID}.{dataset}.{table}`'
)
# API request
query_job = client.query(QUERY)
# Wait for the Query to finish and store the query.
rows = query_job.result()
# Define the schema of the new table
schema = [
bigquery.SchemaField('ExampleField', 'STRING', mode='REQUIRED')
]
#The query is returned as a tuple per row
for value in rows:
table_ref = dataset_ref.table(value[0])
table = bigquery.Table(table_ref, schema=schema)
table = client.create_table(table)
assert table.table_id == value[0]
请记住,表名只能包含字母(大写或小写)、数字和下划线。因此,如果尝试使用无效值,可能会出现异常