简体   繁体   English

谷歌云函数 Node.Js

[英]Google Cloud Function Node.Js

Cloud function waits for a file to land in a bucket and loads it to BQ. Cloud 函数等待文件进入存储桶并将其加载到 BQ。 The BQ table already exists. BQ 表已经存在。 My datasetid is undefined even though I assign the dataset name in the top of the function.即使我在函数顶部分配了数据集名称,我的 datasetid 也未定义。 Any ideas on what my issue could be?关于我的问题可能是什么的任何想法?

Following Function returns the following error.以下函数返回以下错误。 Error: function crashed.错误:函数崩溃。 Details: Cannot read property 'table' of undefined详细信息:无法读取未定义的属性“表”

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

The client creation is missing, add this to your code to set it缺少客户端创建,将此添加到您的代码中以进行设置

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

Now instead of the BigQuery object you should use the bigquery client object.现在,您应该使用bigquery客户端对象代替BigQuery对象。

Also, to create a reference to an existing dataset use bigquery.dataset(datasetId) and not bigquery.Dataset(datasetId) .此外,要创建对现有数据集的引用,请使用bigquery.dataset(datasetId)而不是bigquery.Dataset(datasetId)

More information about the BigQuery Node.js Client Library can be found here .可以在 此处找到有关 BigQuery Node.js 客户端库的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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