Google bigquery 从Pubsub在BigQuery中写入查询

Google bigquery 从Pubsub在BigQuery中写入查询,google-bigquery,google-cloud-functions,google-cloud-pubsub,Google Bigquery,Google Cloud Functions,Google Cloud Pubsub,我需要一些帮助 我收到的消息中包含PubSub主题中的数据,我需要插入从消息中获取的数据,并使用后台云函数(PUB/SUB)在BigQuery中进行查询 我要做的是: /** * Triggered from a message on a Cloud Pub/Sub topic. * * @param {!Object} event Event payload. * @param {!Object} context Metadata for the event. */ exports.

我需要一些帮助

我收到的消息中包含PubSub主题中的数据,我需要插入从消息中获取的数据,并使用后台云函数(PUB/SUB)在BigQuery中进行查询

我要做的是:

/**
 * Triggered from a message on a Cloud Pub/Sub topic.
 *
 * @param {!Object} event Event payload.
 * @param {!Object} context Metadata for the event.
 */
exports.insertBigQuery = (message, context) => {
    extractQuery(message.data);
   
};

function extractQuery(pubSubMessage){
    // Decide base64 the PubSub message
    let logData = Buffer.from(pubSubMessage, 'base64').toString();
    // Convert it in JSON
    let logMessage= JSON.parse(logData);

    console.log(logMessage.customerToken)
    console.log(logMessage.fbclid)
    console.log(logMessage.fbc)
    console.log(logMessage.fbp)
    console.log(logMessage.firstHitTS)
    console.log(logMessage.consentFB)

    main();
    
    return logMessage

    }

"use strict";

function main() {
  const { BigQuery } = require("@google-cloud/bigquery");
  const bigquery = new BigQuery();

  async function query() {
    const query = `INSERT INTO MYTABLE( customerToken, fbclid, fbc, fbp, firstHitTS, consentFB)
    VALUES ("customerTokenSCRIPTCLOUD","fbclidSCRIPT"," fbcSCRIPTCLOUD"," fbpSCRIPTCLOUD","2021-01-05",TRUE )`;

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

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

    const [rows] = await job.getQueryResults();

    console.log("Rows:");
    rows.forEach((row) => console.log(row));
  }

  query();
}
现在,每当我收到一条消息时,我都会在bigQuery中查询,但我的值是硬编码的,如您所见:

const query = `INSERT INTO devsensetestprojects.TestDataSet.fbSimpleData( customerToken, fbclid, fbc, fbp, firstHitTS, consentFB)
    VALUES ("customerTokenSCRIPTCLOUD","fbclidSCRIPT"," fbcSCRIPTCLOUD"," fbpSCRIPTCLOUD","2021-01-05",TRUE )`;

我不能做的是从
函数extractQuery(pubSubMessage)
中获取值,并在查询中使用它们,方法与在函数(logMessage.SOMEVALUE)中使用的方法相同,以获得所需的正确值


提前谢谢

正如您所说,您是开发新手。这里有一个更简洁高效的代码。我没有测试它,但它更接近你想要的。让我知道对你来说有些地方是神秘的


// Make them global to load them only when the Cloud Function instance is created
// They will be reused in the subsequent processing and until the instance deletion
const { BigQuery } = require("@google-cloud/bigquery");
const bigquery = new 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();

}

function createQuery(logMessage) {
    // You maybe have to format correctly the logMessage.firstHitTS to be accepted by BigQuery as a date.
    return `INSERT INTO MYTABLE(customerToken, fbclid, fbc, fbp, firstHitTS, consentFB)
                   VALUES (logMessage.customerToken, logMessage.fbclid, logMessage.fbc, logMessage.fbp,
                           logMessage.firstHitTS, logMessage.consentFB)`;
}

“错误:无法识别的名称:logMessage在[2:15]的新APIRROR(/workspace/node_modules/@google cloud/common/build/src/util.js:59:15)”我不知道为什么会发生这种情况,似乎logMessage仍然没有通过。谢谢你️问题似乎是在该部分
中插入MYTABLE(customerToken、fbclid、fbc、fbp、firstHitTS、ApproveFB)值(logMessage.customerToken、logMessage.fbclid、logMessage.fbc、logMessage.fbp、logMessage.firstHitTS、logMessage.ApproveFB)
每件事都是一个字符串,我需要使用变量,可能是语法问题亲爱的@guillaume blaquiere,你的函数太完美了!我只需要更改
函数createQuery(logMessage)
就可以了!非常感谢你!你改变了什么来修复它?NodeJS是我较弱的语言,我没有立即发现问题所在!我刚刚改变了在查询字符串
queryString=“INSERT INTO\”MYTABLE\(abc,def,ghi)值(@abc,@def,@ghi)中获取变量的方式最后我得到了类似的东西,现在我在我的手机里,明天我会评论洞块!Merci Maître Edit:代码本身没有问题,函数是完美的,这是sql语法。