Skip to content

Instantly share code, notes, and snippets.

@simonmorley
Forked from blmarket/README.mkd
Created December 3, 2017 16:23
Show Gist options
  • Save simonmorley/39e8ab368f05ab05bbd5e81b04c4c4a6 to your computer and use it in GitHub Desktop.
Save simonmorley/39e8ab368f05ab05bbd5e81b04c4c4a6 to your computer and use it in GitHub Desktop.
HBase with thrift using node.js

HBase with thrift using node.js

Install (on Mac OS X)

homebrew

Trivial

hbase

used https://github.com/seomoz/homebrew-cloudera

thrift

due to build failure in default formula(my OS X clang doesn't have TR1),

used https://gist.github.com/duedal/6934417

brew install https://gist.github.com/duedal/6934417/raw/d802f592d66c5bb9908db8a7c75ae57262dafa06/thrift.rb

(you may need github api key because it accesses github several times)

generate HBase.js(and more)

For me, thrift file is on

/usr/local/Cellar/hbase/0.94.6-cdh4.3.0/libexec/src/main/resources/org/apache/hadoop/hbase/thrift

move to the directory and execute

thrift --gen js:node Hbase.thrift

that's it! gen-nodejs will be generated. copy these files into your working directory(where test.coffee is in)

use with node.

npm install thrift
coffee test.coffee
util = require 'util'
thrift = require 'thrift'
hbase = require './Hbase'
{Mutation} = require './Hbase_types'
HOST = 'ec2-54-249-207-247.ap-northeast-1.compute.amazonaws.com'
PORT = 9090
inspect = (obj) -> util.inspect obj, { colors: true, depth: null }
debug = (err, res) -> console.log err? && err || inspect(res)
conn = thrift.createConnection HOST, PORT, {
transport: thrift.TBufferedTransport
protocol: thrift.TBinaryProtocol
}
conn.on 'connect', ->
console.log 'connected'
client = thrift.createClient(hbase, conn)
# client.getTableNames (err, data) ->
# console.log err
# console.log data
# client.getColumnDescriptors 'item_id_test', (err, data) ->
# console.log err
# console.log data
client.mutateRow 'item_id_test', 'asdf', [
new Mutation({ column: 'f1:test1', value: 'value1' })
new Mutation({ column: 'f1:test2', value: 'value2' })
], {}, ->
console.log arguments
return
client.getRow 'item_id_test', 'asdf', {}, debug
# client.getTableRegions 'user_action_log', (err, list) ->
# for region in list
# toBuffer = (value) ->
# ret = new Buffer(8)
# ret.fill(0)
# ret.write value, null, null, 'binary'
# return ret
# console.log JSON.stringify(region.startKey), toBuffer(region.startKey)
# console.log JSON.stringify(region.endKey), toBuffer(region.endKey)
# # console.log toBuffer(region.startKey)
# # console.log toBuffer(region.endKey)
conn.on 'error', (err) ->
console.log 'error', err
console.log hbase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment