First, make sure that the node. js and HBase environments are successfully built.
Insert data: You can use the put Method to insert data into a single column or multiple columns at the same time.
Example:
1: var hbase = require('hbase');
2: var client = hbase({
3: host:'localhost',
4: port:8090
5: });
6: var table = client.getTable('testPut');
7: table.create('cf',function(err,success){
8: this
9: .getRow('row3')
10: .put('cf:a','value1',function(err,success){
11: console.log('insert one column');
12: console.log(success);
13: });
14: });
15: var myTable = client.getTable('test');
16: myTable.create('cf',function(err,success){
17: this
18: .getRow('row2')
19: .put(['cf:a','cf:b','cf:c'],['a','b','c'],function(err,success){
20: console.log('insert multi columns');
21: console.log(success);
22: });
23: });
24: var cells =
25: [{ column:'cf:a',timestamp:Date.now(),$: 'a'},
26: { column:'cf:b',timestamp:Date.now(),$: 'b'},
27: { column:'cf:c',timestamp:Date.now(),$: 'c'}
28: ];
29: myTable.create('cf',function(err,success){
30: this
31: .getRow('row1')
32: .put(cells,function(err,success){
33: console.log('insert multi columns user array');
34: console.log(success);
35: });
36: });
Display result:
Get data: You can use the get method to obtain data. You can obtain only one row and one column or multiple columns in one row at a time.
Example:
1: var hbase = require('hbase');
2: var client = hbase({
3: host:'localhost',
4: port:8090
5: });
6: var myRow = client.getTable('test').getRow('row1');
7: myRow.exists('cf',function(err,exists){
8: if(exists){
9: this.get('cf',function(err,values){
10: console.log('get column family');
11: console.log(values);
12: });
13: }
14: });
15: myRow.exists('cf:a',function(err,exists){
16: if(exists){
17: this.get('cf:a',function(err,value){
18: console.log('get column a');
19: console.log(value);
20: });
21: }
22: });
Display result:
Use the token to obtain data:
1: var hbase = require('hbase');
2: var scanner = hbase({host:'localhost',port:8090}).getScanner('test');
3: scanner.create({
4: batch:3
5: },function(err,success){
6: this.get(function(err,cells){
7: if(err){
8: console.log('err');
9: }
10: if(cells){
11: console.log(cells);
12: }else{
13: this.delete();
14: }
15: });
16: });
17:
Display result:
Delete data: Use the delete method.
1: var hbase = require('hbase');
2: var client = hbase({
3: host:'localhost',
4: port:8090
5: });
6: var table = client.getTable('test');
7: var row = table.getRow('row1');
8: row.delete(function(err,success){
9: if(success){
10: console.log('delete a row');
11: }
12: });
13: var row1 = table.getRow('row3');
14: row.delete('cf:b',function(err,success){
15: if(success){
16: console.log('delete a column');
17: }
18: });
19: row.delete(['cf:a','cf:c',],function(err,success){
20: if(success){
21: console.log('delete multi column');
22: }
23: });
After the deletion is successful, the log Content is displayed.