Skip to content
Browse files

support headers and ResultSet

  • Loading branch information...
1 parent 88506fc commit b67b022bd1ac5c0cb4f2ad64e98484937f3073b5 @andykent andykent committed
Showing with 63 additions and 8 deletions.
  1. +4 −3 examples/test.js
  2. +9 −5 node-hive.js
  3. +50 −0 result_set.js
View
7 examples/test.js
@@ -1,12 +1,13 @@
hive = require('../node-hive').for({server:"hive.hadoop.forward.co.uk"});
hive.fetch("SELECT * FROM weather_data where dated = '2011-07-01' limit 10", function(err, data) {
- console.log("SELECT * FROM weather_data where dated = '2011-07-01' limit 10");
- console.log(data);
+ data.each(function(record) {
+ console.log(record);
+ });
});
hive.fetch("SELECT * FROM weather_data where dated = '2011-07-02' limit 10", function(err, data) {
console.log("SELECT * FROM weather_data where dated = '2011-07-02' limit 10");
- console.log(data)
+ console.log(data.toTSV())
});
hive.execute("DESCRIBE weather_data", function(err, data) {
console.log("DESCRIBE weather_data");
View
14 node-hive.js
@@ -1,6 +1,7 @@
var thrift = require('thrift'),
ttransport = require('thrift/transport'),
- ThriftHive = require('gen-nodejs/ThriftHive');
+ ThriftHive = require('gen-nodejs/ThriftHive')
+ ResultSet = require('./result_set')
var futureConnection = function(config) {
return function(cb) {
@@ -15,11 +16,14 @@ var hiveClient = function(futureConnection) {
fetch: function(query, cb) {
futureConnection(function(client, connection) {
client.execute(query, function(err){
- if (err) return cb(true, err);
- client.fetchAll(function(err, data){
+ if(err) return cb(true, err);
+ client.getSchema(function(err, schema) {
if (err) return cb(true, err);
- cb(null, data);
- connection.end();
+ client.fetchAll(function(err, data){
+ if (err) return cb(true, err);
+ cb(null, ResultSet.create(data, schema));
+ connection.end();
+ });
});
});
})
View
50 result_set.js
@@ -0,0 +1,50 @@
+var ResultSet = function(rows, schema) {
+ this.rows = rows;
+ this.schema = schema.fieldSchemas;
+};
+
+ResultSet.prototype.each = function(cb) {
+ for(var i in this.rows) {
+ var rowArray = this.rows[i].split("\t");
+ var row = {};
+ var headers = this.headers();
+ for(var a in rowArray) {
+ row[headers[a]] = rowArray[a];
+ }
+ cb(row);
+ };
+};
+
+ResultSet.prototype.toArray = function() {
+ var ret = [];
+ for(var i in this.rows) {
+ ret.push(this.rows[i].split("\t"));
+ };
+ return ret;
+};
+
+ResultSet.prototype.headers = function() {
+ var colCount = this.rows[0].split("\t").length;
+ var ret = [];
+ var partitionCount = 1;
+ var i = 0;
+ while(i < colCount) {
+ schemaName = this.schema[i] ? this.schema[i].name : ("_p" + partitionCount++);
+ ret.push(schemaName);
+ i++;
+ }
+ this.headers = function() { return ret; };
+ return ret;
+};
+
+ResultSet.prototype.toTSV = function(headers) {
+ var body = this.rows.join("\n");
+ if(headers) {
+ body = this.headers().join("\t") + "\n" + body;
+ }
+ return body;
+};
+
+exports.create = function(rows, schema) {
+ return (new ResultSet(rows, schema))
+};

0 comments on commit b67b022

Please sign in to comment.
Something went wrong with that request. Please try again.