NN的随笔

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

首先要确保Nodejs和HBase环境搭建成功。

插入数据:使用put方法,插入数据既可以是插入单列也可以同时插入多列。

示例:

   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:  });
 
显示结果:
put

获取数据:获取数据使用get方法,同样的获取数据时可以一次只获取一行一列,或者一行多列。

示例:

   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:  });
 

显示结果:

get

使用scanner获取数据:

   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:   

显示结果:

scanner

删除数据:使用delete方法

 

   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:  });

删除成功后即会显示log内容。

posted on 2012-10-15 21:09  NNS  阅读(6206)  评论(1编辑  收藏  举报