简体   繁体   中英

Python Bigquery create temp table

When I create temp table via python, an error throws 400 Use of CREATE TEMPORARY TABLE requires a script or session How can I create a session?

from google.colab import auth
from google.cloud import bigquery
from google.colab import data_table
client = bigquery.Client(project=project, location = location)


client.query('''
create temp table t_acquisted_users as
select *
from table_a
limit 10
''').result()

You can create a session using the BigQuery API using the create_session parameter in a job config, for example: job_config=bigquery.QueryJobConfig(create_session=True)

More details on this excellent article: https://dev.to/stack-labs/bigquery-transactions-over-multiple-queries-with-sessions-2ll5

That's how I fix it in quick. Awaiting others provide a better answer

# create session
client0 = bigquery.Client(project=project, location=location)
job = client0.query(
    "SELECT 1;",  # a query can't fail
    job_config=bigquery.QueryJobConfig(create_session=True)
)
session_id = job.session_info.session_id
job.result()

# set default session
client = bigquery.Client(project=project, location=location, 
    default_query_job_config=bigquery.QueryJobConfig(
    connection_properties=[
          bigquery.query.ConnectionProperty(
          key="session_id", value=session_id
      )
    ]
))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM