【问题标题】:UNIX timestamp TO Google bigQuery TS FormatUNIX 时间戳转 Google bigQuery TS 格式
【发布时间】:2021-04-19 16:29:25
【问题描述】:

我正在使用 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 时间戳的消息,当函数运行时,我的查询给了我一个错误。我找不到任何解决方案,非常感谢任何帮助!提前致谢!

【问题讨论】:

    标签: google-bigquery google-cloud-pubsub


    【解决方案1】:

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

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

    【讨论】:

    • 完美解决方案!非常感谢@Daniel Zagales!请问怎么做逆行?我的意思是,稍后我将使用查询调用 API,如何将 BigQuery 时间戳再次转换为 UNIX 时间戳?非常感谢您的回答!
    • 你应该可以使用UNIX_SECONDS函数cloud.google.com/bigquery/docs/reference/standard-sql/…
    猜你喜欢
    • 2015-09-08
    • 2014-10-26
    • 1970-01-01
    • 2020-02-13
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多