Instantly share code, notes, and snippets.

Embed
What would you like to do?
bigQuery.js
//require the google-cloud npm package
//setup the API keyfile, so your local environment can
//talk to the Google Cloud Platform
const gcloud = require('google-cloud')({
projectId: process.env.GCLOUD_PROJECT,
keyFilename: process.env.GCLOUD_KEY_FILE
});
//We will make use of the bigquery() API
const bq = gcloud.bigquery();
//Make use of a dataset called: dutchelectionstweets
const dataset = bq.dataset('dutchelectionstweets');
//Make use of a BigQuery table called: dutchelections
const table = dataset.table('dutchelections');
//If the dataset doesn't exist, let's create it.
dataset.exists(function(err, exists) {
if(!exists){
dataset.create({
id: 'dutchelectionstweets'
}).then(function(data) {
console.log("dataset created");
});
}
});
//If the table doesn't exist, let's create it.
//Note the schema that we will pass in.
table.exists(function(err, exists) {
if(!exists){
table.create({
id: 'dutchelections',
schema: 'TEXT, CREATED:TIMESTAMP, PARTY, COORDINATES, SCORE:FLOAT:, MAGNITUDE:FLOAT, HASHTAGS'
}).then(function(data) {
console.log("table created");
});
}
});
//Insert rows in BigQuery
var insertInBq = function(row){
table.insert(row, function(err, apiResponse){
if (!err) {
console.log("[BIGQUERY] - Saved.");
}
});
};
module.exports.insertInBq = insertInBq;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment