Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add select samples; createClient now return a sugar object

  • Loading branch information...
commit 89d59e88323282435337743dc70b3fb6b7cd5604 1 parent 278ba96
@wdavidw authored
View
31 README.md
@@ -4,12 +4,22 @@ The project export the Hive API throught Thrift. Multiple versions of hive are
supported.
The only function added to the generated Thrift code is `hive.createClient`. It
-take an `options` object as its argument and return the result of
-`thrift.createClient`. This client object is enriched with a `connection`
-property to expose the object returned by `thrift.createConnection` as well as
-with the `end` function as a shortcut to `connection.end`.
+take an `options` object as its argument and return an object with the following
+properties:
-## Hive connection: suggar example
+- `client`
+ A reference to the hive client returned by `thrift.createClient`
+- `connection`
+ A reference to the hive connection returned by `thrift.createConnection`
+- `end([callback])`
+ Close the Thrift connection
+- `execute(query, [callback])`
+ Execute a query
+- `query(query, [size], [callback])`
+ Execute a query and return its results as an array of arrays (rows and
+ columns)
+
+## Hive connection: sugar example
```javascript
var hive = require('thrift-hive');
@@ -20,14 +30,11 @@ with the `end` function as a shortcut to `connection.end`.
port: 10000
timeout: 1000
});
- // Execute with fetchAll
- client.execute('show databases', function(err){
+ // Execute query
+ client.query('show databases', function(err, databases){
assert.ifError(err);
- client.fetchAll(function(err, databases){
- assert.ifError(err);
- console.log(databases);
- client.end();
- });
+ console.log(databases);
+ client.end();
});
```
## Hive connection: raw example
View
27 lib/hive.coffee
@@ -2,9 +2,6 @@
thrift = require 'thrift'
transport = require 'thrift/lib/thrift/transport'
-#module.exports = class Client
- #
-
module.exports.createClient = (options = {}) ->
options.version ?= '0.7.1-cdh3u2'
options.server ?= '127.0.0.1'
@@ -13,7 +10,25 @@ module.exports.createClient = (options = {}) ->
options.transport ?= transport.TBufferedTransport
connection = thrift.createConnection options.server, options.port, options
client = thrift.createClient require("./#{options.version}/ThriftHive"), connection
- client.connection = connection
- client.end = connection.end
- client
+ # Returned object
+ connection: connection
+ client: client
+ end: connection.end.bind connection
+ execute: (query, callback) ->
+ client.execute query, (err) ->
+ callback err if callback
+ query: (query, size, callback) ->
+ if arguments.length is 2 and typeof callback is 'function'
+ callback = size
+ size = -1
+ client.execute query, (err) ->
+ callback err if err and callback
+ if size is -1
+ client.fetchAll (err, data) ->
+ data = data.map (row) -> row.split '\t'
+ callback err, data if callback
+ else
+ client.fetchN size, (err, data) ->
+ data = data.map (row) -> row.split '\t'
+ callback err, data if callback
View
4 samples/connect_raw/sample.js → samples/connect_raw.js
@@ -4,7 +4,7 @@
var assert = require('assert');
var thrift = require('thrift');
var transport = require('thrift/lib/thrift/transport');
- var ThriftHive = require('../../lib/0.7.1-cdh3u2/ThriftHive');
+ var ThriftHive = require('../lib/0.7.1-cdh3u2/ThriftHive');
// Client connection
var options = {transport: transport.TBufferedTransport, timeout: 1000};
var connection = thrift.createConnection('127.0.0.1', 10000, options);
@@ -17,4 +17,4 @@
console.log(databases);
connection.end();
});
- });
+ });
View
17 samples/connect_sugar.js
@@ -0,0 +1,17 @@
+#!/usr/bin/env node
+
+ var assert = require('assert');
+ var hive = require('..');
+ // Client connection
+ var client = hive.createClient({
+ version: '0.7.1-cdh3u2',
+ server: '127.0.0.1',
+ port: 10000,
+ timeout: 1000
+ });
+ // Execute query
+ client.query('show databases', function(err, databases){
+ assert.ifError(err);
+ console.log(databases);
+ client.end();
+ });
View
20 samples/connect_sugar/sample.js
@@ -1,20 +0,0 @@
-#!/usr/bin/env node
-
- var assert = require('assert');
- var hive = require('../../');
- // Client connection
- var client = hive.createClient({
- version: '0.7.1-cdh3u2',
- server: '127.0.0.1',
- port: 10000,
- timeout: 1000
- });
- // Execute with fetchAll
- client.execute('show databases', function(err){
- assert.ifError(err);
- client.fetchAll(function(err, databases){
- assert.ifError(err);
- console.log(databases);
- client.end();
- });
- });
View
16 samples/select_all.coffee
@@ -0,0 +1,16 @@
+#!/usr/bin/env coffee
+
+ assert = require 'assert'
+ hive = require '../../'
+
+ client = hive.createClient
+ version: '0.7.1-cdh3u2'
+ server: '127.0.0.1'
+ port: 10000
+ timeout: 1000
+
+ client.execute 'use my_db', (err) ->
+ client.query 'select * from my_table limit 10', (err, results) ->
+ assert.ifError(err)
+ console.log(results)
+ client.end()
View
16 samples/select_n.coffee
@@ -0,0 +1,16 @@
+#!/usr/bin/env coffee
+
+ assert = require 'assert'
+ hive = require '../../'
+
+ client = hive.createClient
+ version: '0.7.1-cdh3u2'
+ server: '127.0.0.1'
+ port: 10000
+ timeout: 1000
+
+ client.execute 'use my_db', (err) ->
+ client.query 'select * from my_table', 10, (err, results) ->
+ assert.ifError(err)
+ console.log(results)
+ client.end()
Please sign in to comment.
Something went wrong with that request. Please try again.