Skip to content

Instantly share code, notes, and snippets.

@savelee
Created March 10, 2017 13:42
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save savelee/11490d86a942f0b0acb83b55009cac95 to your computer and use it in GitHub Desktop.
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