【问题标题】:how to access bigquery data from firebase cloud functions?如何从 firebase 云功能访问 bigquery 数据?
【发布时间】: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 也已经尝试过了!
  • 您是否以管理权限运行它?
  • 是的,我运行这个函数的这个项目有管理员权限
  • 查看Related post

标签: firebase google-bigquery google-cloud-functions


【解决方案1】:

是的,可以从 Cloud Function 访问 bigquery 数据。您只需使用require() 导入它:

const functions = require('firebase-functions');
const bigquery = require('@google-cloud/bigquery');

编辑:如果您的云函数没有实时数据库触发器,您还需要添加 Admin SDK 才能写入数据库:

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

然后在您声明的云函数中运行查询:

var bigQuery = BigQuery({ projectId: 'YOUR-PROJECT-ID' });
    bigQuery.query({
        query: 'YOUR QUERY HERE'
    }).then(function (results) {
        var ref = admin.database().ref('queryresults');

        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
    });

【讨论】:

  • 如果我想将此查询结果存储到我的实时数据库中,那么???
  • 我尝试了下面的代码,但遇到了下面的错误!请帮助解决此错误
  • 我在 BigQuery 导入后删除了括号。请检查
  • 我也删除了,但同样的错误即将到来。 BigQuery 出现错误未在 exports.sendBigQueryData.functions.analytics.event.onLog.event 中定义
【解决方案2】:

Examples I've seen 要求像这样实例化 BigQuery:

改变这一行:

var bigQuery = BigQuery({ projectId:  });

到这里:

var bigQuery = new BigQuery({ projectId:  });

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 2020-08-19
    • 2019-04-16
    • 2019-06-05
    • 2019-09-20
    • 2018-06-24
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多