google 云 BigQuery REST API,table 创建时带有过期时间
google cloud BigQuery REST API, table creation with expirationTime
我在我的 appengine 项目中使用 BigQuery API 创建一个 table,然后使用 insertAll 流输入方法加载数据。 [参考:https://cloud.google.com/bigquery/docs/reference/v2/tables/insert]
示例 python 创建代码 table:
scope = "https://www.googleapis.com/auth/bigquery"
authorization_token, _ = app_identity.get_access_token(scope)
Bodyfields = {
"kind": "bigquery#table",
"tableReference": {
"projectId": BIGQUERY_PROJECTID,
"datasetId": BIGQUERY_DATASETID,
"tableId": BIGQUERY_TABLEID
},
"friendlyName": 'Table 1',
"description": 'My first table in big query',
"schema": {
"fields": [
{
"name": 'A',
"type": 'STRING'
},
{
"name": 'B',
"type": 'STRING'
}
]
}
}
result = urlfetch.fetch(url="https://www.googleapis.com/bigquery/v2/projects/" + BIGQUERY_PROJECTID + "/datasets/" + BIGQUERY_DATASETID + "/tables", method=urlfetch.POST, payload=json.dumps(Bodyfields), headers = {'Content-Type': 'application/json', "Authorization": "Bearer " + authorization_token})
logging.info(result.content)
这按预期工作,但不幸的是 table 的 'expirationTime' 设置为 1 天。
根据文档,当 requestBody 中未提供 expirationTime 时,table 将无限期保留。 [参考:https://cloud.google.com/bigquery/docs/reference/v2/tables#expirationTime]。如图所示,我的 table 将在 1 天后到期。
所以我修改了代码,在inserttable方法的requestBody中包含了'expirationTime'。
代码示例:
ist_one_year_later = datetime.now() + timedelta(weeks=52)
ist_one_year_later_epoch_timestamp = long(float((ist_one_year_later - datetime(1970,1,1)).total_seconds()))
scope = "https://www.googleapis.com/auth/bigquery"
authorization_token, _ = app_identity.get_access_token(scope)
Bodyfields = {
"kind": "bigquery#table",
"tableReference": {
"projectId": BIGQUERY_PROJECTID,
"datasetId": BIGQUERY_DATASETID,
"tableId": BIGQUERY_TABLEID
},
"friendlyName": 'Table 1',
"description": 'My first table in big query',,
"expirationTime": ist_one_year_later_epoch_timestamp,
"schema": {
"fields": [
{
"name": 'A',
"type": 'STRING'
},
{
"name": 'B',
"type": 'STRING'
}
]
}
}
result = urlfetch.fetch(url="https://www.googleapis.com/bigquery/v2/projects/" + BIGQUERY_PROJECTID + "/datasets/" + BIGQUERY_DATASETID + "/tables", method=urlfetch.POST, payload=json.dumps(Bodyfields), headers = {'Content-Type': 'application/json', "Authorization": "Bearer " + authorization_token})
logging.info(result.content)
现在的结局更加离奇了。此请求的响应表示 table 已创建。但是当我尝试在数据集中列出 table 时,table 不可用。
有人能告诉我如何将 table 的过期时间设置为距创建时间 1 年吗?
编辑:我已经尝试了补丁和更新方法。两者都没有给出任何错误,但也没有延长过期时间。
根据对您的问题的评论,table创建的没有过期时间的文件采用您数据集中一天的默认过期时间。
您创建具有明确到期时间的 table 的代码不正确。每 https://cloud.google.com/bigquery/docs/reference/v2/tables#expirationTime,过期时间以毫秒表示,但您提供了自纪元以来的秒数。因此,table 已成功创建且过期时间为过去,并且当您在数据集中列出 table 时不存在。
我在我的 appengine 项目中使用 BigQuery API 创建一个 table,然后使用 insertAll 流输入方法加载数据。 [参考:https://cloud.google.com/bigquery/docs/reference/v2/tables/insert]
示例 python 创建代码 table:
scope = "https://www.googleapis.com/auth/bigquery"
authorization_token, _ = app_identity.get_access_token(scope)
Bodyfields = {
"kind": "bigquery#table",
"tableReference": {
"projectId": BIGQUERY_PROJECTID,
"datasetId": BIGQUERY_DATASETID,
"tableId": BIGQUERY_TABLEID
},
"friendlyName": 'Table 1',
"description": 'My first table in big query',
"schema": {
"fields": [
{
"name": 'A',
"type": 'STRING'
},
{
"name": 'B',
"type": 'STRING'
}
]
}
}
result = urlfetch.fetch(url="https://www.googleapis.com/bigquery/v2/projects/" + BIGQUERY_PROJECTID + "/datasets/" + BIGQUERY_DATASETID + "/tables", method=urlfetch.POST, payload=json.dumps(Bodyfields), headers = {'Content-Type': 'application/json', "Authorization": "Bearer " + authorization_token})
logging.info(result.content)
这按预期工作,但不幸的是 table 的 'expirationTime' 设置为 1 天。
根据文档,当 requestBody 中未提供 expirationTime 时,table 将无限期保留。 [参考:https://cloud.google.com/bigquery/docs/reference/v2/tables#expirationTime]。如图所示,我的 table 将在 1 天后到期。
所以我修改了代码,在inserttable方法的requestBody中包含了'expirationTime'。
代码示例:
ist_one_year_later = datetime.now() + timedelta(weeks=52)
ist_one_year_later_epoch_timestamp = long(float((ist_one_year_later - datetime(1970,1,1)).total_seconds()))
scope = "https://www.googleapis.com/auth/bigquery"
authorization_token, _ = app_identity.get_access_token(scope)
Bodyfields = {
"kind": "bigquery#table",
"tableReference": {
"projectId": BIGQUERY_PROJECTID,
"datasetId": BIGQUERY_DATASETID,
"tableId": BIGQUERY_TABLEID
},
"friendlyName": 'Table 1',
"description": 'My first table in big query',,
"expirationTime": ist_one_year_later_epoch_timestamp,
"schema": {
"fields": [
{
"name": 'A',
"type": 'STRING'
},
{
"name": 'B',
"type": 'STRING'
}
]
}
}
result = urlfetch.fetch(url="https://www.googleapis.com/bigquery/v2/projects/" + BIGQUERY_PROJECTID + "/datasets/" + BIGQUERY_DATASETID + "/tables", method=urlfetch.POST, payload=json.dumps(Bodyfields), headers = {'Content-Type': 'application/json', "Authorization": "Bearer " + authorization_token})
logging.info(result.content)
现在的结局更加离奇了。此请求的响应表示 table 已创建。但是当我尝试在数据集中列出 table 时,table 不可用。
有人能告诉我如何将 table 的过期时间设置为距创建时间 1 年吗?
编辑:我已经尝试了补丁和更新方法。两者都没有给出任何错误,但也没有延长过期时间。
根据对您的问题的评论,table创建的没有过期时间的文件采用您数据集中一天的默认过期时间。
您创建具有明确到期时间的 table 的代码不正确。每 https://cloud.google.com/bigquery/docs/reference/v2/tables#expirationTime,过期时间以毫秒表示,但您提供了自纪元以来的秒数。因此,table 已成功创建且过期时间为过去,并且当您在数据集中列出 table 时不存在。