【问题标题】:Google Cloud Function Node.Js谷歌云函数 Node.Js
【发布时间】:2018-12-07 05:06:49
【问题描述】:

Cloud 函数等待文件进入存储桶并将其加载到 BQ。 BQ 表已经存在。即使我在函数顶部分配了数据集名称,我的 datasetid 也是未定义的。关于我的问题的任何想法?

Following Function 返回以下错误。 错误:函数崩溃。细节: 无法读取未定义的属性“表”

{
  "name": "sample-cloud-storage",
  "version": "0.0.1",
  "repository": "googleapis/nodejs-bigquery",
  "engines": {
    "node": ">=4"
  },
  "dependencies": {
    "@google-cloud/bigquery": "1.2.0",
    "@google-cloud/storage": "1.5.1",
    "yargs": "10.0.3"
  },
  "devDependencies": {
    "@google-cloud/nodejs-repo-tools": "2.1.3",
    "ava": "0.24.0",
    "nyc": "11.3.0",
    "proxyquire": "1.8.0",
    "sinon": "4.1.3",
    "uuid": "3.1.0"
  }
} 

exports.ToBigQuery_Stage = (event, callback) => {
  //https://stackoverflow.com/questions/49111829/using-cloud-function-to-load-data-into-big-query-table-it-is-appending-to-the-t

  const BigQuery = require('@google-cloud/bigquery');
  const Storage = require('@google-cloud/storage');

  const file = event.data;
  const context = event.context;
  const projectId = "bi-sales-sms";
  const datasetId = "bi-sales-sms:BI_Sales_SMS";
  const bucketName = file.bucket;
  const filename = file.name;

  //const dashOffset = filename.indexOf('-');

  //const tableId = filename + "_STAGE";
   const tableId = 'Roll_Up_Daily_Sales_Per_Retailer';

  console.log('hello norman');
  console.log('Load ' + filename + ' into ' + tableId);

const metadata = {
  allowJaggedRows: true,
  skipLeadingRows: 1

 };

  //test the print
  console.error(BigQuery.Dataset(datasetId));

  BigQuery
//issue is here  
    .Dataset(datasetId)
    .table(tableId)
    .Load(storage.bucket(bucketName).file(filename),metadata)
    .then(results => {
      job = results[0];
      // Wait for the job to finish
      return job;
     })
    .then(metadata => {
       // Check the job's status for errors
      const errors = metadata.status.errors;
      if (errors && errors.length > 0) {
        throw errors;
      }
    })
    .then(() => {
      console.log("Job " + job.id + " completed.");
    })
    .catch(err => {
      console.error('ERROR:', err);
    });


  callback();
};

【问题讨论】:

  • 嗨@N0RM1NAT0R,我提供了一个我相信可以解决您的问题的答案。它对你有用吗?

标签: node.js google-cloud-platform google-bigquery google-cloud-functions


【解决方案1】:

客户端创建丢失,将其添加到您的代码中进行设置

const bigquery = new BigQuery({
   projectId: projectId,
 });

现在,您应该使用 bigquery 客户端对象,而不是 BigQuery 对象。

此外,要创建对现有数据集的引用,请使用 bigquery.dataset(datasetId) 而不是 bigquery.Dataset(datasetId)

有关 BigQuery Node.js 客户端库的更多信息,请访问 here

【讨论】:

    猜你喜欢
    • 2019-02-15
    • 2018-10-02
    • 2021-01-22
    • 2021-06-07
    • 2018-09-26
    • 2020-03-10
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多