Cloud function waits for a file to land in a bucket and loads it to BQ. The BQ table already exists. My datasetid is undefined even though I assign the dataset name in the top of the function. 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.
Also, to create a reference to an existing dataset use bigquery.dataset(datasetId)
and not bigquery.Dataset(datasetId)
.
More information about the BigQuery Node.js Client Library can be found here .
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.