Python 2.7 如何设置表过期时间大查询
需要帮助设置GBQ中新表的过期时间 我正在使用以下代码在gbq中创建/上载一个新文件作为表Python 2.7 如何设置表过期时间大查询,python-2.7,api,google-bigquery,Python 2.7,Api,Google Bigquery,需要帮助设置GBQ中新表的过期时间 我正在使用以下代码在gbq中创建/上载一个新文件作为表 def uploadCsvToGbq(self, table_name, jsonSchema, csvFile, delim): job_data = { 'jobReference': { 'projectId': self.project_id, 'job_id': str(uuid.uuid4()) },
def uploadCsvToGbq(self, table_name, jsonSchema, csvFile, delim):
job_data = {
'jobReference': {
'projectId': self.project_id,
'job_id': str(uuid.uuid4())
},
#"expires":str(datetime.now()+timedelta(seconds=60)),
#"expirationTime": 20000,
#"defaultTableExpirationMs":20000,
'configuration': {
'load': {'writeDisposition': 'WRITE_TRUNCATE',
'fieldDelimiter': delim,
'skipLeadingRows': 1,
'sourceFormat': 'CSV',
'schema': {
'fields': jsonSchema
},
'destinationTable': {
'projectId': self.project_id,
'datasetId': self.dataset_id,
'tableId': table_name
}
}
}
}
upload = MediaFileUpload(csvFile,
mimetype='application/octet-stream', chunksize=1048576,
# This enables resumable uploads.
resumable=True)
start = time.time()
job_id = 'job_%d' % start
# Create the job.
return self.bigquery.jobs().insert(projectId=self.project_id,
body=job_data,
media_body=upload).execute()
这是一个完美的代码,可以将该文件作为一个新表上传到GBQ中,现在我需要设置该表的过期时间,我已经尝试设置了expires、expirationTime和defaultTableExpirationMs,但是没有任何效果
有人知道吗?您应该使用并设置属性您应该使用并设置属性下面的函数创建一个带有expirationTime的表,作为替代解决方案,您可以先创建表,然后插入数据
def createTableWithExpire(bigquery, dataset_id, table_id, expiration_time):
"""
Creates a BQ table that will be expired in specified time.
Expiration time can be in Unix timestamp format e.g. 1452627594
"""
table_data = {
"expirationTime": expiration_time,
"tableReference":
{
"tableId": table_id
}
}
return bigquery.tables().insert(
projectId=_PROJECT_ID,
datasetId=dataset_id,
body=table_data).execute()
米哈伊尔也回答了这个问题 下面的函数创建一个带有expirationTime的表,作为替代解决方案,您可以先创建表,然后插入数据
def createTableWithExpire(bigquery, dataset_id, table_id, expiration_time):
"""
Creates a BQ table that will be expired in specified time.
Expiration time can be in Unix timestamp format e.g. 1452627594
"""
table_data = {
"expirationTime": expiration_time,
"tableReference":
{
"tableId": table_id
}
}
return bigquery.tables().insert(
projectId=_PROJECT_ID,
datasetId=dataset_id,
body=table_data).execute()
米哈伊尔也回答了这个问题 谢谢你们,我结合了这两种解决方案,但对我的解决方案做了一些修改。 当我通过上传csv创建表时,我通过调用patch方法并将tableid传递给该方法来设置expirationTime
def createTableWithExpire(bigquery, dataset_id, table_id, expiration_time):
"""
Creates a BQ table that will be expired in specified time.
Expiration time can be in Unix timestamp format e.g. 1452627594
"""
table_data = {
"expirationTime": expiration_time,
}
return bigquery.tables().patch(
projectId=_PROJECT_ID,
datasetId=dataset_id,
tableId=table_id,
body=table_data).execute()
谢谢你们,我结合了这两种解决方案,但做了一些修改,为我的工作。 当我通过上传csv创建表时,我通过调用patch方法并将tableid传递给该方法来设置expirationTime
def createTableWithExpire(bigquery, dataset_id, table_id, expiration_time):
"""
Creates a BQ table that will be expired in specified time.
Expiration time can be in Unix timestamp format e.g. 1452627594
"""
table_data = {
"expirationTime": expiration_time,
}
return bigquery.tables().patch(
projectId=_PROJECT_ID,
datasetId=dataset_id,
tableId=table_id,
body=table_data).execute()
另一种选择是在创建表后设置过期时间:
from google.cloud import bigquery
import datetime
client = bigquery.Client()
table_ref = client.dataset('my-dataset').table('my-table') # get table ref
table = client.get_table(table_ref) # get Table object
# set datetime of expiration, must be a datetime type
table.expires = datetime.datetime.combine(datetime.date.today() +
datetime.timedelta(days=2),
datetime.time() )
table = client.update_table(table, ['expires']) # update table
另一种选择是在创建表后设置过期时间:
from google.cloud import bigquery
import datetime
client = bigquery.Client()
table_ref = client.dataset('my-dataset').table('my-table') # get table ref
table = client.get_table(table_ref) # get Table object
# set datetime of expiration, must be a datetime type
table.expires = datetime.datetime.combine(datetime.date.today() +
datetime.timedelta(days=2),
datetime.time() )
table = client.update_table(table, ['expires']) # update table
你能理解我们的答案真是太好了!同时,即使你还没有足够的声誉去投票——你至少可以接受下面的一个答案——你也可以在投票下面贴出的答案左边打勾来标记接受的答案。看看为什么它很重要!你能理解我们的答案真是太好了!同时,即使你还没有足够的声誉去投票——你至少可以接受下面的一个答案——你也可以在投票下面贴出的答案左边打勾来标记接受的答案。看看为什么它很重要!