Skip to content

Instantly share code, notes, and snippets.

@Bryanmuloni
Last active June 14, 2020 09:11
Show Gist options
  • Save Bryanmuloni/9606be1bc66a270dd20314233ecc65b4 to your computer and use it in GitHub Desktop.
Save Bryanmuloni/9606be1bc66a270dd20314233ecc65b4 to your computer and use it in GitHub Desktop.
Importing Large Data from BigQuery to Firestore using CloudFunctions
const {BigQuery} = require("@google-cloud/bigquery");
const firebaseConfig = require('firebase-admin');
firebaseConfig.initializeApp();
const firestoreDb = firebaseConfig.firestore();
var batchCommits = [];
var batch = firestoreDb.batch();
var counter = 0;
exports.fetchAdminTwo = (req,res) =>{
var bigQuery = new BigQuery({ projectId: 'fao-empres-re' });
var sqlStatement = 'SELECT id, id_admin1, description_en, description_fr, description_es, latitude, longitude FROM `fao-empres-re.empres_data.admin2`;';
console.log('Triggering bigquery to execute sql statement');
return bigQuery.query({query:sqlStatement}).then(results =>{
var adminList = results[0];
console.log('Query results:',adminList);
res.send(adminList);
adminList.forEach((admin,i) =>{
var collectionRef = firestoreDb.collection('ref-admins-two').doc();
batch.set(collectionRef,admin);
console.log('Doc ID:',collectionRef.id,'Count:',counter++);
if ((i + 1) % 500 === 0) {
console.log(`Batch count: ${i + 1}`);
batchCommits.push(batch.commit());
batch = firestoreDb.batch();
}
});
batchCommits.push(batch.commit());
return Promise.all(batchCommits);
}).catch(error =>{
console.log("Query error:",error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment