Чтобы создать временную таблицу в BigQuery с помощью Python, вы можете использовать клиентскую библиотеку BigQuery Python. Вот несколько методов с примерами кода:
Метод 1. Использование библиотеки google-cloud-bigquery
from google.cloud import bigquery
# Instantiate the BigQuery client
client = bigquery.Client()
# Define the SQL query to create a temporary table
sql_query = """
CREATE TEMPORARY TABLE my_temp_table
AS
SELECT *
FROM `project.dataset.source_table`
"""
# Execute the query
query_job = client.query(sql_query)
query_job.result() # Wait for the query to complete
# Verify that the temporary table was created
table_ref = client.dataset('dataset').table('my_temp_table')
table = client.get_table(table_ref)
print(f"Temporary table '{table.table_id}' created successfully!")
Метод 2. Использование расширения google.cloud.bigquery.magics(Jupyter Notebook)
%load_ext google.cloud.bigquery
# Define the SQL query to create a temporary table
sql_query = """
CREATE TEMPORARY TABLE my_temp_table
AS
SELECT *
FROM `project.dataset.source_table`
"""
# Execute the query
%%bigquery --project project_name
{sql_query}
# Verify that the temporary table was created
table_ref = client.dataset('dataset').table('my_temp_table')
table = client.get_table(table_ref)
print(f"Temporary table '{table.table_id}' created successfully!")
Метод 3. Использование библиотеки pandas
import pandas as pd
from google.cloud import bigquery
# Instantiate the BigQuery client
client = bigquery.Client()
# Fetch data from the source table into a pandas DataFrame
sql_query = """
SELECT *
FROM `project.dataset.source_table`
"""
df = client.query(sql_query).to_dataframe()
# Create a temporary table from the DataFrame
temp_table_name = 'my_temp_table'
df.to_gbq(destination=temp_table_name, project_id='project_id', if_exists='fail', temporary=True)
# Verify that the temporary table was created
table_ref = client.dataset('dataset').table(temp_table_name)
table = client.get_table(table_ref)
print(f"Temporary table '{table.table_id}' created successfully!")