Skip to content

Instantly share code, notes, and snippets.

@peihsinsu
Created June 5, 2016 11:33
Show Gist options
  • Save peihsinsu/751b9e168cd14177fe2e75235d933648 to your computer and use it in GitHub Desktop.
Save peihsinsu/751b9e168cd14177fe2e75235d933648 to your computer and use it in GitHub Desktop.
peihsinsu/bigquery module full example
var bq = require('bigquery')
, fs = require('fs')
, prjId = 'mitac-cp300'; //you need to modify this
bq.init({
json_file: '/Users/peihsinsu/.gcpkeys/mitac-cp300/mitac-cp300-e75b19c172ba.json'
});
var sql = {
aaa: 'SELECT count(*) FROM [cp300.books2] LIMIT 1000'
}
bq.job.query(prjId, sql.aaa, function(e,r,d){
if(e) console.log(e);
var result = format(d);
console.log(result);
});
function format(d) {
var schema = d.schema.fields;
var data = d.rows;
var out = [];
for(var i = 0 ; i < data.length ; i++) {
var v = data[i];
var vo = {};
for(var j=0; j < schema.length ; j++) {
vo[schema[j]['name']] = v.f[j]['v'];
}
out.push(vo);
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment