我們正在研究涉及大量數據的項目。現在我們最近閱讀了關於Google BigQuery的內容。但我們如何將數據導出到這個平臺?我們已經看到了將輸入日誌導入Google BigQuery的示例。但是這並不包含有關更新和刪除數據(僅插入)的信息。如何讓Firebase數據庫與BigQuery保持同步?
所以我們的對象能夠更新他們的數據。我們對BigQuery表的查詢數量有限。我們如何在不超過BigQuery配額限制的情況下同步我們的數據。
我們當前的功能代碼:
'use strict';
// Default imports.
const functions = require('firebase-functions');
const bigQuery = require('@google-cloud/bigquery')();
// If you want to change the nodes to listen to REMEMBER TO change the constants below.
// The 'id' field is AUTOMATICALLY added to the values, so you CANNOT add it.
const ROOT_NODE = 'categories';
const VALUES = [
'name'
];
// This function listens to the supplied root node.
// When the root node is completed empty all of the Google BigQuery rows will be removed.
// This function should only activate when the root node is deleted.
exports.root = functions.database.ref(ROOT_NODE).onWrite(event => {
if (event.data.exists()) {
return;
}
return bigQuery.query({
query: [
'DELETE FROM `stampwallet.' + ROOT_NODE + '`',
'WHERE true'
].join(' '),
params: []
});
});
// This function listens to the supplied root node, but on child added/removed/changed.
// When an object is inserted/deleted/updated the appropriate action will be taken.
exports.children = functions.database.ref(ROOT_NODE + '/{id}').onWrite(event => {
const id = event.params.id;
if (!event.data.exists()) {
return bigQuery.query({
query: [
'DELETE FROM `stampwallet.' + ROOT_NODE + '`',
'WHERE id = ?'
].join(' '),
params: [
id
]
});
}
const item = event.data.val();
if (event.data.previous.exists()) {
let update = [];
for (let index = 0; index < VALUES.length; index++) {
const value = VALUES[index];
update.push(item[value]);
}
update.push(id);
return bigQuery.query({
query: [
'UPDATE `stampwallet.' + ROOT_NODE + '`',
'SET ' + VALUES.join(' = ?, ') + ' = ?',
'WHERE id = ?'
].join(' '),
params: update
});
}
let template = [];
for (let index = 0; index < VALUES.length; index++) {
template.push('?');
}
let create = [];
create.push(id);
for (let index = 0; index < VALUES.length; index++) {
const value = VALUES[index];
create.push(item[value]);
}
return bigQuery.query({
query: [
'INSERT INTO `stampwallet.' + ROOT_NODE + '` (id, ' + VALUES.join(', ') + ')',
'VALUES (?, ' + template.join(', ') + ')'
].join(' '),
params: create
});
});
什麼是同步火力點至BigQuery的最佳方式?
@stanvanheumen - 如果我的回答對您有幫助,並且您接受了它,也請考慮投票。有關http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work#5235中的http://stackoverflow.com/help/someone-answers和Upvote部分的詳情,請參閱 –