我有一个酒吧事件每4小时运行一次。我想在预定活动结束后保存所有用户的团队。我是批处理,但是批次写入的每提名限制为500。以下是类似于我要做的示例代码。
问题:如何自动缩放批次写入。
exports.updateNews = functions.pubsub
.topic("my-scheduled-topic")
.onPublish(message => {
return axios
.get(
"https://newsapi.org/v2/top-headlines?apiKey=someKey&sources=espn-cric-info"
)
.then(result => {
const batch = db.batch();
result.data.articles.forEach(article => {
const docRef = db.collection("news").doc();
batch.set(docRef, article);
});
return batch.commit();
})
.then(result => {
console.log(result);
return result;
})
.catch(error => {
console.log(error);
return error;
});
});
如果遇到批处理的500限制的限制,则可以使用Promise.all()
,如下所示。当add()
方法呼叫返回的所有承诺解决时,它将返回单个承诺。
exports.updateNews = functions.pubsub
.topic("my-scheduled-topic")
.onPublish(message => {
return axios
.get(
"https://newsapi.org/v2/top-headlines?apiKey=someKey&sources=espn-cric-info"
)
.then(result => {
const promises = [];
result.data.articles.forEach(article => {
promises.push(db.collection("news").add(article));
});
return Promise.all(promises);
})
.then(results => {
console.log(results);
return null;
})
.catch(error => {
console.log(error);
return null;
});
});
我认为我自己解决了它需要专家意见:
我从问题中知道我的代码看起来不像答案中的代码,我在提出问题之前简化了问题。
\ reference the collection I wanted to save to or the place I wanna write
var userTeamsSave = db.collection("saveTeams");
\ api call
db.collection("news")
.get()
.then(querySnapshot => {
\ create Json array from query snapshot
let users = [];
querySnapshot.forEach(user => {
users.push({ id: user.id, data: user.data() });
});
return users;
})
.then(users => {
var counter = 0;
var commitCounter = 0;
var batches = [];
\ array of batches
batches[commitCounter] = db.batch();
users.forEach(user => {
\ limit batch write in 1 commit upto 499
if (counter <= 200) {
var thisRef = userTeamsSave.doc(user.id);
batches[commitCounter].set(thisRef, user.data);
counter = counter + 1;
} else {
\\ Reset Counter
counter = 0;
thisRef = userTeamsSave.doc(user.id);
batches[commitCounter].set(thisRef, user.data);
commitCounter = commitCounter + 1;
batches[commitCounter] = db.batch();
}
});
\ return all batches
return batches;
})
.then(fullBatch => {
fullBatch.forEach(batch => {
console.count("wrote batch");
\ commit all batches
return batch.commit();
});
return;
})
.catch(error => {
console.error(error);
return error;
});