如何为通过Google Apps脚本创建的BigQuery中的新表设置过期时间?

时间:2020-07-17 10:40:50

标签: google-apps-script google-bigquery

我有一个脚本,可以创建来自Google Apps脚本项目的新Google BigQuery表。 当我使用默认到期时间运行它时,它运行良好。 但是现在我想将到期时间设置为2小时(7200000ms)

function createNewTable(tableId){  
  var table = {
    expirationTime: "7200000",
    tableReference: {
      projectId: projectId,
      datasetId: datasetId,
      tableId: tableId
    }    
  }
  table = BigQuery.Tables.insert(table, projectId, datasetId);
  console.info('Table created: %s', table.id);
}

它不起作用,因为错误API call to bigquery.tables.insert failed with error: Expiration time must be at least 1 seconds in the future。 我在哪里出错以及如何解决?

1 个答案:

答案 0 :(得分:2)

问题:

您应该提供自纪元以来的毫秒数,而不是当前时刻。来自BigQuery table resource docs

到期时间:可选。该表过期的时间,自该纪元以来的毫秒数。

解决方案:

获取自大纪元到当前时刻的毫秒数,并将其加上两个小时:

var expirationTime = 7200000 + new Date().getTime();

如有必要,在设置请求正文expirationTime: expirationTime.toString()时将其解析为字符串。

相关问题