VishalParashar
VishalParashar

Reputation: 51

how to access bigquery data from firebase cloud functions?

Is it posssible to access bigquery data from firebase cloud functions? or can we write a cloud function which can perform operation directly of bigquery data?

Update: This is MyCode

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 is not defined

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)

Upvotes: 4

Views: 6784

Answers (2)

Ben Cochrane
Ben Cochrane

Reputation: 3375

Examples I've seen require BigQuery to be instantiated like this:

Change this line:

var bigQuery = BigQuery({ projectId:  });

to this:

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

Upvotes: 2

Ros&#225;rio P. Fernandes
Ros&#225;rio P. Fernandes

Reputation: 11326

Yes, it is possible to acess bigquery data from a Cloud Function. You simply need to import it using require():

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

Edit: If your cloud function doesn't have a realtime database trigger, you'll need to add the Admin SDK as well in order to write to the database:

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

Then run the query inside your declared cloud function:

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

Upvotes: 8

Related Questions