如何通过适用于Google BigQuery的Python客户端库设置现有的表到期时间? [英] How to set existing table expiration via Python Client Library for Google BigQuery?
问题描述
使用官方的 Google BigQuery的Python客户端,不会出现一种在现有表上设置表expires
(或其他属性)的方法;您只能使用所需的属性,然后在其顶部加载数据.
API本身具有表/补丁命令,该命令允许设置expirationTime
属性.但是,我在客户端库代码中看不到对此有任何使用.
client.py
中的以下代码用于create_table
,我认为,只需将"POST"
更改为"PATCH"
并将table_id
添加到路径即可.>
是为什么客户端库不支持表修补或我遗漏了什么?
table = _table_arg_to_table(table, default_project=self.project)
path = "/projects/%s/datasets/%s/tables" % (table.project, table.dataset_id)
data = table.to_api_repr()
try:
api_response = self._call_api(retry, method="POST", path=path, data=data)
return Table.from_api_repr(api_response)
except google.api_core.exceptions.Conflict:
if not exists_ok:
raise
return self.get_table(table.reference, retry=retry)
client.Client
具有update_table
方法: github .
您可以使用表示形式.然后,用新的所需日期修改expires
属性,并用update_table
更新它(请注意,我们指定了要更新的字段列表):
table_info = client.get_table(table_name)
table_info.expires = datetime.now() + timedelta(days=1)
new_table_info = client.update_table(
table_info, ['expires'])
作为示例,我创建一个没有到期的空表:
$ bq mk -t test.expiration
Table 'PROJECT_ID:test.expiration' successfully created.
并运行脚本(库版本为google-cloud-bigquery==1.23.1
):
Initial expiration: None
Final expiration: 2019-12-22 08:47:52.507000+00:00
完整代码:
from datetime import datetime
from datetime import timedelta
from google.cloud import bigquery
project_id = "PROJECT_ID"
table_name = "test.expiration"
client = bigquery.Client(project=project_id)
# get the initial expiration date
table_info = client.get_table(table_name)
print("Initial expiration: {}".format(table_info.expires))
# update with the new desired field
table_info.expires = datetime.now() + timedelta(days=1)
new_table_info = client.update_table(
table_info, ['expires'])
# check results
print("Final expiration: {}".format(new_table_info.expires))
Using the official Python Client for Google BigQuery there doesn't appear to be a way to set the table expires
(or other properties) on an existing table; you can only create a table with the properties you want and then load data "on top" of it.
The API itself has a tables/patch command which does allow setting the expirationTime
propery. However, I don't see any use of this in the client library code.
The following code in client.py
is used for create_table
and I think that simply changing "POST"
to "PATCH"
and adding the table_id
to the path could work.
Is there a reason why table patching isn't supported in the client library or am I missing something?
table = _table_arg_to_table(table, default_project=self.project)
path = "/projects/%s/datasets/%s/tables" % (table.project, table.dataset_id)
data = table.to_api_repr()
try:
api_response = self._call_api(retry, method="POST", path=path, data=data)
return Table.from_api_repr(api_response)
except google.api_core.exceptions.Conflict:
if not exists_ok:
raise
return self.get_table(table.reference, retry=retry)
client.Client
has an update_table
method: api reference and github.
You can retrieve the table settings with get_table
to get the table.Table
representation. Then, you modify the expires
attribute with the new desired date and update it with update_table
(note that we specify the list of fields to update):
table_info = client.get_table(table_name)
table_info.expires = datetime.now() + timedelta(days=1)
new_table_info = client.update_table(
table_info, ['expires'])
As an example I create an empty table without expiration:
$ bq mk -t test.expiration
Table 'PROJECT_ID:test.expiration' successfully created.
and run the script (library version is google-cloud-bigquery==1.23.1
):
Initial expiration: None
Final expiration: 2019-12-22 08:47:52.507000+00:00
Full code:
from datetime import datetime
from datetime import timedelta
from google.cloud import bigquery
project_id = "PROJECT_ID"
table_name = "test.expiration"
client = bigquery.Client(project=project_id)
# get the initial expiration date
table_info = client.get_table(table_name)
print("Initial expiration: {}".format(table_info.expires))
# update with the new desired field
table_info.expires = datetime.now() + timedelta(days=1)
new_table_info = client.update_table(
table_info, ['expires'])
# check results
print("Final expiration: {}".format(new_table_info.expires))
这篇关于如何通过适用于Google BigQuery的Python客户端库设置现有的表到期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!