UNIX 时间戳 Google bigQuery TS 格式

UNIX timestamp TO Google bigQuery TS Format

我正在使用 google Pub/sub 接收消息并触发云函数,该函数在 BigQuery 中查询消息的数据,问题是在我的消息中我收到了 UNIX时间戳,我需要将这个时间戳转换为bigquery格式,否则函数无法运行我的查询...

在这部分函数中:

exports.insertBigQuery = async (message, context) => {
  // Decode base64 the PubSub message
  let logData = Buffer.from(message.data, "base64").toString();
  // Convert it in JSON
  let logMessage = JSON.parse(logData);

  const query = createQuery(logMessage);

  const options = {
    query: query,
    location: "US",
  };

  const [job] = await bigquery.createQueryJob(options);
  console.log(`Job ${job.id} started.`);

  // Only wait the end of the job. Theere is no row as answer, it's only an insert
  await job.getQueryResults();
};

我访问了消息中的数据。

关于我在 bigquery 中查询的这部分函数:

function createQuery() {
  const queryString = `INSERT INTO \`mytable\`(myTS, userTS, registerTS) 
VALUES ( @myTS, @userTS, @registerTS);`;

我的问题是我收到带有 UNIX 时间戳的消息,而当函数 运行s 我的查询给我一个错误时。我找不到任何解决方案,非常感谢任何帮助!提前致谢!

处理此问题的一种方法是使用 TIMESTAMP_SECONDS 函数将您的值包装在插入

INSERT INTO \`mytable\`(myTS, userTS, registerTS) 
VALUES ( TIMESTAMP_SECONDS(@myTS), TIMESTAMP_SECONDS(@userTS), TIMESTAMP_SECONDS(@registerTS));