Created
June 5, 2016 11:33
-
-
Save peihsinsu/751b9e168cd14177fe2e75235d933648 to your computer and use it in GitHub Desktop.
peihsinsu/bigquery module full example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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