Permalink
Browse files

fetch & execute implemented - cheers Andy Kent!

  • Loading branch information...
1 parent fb1f6ef commit 3408254e46b809d5936d5a25663275d08870bc24 @Jae Jae committed Jul 12, 2011
Showing with 61 additions and 43 deletions.
  1. +1 −0 README
  2. +14 −0 examples/test.js
  3. +44 −0 node-hive.js
  4. +2 −2 package.json
  5. +0 −41 test.js
View
1 README
@@ -0,0 +1 @@
+Thrift module has been cloned from https://github.com/wadey/node-thrift/commit/25c0eb4eb85aa63cfb49a8e8c815bd57e2b8043a
View
@@ -0,0 +1,14 @@
+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);
+});
+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)
+});
+hive.execute("DESCRIBE weather_data", function(err, data) {
+ console.log("DESCRIBE weather_data");
+ console.log(data);
+});
View
@@ -0,0 +1,44 @@
+var thrift = require('thrift'),
+ ttransport = require('thrift/transport'),
+ ThriftHive = require('gen-nodejs/ThriftHive');
+
+var futureConnection = function(config) {
+ return function(cb) {
+ var connection = thrift.createConnection(config.server, config.port || 10000, {transport: ttransport.TBufferedTransport, timeout: config.timeout || 1000});
+ var client = thrift.createClient(ThriftHive, connection);
+ connection.addListener("connect", function(){
+ cb(client, connection);
+ });
+ };
+};
+
+var hiveClient = function(futureConnection) {
+ return {
+ 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);
+ cb(null, data);
+ connection.end();
+ });
+ });
+ })
+ },
+
+ execute: function(query, cb){
+ futureConnection(function(client, connection) {
+ client.execute(query, function(err){
+ if (err) return cb(true, err);
+ cb(null, null);
+ connection.end();
+ });
+ });
+ },
+ };
+};
+
+exports.for = function(config) {
+ return hiveClient(futureConnection(config));
+};
View
@@ -1,9 +1,9 @@
{
"author": "Forward",
- "contributors":["Jae Lee", "Antonio Terreno"],
+ "contributors":["Jae Lee", "Antonio Terreno", "Andy Kent"],
"name": "node-hive",
"description": "Node Hive Client Library",
- "version": "0.0.1",
+ "version": "0.0.2",
"homepage": "https://github.com/forward/node-hive",
"repository": {
"type": "git",
View
41 test.js
@@ -1,41 +0,0 @@
-var thrift = require('thrift'),
- ttransport = require('thrift/transport'),
- ThriftHive = require('gen-nodejs/ThriftHive');
-
-var connection = thrift.createConnection("hive.hadoop.forward.co.uk", 10000, {transport: ttransport.TBufferedTransport, timeout: 1*1000});
-var client = thrift.createClient(ThriftHive, connection);
-var done = false;
-
-connection.on('error', function(err){ console.error(err); });
-
-var run_test = function(conn, client, query, wait, callback) {
- var func = function(){
- console.log("executing.");
- client.execute(query, function(err){
- console.log("executed.");
- if (err){ console.error("error on execute():", err); }
-
- client.fetchAll(function(err, data){
- console.log("fetched.");
- if (err){ console.error("error on fetchAll():", err); }
-
- console.log("result:", data);
- callback();
- });
- });
- };
- conn.addListener("connect", function(){
- console.log("connected.");
- setTimeout(func, wait);
- });
-};
-
-var query = "select count(1) from ask_hourly_clicks where country = 'mx' and dated = '2011-07-11'";
-run_test(connection, client, query, 5000, function(){ done = true; });
-
-setInterval(function(){
- if (done){
- connection.end();
- process.exit(0);
- }
-}, 1000);

0 comments on commit 3408254

Please sign in to comment.