Nodejs operating HBase

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.