If you’re use HBase >= 0.96.x,
please use hbase-rpc-client which CoffeeScript HBase Implementation with protobuf.
Install
$ npm install hbase-client --save
Run Unit Test
Start local hbase server
$ ./start-local-hbase.sh
If everything go fine, run tests
$ make test
Stop hbase server
$ ./stop-local-hbase.sh
Get Started with CRUD
Create a hbase client through zookeeper:
var HBase = require('hbase-client');
var client = HBase.create({
zookeeperHosts: [
'127.0.0.1:2181' // only local zookeeper
],
zookeeperRoot: '/hbase-0.94.16',
});
client.getRow('someTableName', 'rowkey1', ['f1:name', 'f1:age'], function (err, row) {
console.log(row);
});
Delete the row we put:
client.deleteRow('someTableName', 'rowkey1', function (err) {
console.log(err);
});
Usage
get(table, get, callback): Get a row from a table
var HBase = require('hbase-client');
var client = HBase.create({
zookeeperHosts: [
'127.0.0.1:2181', '127.0.0.1:2182',
],
zookeeperRoot: '/hbase-0.94',
});
// Get `f1:name, f2:age` from `user` table.
var param = new HBase.Get('foo');
param.addColumn('f1', 'name');
param.addColumn('f1', 'age');
client.get('user', param, function (err, result) {
console.log(err);
var kvs = result.raw();
for (var i = 0; i < kvs.length; i++) {
var kv = kvs[i];
console.log('key: `%s`, value: `%s`', kv.toString(), kv.getValue().toString());
}
});
getRow(table, rowkey, columns, callback)
client.getRow(table, row, ['f:name', 'f:age'], function (err, row) {
row.should.have.keys('f:name', 'f:age');
});
// return all columns, like `select *`
client.getRow(table, row, function (err, row) {
row.should.have.keys('f:name', 'f:age', 'f:gender');
});
client.getRow(table, row, '*', function (err, row) {
row.should.have.keys('f:name', 'f:age', 'f:gender');
});
put(table, put, callback): Put a row to table
var put = new HBase.Put('foo');
put.add('f1', 'name', 'foo');
put.add('f1', 'age', '18');
client.put('user', put, function (err) {
console.log(err);
});
Atomically checks if a row/family/qualifier value matches the expected value.
If it does, it adds the put.
If the passed value is null, the check is for the lack of column (ie: non-existance)
eg.
var put = new Put('rowKey');
put.add('f:col1', 'value');
client.checkAndPut(tableName, 'rowKey', 'f', 'col1', 'val1', put, function (err, hasPut) {
if (err) {
// Do something
}
console.log(hasPut); // true or false, indicates if check passed and put
});
Atomically checks if a row/family/qualifier value matches the expected value.
If it does, it adds the delete.
If the passed value is null, the check is for the lack of column (ie: non-existance)
eg.
var deleter = new Delete('rowKey');
deleter.deleteColumns('f', 'col1');
client.checkAndDelete(tableName, 'rowKey', 'f', 'col1', null, deleter, function (err, hasDeleted) {
if (err) {
// Do something
}
console.log(hasPut); // true or false, indicates if check passed and deleted
});
FilterList filterList = new FilterList({operator: FilterList.Operator.MUST_PASS_ALL});
filterList.addFilter(new FirstKeyOnlyFilter());
filterList.addFilter(new KeyOnlyFilter());
Scan scan = new Scan(Bytes.toBytes("scanner-row0"));
scan.setFilter(filterList);
Nodejs code:
var filters = require('hbase-client').filters;
var filterList = new filters.FilterList({operator: filters.FilterList.Operator.MUST_PASS_ALL});
filterList.addFilter(new filters.FirstKeyOnlyFilter());
filterList.addFilter(new filters.KeyOnlyFilter());
var scan = new Scan('scanner-row0');
scan.setFilter(filterList);
client.getScanner('user', scan, function (err, scanner) {\
var index = 0;
var next = function (numberOfRows) {
scanner.next(numberOfRows, function (err, rows) {
// console.log(rows)
should.not.exists(err);
if (rows.length === 0) {
index.should.equal(5);
return scanner.close(done);
}
rows.should.length(1);
var closed = false;
rows.forEach(function (row) {
var kvs = row.raw();
var r = {};
for (var i = 0; i < kvs.length; i++) {
var kv = kvs[i];
kv.getRow().toString().should.equal('scanner-row' + index++);
kv.toString().should.include('/vlen=0/');
console.log(kv.getRow().toString(), kv.toString())
}
});
if (closed) {
return scanner.close(done);
}
next(numberOfRows);
});
};
next(1);
});
Scan table and return row filtered by single column value
Java code:
byte [] family = Bytes.toBytes("cf1");
byte [] qualifier = Bytes.toBytes("qualifier2");
FilterList filterList = new FilterList({operator: FilterList.Operator.MUST_PASS_ALL});
filterList.addFilter(new SingleColumnValueFilter(family, qualifier, CompareOp.LESS_OR_EQUAL, Bytes.toBytes("scanner-row0 cf1:qualifier2")));
filterList.addFilter(new SingleColumnValueFilter(family, qualifier, CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("scanner-"))));
filterList.addFilter(new SingleColumnValueFilter(family, qualifier, CompareOp.NOT_EQUAL, new BitComparator(Bytes.toBytes("0"), BitComparator.BitwiseOp.XOR)));
filterList.addFilter(new SingleColumnValueFilter(family, qualifier, CompareOp.NOT_EQUAL, new NullComparator()));
filterList.addFilter(new SingleColumnValueFilter(family, qualifier, CompareOp.EQUAL, new RegexStringComparator("scanner-*"))));
filterList.addFilter(new SingleColumnValueFilter(family, qualifier, CompareOp.EQUAL, new SubstringComparator("cf1:qualifier2"))));
Scan scan = new Scan(Bytes.toBytes("scanner-row0"));
scan.setFilter(filterList);
Nodejs code:
var filterList = new filters.FilterList({operator: filters.FilterList.Operator.MUST_PASS_ALL});
var family = 'cf1';
var qualifier = 'qualifier2';
filterList.addFilter(new filters.SingleColumnValueFilter(family, qualifier, 'LESS_OR_EQUAL', 'scanner-row0 cf1:qualifier2'));
filterList.addFilter(new filters.SingleColumnValueFilter(family, qualifier, 'GREATER_OR_EQUAL', new filters.BinaryPrefixComparator('scanner-')));
filterList.addFilter(new filters.SingleColumnValueFilter(family, qualifier, 'NOT_EQUAL', new filters.BitComparator('0', filters.BitComparator.BitwiseOp.XOR)));
filterList.addFilter(new filters.SingleColumnValueFilter(family, qualifier, 'NOT_EQUAL', new filters.NullComparator()));
filterList.addFilter(new filters.SingleColumnValueFilter(family, qualifier, 'EQUAL', new filters.RegexStringComparator('scanner-*')));
filterList.addFilter(new filters.SingleColumnValueFilter(family, qualifier, 'EQUAL', new filters.SubstringComparator('cf1:qualifier2')));
var scan = new Scan('scanner-row0');
scan.setFilter(filterList);
client.getScanner('user', scan, function (err, scanner) {\
//TODO:...
});
Copyright (c) 2013 - 2014 Alibaba Group Holding Limited
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
hbase-client
Asynchronous HBase client for Node.js, pure JavaScript implementation.
0.94.0and0.94.16Support HBase Server Versions
If you’re use HBase >= 0.96.x, please use hbase-rpc-client which CoffeeScript HBase Implementation with protobuf.
Install
Run Unit Test
Start local hbase server
If everything go fine, run tests
Stop hbase server
Get Started with
CRUDUsage
get(table, get, callback): Get a row from a tablegetRow(table, rowkey, columns, callback)put(table, put, callback): Put a row to tableputRow(table, rowKey, data, callback)delete(tableName, del, callback)deleteRow(tableName, rowkey, callback)mget(tableName, rows, columns, callback)mput(tableName, rows, callback)mdelete(tableName, rowkeys, callback)Atomic Operations
HBase 0.94 provides two
checkAnd*atomic operations.checkAndPut(tableName, family, qualifier, value, put, callback)Atomically checks if a row/family/qualifier value matches the expected value.
eg.
checkAndDelete(tableName, family, qualifier, value, deleter, callback)Atomically checks if a row/family/qualifier value matches the expected value.
eg.
Scan
Scan table and return row key only
Java code:
Nodejs code:
Scan table and return row filtered by single column value
Java code:
Nodejs code:
TODO
putdeleteBenchmarks
@see docs/benchmark.md
Authors
Thanks for @haosdent support the test hbase clusters environment and debug helps.
License
(The MIT License)
Copyright (c) 2013 - 2014 Alibaba Group Holding Limited
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.