Permalink
Browse files

New events `row-first` and `row-last`

  • Loading branch information...
1 parent c496d75 commit 404bcab93dbd014645c2c200909e33cb66272923 @wdavidw committed Nov 22, 2011
Showing with 39 additions and 4 deletions.
  1. +14 −4 README.md
  2. +2 −0 lib/hive.coffee
  3. +1 −0 package.json
  4. +22 −0 test/QueryTest.coffee
View
@@ -50,11 +50,19 @@ Available API
## Hive Query
-The `client.query` function return an object similar to the
-[Readable Stream API][3]. At the moment, we have only implemented `pause` and
-`resume`.
+The `client.query` function implement the [EventEmitter API][3].
+The following events are emitted:
+- `row`
+- `row-first`
+- `row-last`
+- `error`
+- `end`
+
+The `client.query` functionreturn a Node [Readable Stream][4]. It is possible to
+pipe the data into a [Writable Stream][5] but it is your responsibility to emit
+the `data` event, usually inside the `row` event.
## Raw versus sugar API
@@ -106,4 +114,6 @@ Here's an exemple using our sugar API
[1]: http://hive.apache.org "Apache Hive"
[2]: http://thrift.apache.org "Apache Thrift"
-[3]: http://nodejs.org/docs/v0.6.2/api/streams.html#readable_Stream "Readable Stream API"
+[3]: http://nodejs.org/docs/v0.6.2/api/events.html#events.EventEmitter "EventEmitter API"
+[4]: http://nodejs.org/docs/v0.6.2/api/streams.html#readable_Stream "Readable Stream API"
+[5]: http://nodejs.org/docs/v0.6.2/api/streams.html#writable_Stream "Writable Stream API"
View
@@ -49,10 +49,12 @@ module.exports.createClient = (options = {}) ->
return
rows = rows.map (row) -> row.split '\t'
for row in rows
+ emitter.emit 'row-first', row, 0 if count is 0
emitter.emit 'row', row, count++
if rows.length is size
fetch() unless emitter.paused
else
+ emitter.emit 'row-last', row, count - 1
emitter.readable = false
emitter.emit 'end'
fetch = ->
View
@@ -7,6 +7,7 @@
{ "name": "David Worms", "email": "david@adaltas.com" }
],
"dependencies": {
+ "coffee-script": "latest",
"thrift": "latest"
},
"devDependencies": {
View
@@ -34,16 +34,25 @@ module.exports =
next()
'Query # all': (next) ->
count = 0
+ call_row_first = call_row_last = false
client.query("SELECT * FROM #{table}")
.on 'row', (row, index) ->
assert.eql index, count
count++
assert.ok Array.isArray row
assert.eql row.length, 3
+ .on 'row-first', (row, index) ->
+ assert.eql count, 0
+ call_row_first = true
+ .on 'row-last', (row, index) ->
+ assert.eql count, 54
+ call_row_last = true
.on 'error', (err) ->
assert.ok false
.on 'end', ->
assert.eql count, 54
+ assert.ok call_row_first
+ assert.ok call_row_last
next()
'Query # n': (next) ->
count = 0
@@ -80,6 +89,19 @@ module.exports =
.on 'end', ->
assert.eql count, 54
next()
+ 'Query # header': (next) ->
+ # Test where hive.cli.print.header impact Thrift
+ # answer is no
+ count = 0
+ client.execute 'set hive.cli.print.header=true', (err) ->
+ query = client.query("select * from #{table}", 10)
+ .on 'row', (row, index) ->
+ count++
+ .on 'error', (err) ->
+ assert.ok false
+ .on 'end', ->
+ assert.eql count, 54
+ next()
'Close': (next) ->
client.end()
next()

0 comments on commit 404bcab

Please sign in to comment.