google-bigquery
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -22,3 +22,3 @@ var request = require('request'); | ||
if ( err || res.statusCode !== 200 ) { | ||
console.log(err || res); | ||
//console.log(err || res); | ||
cb('there was a problem executing your query'); | ||
@@ -25,0 +25,0 @@ } else { |
@@ -23,16 +23,63 @@ var Jobs = function ( options ) { | ||
}, | ||
load: function ( job, data, projId, cb) { | ||
load: function ( job, data, projId, cb ) { | ||
var body = [{ | ||
'content-type': 'application/json', | ||
'body': JSON.stringify(job) | ||
}, {//content-type will not work on this one. | ||
'Content-Type': 'application/octet-stream', | ||
'body': data | ||
}]; | ||
'content-type': 'application/json', | ||
'body': JSON.stringify(job) | ||
}, {//content-type will not work on this one. | ||
'Content-Type': 'application/octet-stream', | ||
'body': data | ||
}]; | ||
console.log('about to make the multipart'); | ||
multipart('/projects/' + projId + '/jobs', body, cb); | ||
}, | ||
query: function ( options, cb ) { | ||
var projId = options.projId, | ||
query = options.query, | ||
maxTimeout = options.maxTimeout || 1000, | ||
jobName = "tempjob" + new Date().getTime(), | ||
url = '/projects/' + projId + '/jobs', | ||
job = { | ||
id: jobName, | ||
jobReference: { | ||
projectId: projId, | ||
jobId: jobName | ||
}, | ||
configuration: { | ||
query: { | ||
"query": query | ||
} | ||
} | ||
}; | ||
post( url, job, function ( err, result ) { | ||
if ( err ) { | ||
cb(err); | ||
return; | ||
} | ||
url = '/projects/' + projId + '/queries/' + jobName + '?maxTimeout=' + maxTimeout; | ||
get( url, function ( err, results ) { | ||
if ( err ) { | ||
cb(err); | ||
return; | ||
} | ||
var list = []; | ||
for( var rowIndex in results.rows ) { | ||
var item = {}; | ||
for( var fieldIndex in results.schema.fields ) { | ||
item[results.schema.fields[fieldIndex].name] = results.rows[rowIndex].f[fieldIndex].v; | ||
} | ||
list.push(item); | ||
} | ||
cb(null, list); | ||
}); | ||
}); | ||
} | ||
@@ -39,0 +86,0 @@ }; |
@@ -21,2 +21,3 @@ var request = require('request'); | ||
if ( err || res.statusCode !== 200 ) { | ||
//console.log(err || res); | ||
cb('there was a problem executing your query'); | ||
@@ -23,0 +24,0 @@ } else { |
{ | ||
"name": "google-bigquery", | ||
"description": "node.js package for accessing google bigquery through a service account", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Gustavo Machado", |
@@ -41,3 +41,2 @@ Access Google BigQuery using a service account from node.js | ||
# TODO | ||
* Uploading of data into tables. | ||
* Better API for querying tables (right now you do it through jobs create and query methods) | ||
@@ -44,0 +43,0 @@ * Review the token expiration/refresh logic. |
@@ -349,3 +349,53 @@ var assert = require('assert'), | ||
it('can create query job', function ( done ) { | ||
/* | ||
projectId: 'tellagostudios.com:kidozen', | ||
datasetId: 'devaudit', | ||
tableId: 'bulkdata' | ||
*/ | ||
var jobName = "testJob" + new Date().getTime(); | ||
var job = { | ||
id: jobName, | ||
jobReference: { | ||
projectId: 'YOUR-PROJECT-ID', | ||
jobId: jobName | ||
}, | ||
configuration: { | ||
query: { | ||
"query": "SELECT tenantId, serviceName, STRFTIME_UTC_USEC(NOW(), '%H:%M') AS ts, count(*) as count FROM [devaudit.bulkdata] " + | ||
"WHERE startedOn * 1000 > UTC_USEC_TO_MONTH(1344970355790 * 1000) " + | ||
"GROUP BY tenantId, serviceName, ts " + | ||
"LIMIT 1000 " | ||
} | ||
} | ||
}; | ||
client.jobs.create(job, function (err, entity) { | ||
assert.equal(undefined, err); | ||
assert.ok(entity); | ||
console.log( err || entity ); | ||
console.log( JSON.stringify(entity)); | ||
done(); | ||
}); | ||
}); | ||
it('can execute query', function ( done ) { | ||
var projectId = 'YOUR-PROJECT-ID', | ||
//COMPLETE YOUR QUERY HERE. | ||
query = "SELECT col1 FROM [dataset1.table1] " + | ||
"LIMIT 1000 ", | ||
options = { | ||
projId: projectId, | ||
query: query | ||
}; | ||
client.jobs.query(options, function ( err, result ){ | ||
assert.equal( undefined, err ); | ||
assert.ok( result ); | ||
console.log( err || result ); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
31852
663
54