我正在努力从下面的云功能代码中获取BigQuery Job ID。我需要工作ID才能验证工作是否成功完成
这是我的代码:
var storage = new Storage({projectId: process.env.PROJECT_ID});
var tableId = getTableID(file);
const metadata = {
sourceFormat: 'CSV',
skipLeadingRows: 1,
autodetect: false,
writeDisposition: 'WRITE_APPEND',
fieldDelimiter: process.env.DELIMITER,
allowJaggedRows: 'TRUE'
};
console.log(`The file ${file.name} has been successfully picked up and going to be stored in table ${process.env.PROJECT_ID}:${process.env.DATASET_ID}.${tableId}.`);
outboundHandler.sendStatusUpdate(file.name, process.env.IN_PROGRESS_CODE);
var errorsState = false;
// Loads data from a Google Cloud Storage file into the table
const [job] = bigquery
.dataset(process.env.DATASET_ID)
.table(tableId)
.load(storage.bucket(file.bucket).file(file.name), metadata);
console.log(job.id);
我想避免使用承诺,因为我不想等到大查询工作将完成:
您可以 SET 在元数据对象中自己的jopid来监视工作状态并避免等待承诺响应。
来自BigQuery Load类文档
- @param {string} [Metadata.jobid] 基础作业的自定义ID。
- @param {string} [metadata.jobprefix]前缀适用于基础作业ID。
这是C#中的一个示例的链接
但是,根据您的描述,听起来您的过程中有一个错误。我建议您尝试在可能的情况下使用Async/Await
而不是promise
,以简化代码并帮助故障排除
// Imports the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = "your-project-id";
// const filename = "/path/to/file.csv";
// const datasetId = "my_dataset";
// const tableId = "my_table";
// Creates a client
const bigquery = new BigQuery({projectId});
// Loads data from a local file into the table
const [job] = await bigquery
.dataset(datasetId)
.table(tableId)
.load(filename);
console.log(`Job ${job.id} completed.`);
// Check the job's status for errors
const errors = job.status.errors;
if (errors && errors.length > 0) {
throw errors;
}
您需要进一步使用Promise
const [job] = bigquery
.dataset(process.env.DATASET_ID)
.table(tableId)
.load(storage.bucket(file.bucket).file(file.name), metadata)
.then(results => {
const job = results[0];
// load() waits for the job to finish
console.log(`Job ${job.id} completed.`);
// Check the job's status for errors
const errors = job.status.errors;
if (errors && errors.length > 0) {
//console.log(errors);
callback(errors);
//throw errors;
} else {
callback();
}
})
.catch(err => {
//console.error('ERROR:', err);
callback(err);
});
确保您在所有可能的完成时都致电callback
。
我们的功能定义为exports.loadFileFromGCStoBigQuery = (data, callback) => {