【发布时间】:2018-07-15 10:44:50
【问题描述】:
是否可以从 firebase 云功能访问 bigquery 数据? 或者我们可以写一个可以直接对大查询数据进行操作的云函数?
更新: 这是我的代码
const functions = require('firebase-functions');
const bigquery = require('@google-cloud/bigquery');
const admin = require('firebase-admin');
var serviceAccount = require("");
admin.initializeApp(functions.config().firebase);
exports.sendBigQueryData = functions.analytics.event('app_open').onLog(event => {
var bigQuery = BigQuery({ projectId: });
bigQuery.query({
query: 'SELECT CAST(TIMESTAMP_ADD(TIMESTAMP_MICROS(event.timestamp_micros), INTERVAL 330 MINUTE) AS date) AS event_date, count(event.name) AS number_questions FROM ``, UNNEST(event_dim) AS event WHERE event.name="asked_question" GROUP BY event_date ORDER BY event_date DESC'
}).then(function (results) {
var ref = admin.database().ref('');
var rows = results[0]; //get all fetched table rows
rows.forEach(function(row){ //iterate through each row
ref.push().set({ //write to the database, under the 'queryresults' node
column1:row['col1'],
column2:row['col2']
});
});
//return result
}).catch(function (error) {
//return an error
})
});
ReferenceError:BigQuery 未定义
at exports.sendBigQueryData.functions.analytics.event.onLog.event (/user_code/index.js:11:16)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
at /var/tmp/worker/worker.js:695:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
【问题讨论】:
-
你应该在你的项目目录上运行
npm install --save @google-cloud/bigquery -
npm install --save @google-cloud/bigquery 也已经尝试过了!
-
您是否以管理权限运行它?
-
是的,我运行这个函数的这个项目有管理员权限
标签: firebase google-bigquery google-cloud-functions