简体   繁体   中英

UNIX timestamp TO Google bigQuery TS Format

I'm using google Pub/sub to receive a message and trigger a cloud func, that func queries the data of the message in BigQuery, the problem is that in my message I receive UNIX timestamp, and I need to convert this time stamp for bigquery format, otherwise the function can not run my query...

In this part of the function:

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();
};

I access the data in the message.

On this part of the function I query in my bigquery:

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

My problem is that I receive the message with UNIX time stamp and when the function runs my query gives me an error. I couldn't find any solution, any help is MUCH appreciated! Thanks in advance!

One way you cand handle this is using the TIMESTAMP_SECONDS function to wrap your values on the insert

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM