【发布时间】:2019-01-19 20:45:01
【问题描述】:
我正在使用 Google Cloud Function 对 bigQuery 执行查询,并在 firestore 时存储结果。
我的问题是,一旦我尝试使用 firestore 批处理对象,云功能就会停止执行。
使用二分法,我认为当我包含批处理对象代码时,函数突然停止工作。
我尝试将函数的内存增加到 1GB,但没有运气。 (目前使用的是 128mb)
const {BigQuery} = require('@google-cloud/bigquery');
const {Firestore} = require('@google-cloud/firestore');
const bigquery = new BigQuery ();
const firestore = new Firestore ();
const fsCollectionName = 'ul_queteur_stats_per_year';
const queryStr = "the bigquery query";
function handleError(err){
//skipped
}
/**
* Triggered from a message on a Cloud Pub/Sub topic.
*
* @param {!Object} event Event payload.
* @param {!Object} context Metadata for the event.
*/
exports.ULQueteurStatsPerYear = (event, context) => {
const pubsubMessage = event.data;
const parsedObject = JSON.parse(Buffer.from(pubsubMessage, 'base64').toString());
console.log("Recieved Message : "+JSON.stringify(parsedObject));
//{ ul_id:parsedObject.ul_id }
const queryObj = {
query: queryStr,
params: {
ul_id: parsedObject.ul_id
}
};
bigquery
.query(queryObj)
.then((data) => {
console.log("Query Successful, # rows : "+data.length+" data[0].length:"+data[0].length);
//rows : [{"amount":367.63,"weight":2399.3,"time_spent_in_minutes":420}]
const rows = data[0];
console.log("Query Successful");
const batch = firestore.batch();
console.log("Batch Created ");
console.log("Getting Collection");
const collection = firestore.collection(fsCollectionName);
console.log("Getting Collection '"+fsCollectionName+"' retrieved");
//#####################################
for(let i=0;i<rows.length;i++)
{
console.log("getting a new DocId");
const docRef = collection.doc();
console.log("Adding to docRef='"+docRef.id+"' : "+JSON.stringify(rows[i]));
batch.set(docRef, rows[i]);
console.log("Added to batch");
}
console.log("Commiting batch insert");
batch.commit().then(() => {
console.log('Successfully executed batch');
});
//#####################################
})
.catch(err => {
handleError(err);
});
};
预期:
在 Firestore 中插入的数据
实际结果:
如果我删除 //####################################
然后我在 stackdriver 中获取每个日志。 (第一个说有 420 行)
如果我让代码之间 //##################################### (或者只是 batch.commit() 部分,或者只是 for 循环部分)
我只得到第一个日志,然后什么也没有。
查询成功,# rows : 1 data[0].length:420
即使我将整个代码放在带有异常的 console.log 的 try/catch 块中,我在堆栈驱动程序中也看不到错误。
解决方案
解决方案是返回 bigquery 承诺。
所以上面的代码应该改成:
return bigquery
.query(queryObj)
.then(...);
感谢Doug 的帮助!
【问题讨论】:
-
尝试
return batch.commit(),然后在最外层添加另一个then
标签: google-bigquery google-cloud-firestore google-cloud-functions