GCP BigQuery如何通过python API将到期日期设置为表格 [英] GCP BigQuery how to set expiration date to table by python api
问题描述
我正在使用BigQuery Python API创建表,并希望为该表设置到期日期,因此该表将在某些天后自动删除.
I am using BigQuery Python API to create table, and would like to set an expiration date to the table, so the table would be automatically dropped after certain days.
这是我的代码:
client = bq.Client()
job_config = bq.QueryJobConfig()
dataset_id = dataset
table_ref = client.dataset(dataset_id).table(filename)
job_config.destination = table_ref
job_config.write_disposition = 'WRITE_TRUNCATE'
dt = datetime.now() + timedelta(seconds=259200)
unixtime = (dt - datetime(1970,1,1)).total_seconds()
expiration_time = unixtime
job_config.expires = expiration_time
query_job = client.query(query, job_config=job_config)
query_job.result()
问题是到期参数似乎不起作用.当我在UI中检查表格详细信息时,到期日期仍然是从不".
The problem is that the expiration parameter doesn't seem to work. When I am checking the table detail in the UI, the expiration date is still Never.
推荐答案
查看文档用于query
方法,我们可以看到无法在查询作业配置中设置过期时间.
Looking at the docs for the query
method we can see that it's not possible to set an expiration time in the query job config.
这样做的正确方法是在Table资源上进行设置,例如:
The proper way of doing so is setting at the Table resource, something like:
client = bq.Client()
job_config = bq.QueryJobConfig()
dataset_id = dataset
table_ref = client.dataset(dataset_id).table(filename)
table = bq.Table(table_ref)
dt = datetime.now() + timedelta(seconds=259200)
table.expires = dt
client.create_table(table)
query_job = client.query(query, job_config=job_config)
query_job.result()
这篇关于GCP BigQuery如何通过python API将到期日期设置为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!