Skip to content

Latest commit

 

History

History
508 lines (306 loc) · 9.06 KB

README.md

File metadata and controls

508 lines (306 loc) · 9.06 KB

#Use thrift2 to CRUD for hbase#

##Get ready for start hadoop hbase thrift2

  • start-dfs.sh

  • start-hbase.sh

  • hbase-daemon.sh start thrift2

####if you run command example display by : jps####

2423 DataNode

2746 ThriftServer

4854 Jps

2349 NameNode

2668 HMaster

2513 SecondaryNameNode

##1 . create Hbase instance client##

var HBase = require('node-thrift-hbase');

var config = {

    host: 'master',

    port: 9090

};

var hbaseClient = HBase.client(config);

#2 . Use get or getRow function to query data

##get(table,get,callback)

var get = hbaseClient.Get('row1');    //row1 is rowKey

//get.addFamily('cf');  //add not found column is error

//get.addFamily('info');

//get.addColumn('info','name');   //this replace addFamily

//get.addTimestamp('info','name',1414385447707);

//get.addColumn('ecf','name');

//get.setMaxVersions(3);  //default 1

//or Recommend this function add

get.add('info');    //get all family info

get.add('info','name');   //get family and qualifier info:name

get.add('info','name',1414385447707); //get info:name and timestamp

get.add('ecf'); //get other family ecf

get.add('ecf','name');  //get family and qualifier ecf:name

get.add('ecf','name',1414385555890); //get info:name and timestamp


hbaseClient.get('users',get,function(err,data){ 
    //get users table

    if(err){
        console.log('error:',err);
        return;
    }
    
    console.log(err,data);

});

##getRow(table,rowKey,columns,versions,callback)##

###introduce getRow function###

  • hbaseClient.getRow = function (table,rowKey,columns,versions,callback) {

    • //table is must
    • //rowKey is must
    • //columns is not must,the default is get all row value
    • //versions is not must, the default is 1 ,if have this params,string is auto cost number
  • }


###getRow( table, rowKey, callback)###

hbaseClient.getRow('users','row1',function(err,data){ 
    //get users table

    if(err){
        console.log('error:',err);
        return;
    }

    console.log(err,data);

});

###getRow( table, rowKey, columns, callback)###

hbaseClient.getRow('users','row1',['info:name','ecf'],function(err,data){ 
    //get users table

    if(err){
        console.log('error:',err);
        return;
    }

    console.log(err,data);

});

###getRow( table, rowKey, columns, versions, callback)###

hbaseClient.getRow('users','row1',['info:name','ecf'], 2 ,function(err,data){ 
    //get users table

    if(err){
        console.log('error:',err);
        return;
    }

    console.log(err,data);

});
  • //'users' is table name

  • //row1 is rowKey

  • //[] is family or family qualifier

  • //['info:name','info:tel'] is right. info is family, name and tel is qualifier

  • //['info:name','ecf'] is rigth too, info is family , ecf is family

  • //function is callback function

  • //2 is Maxversion ,default is 1


#3 . Use put or putRow function to insert or update data

##put( table, put, callback)##

var put = hbaseClient.Put('row1');    //row1 is rowKey

put.add('info','click','100'); // 100 must be string

put.add('info','name','beijing',new Date().getTime());

put.add('ecf','name','zhudaxian');

hbaseClient.put('users',put,function(err){ //put users table

    if(err){

        console.log('error:',err);

        return;

    }

    console.log(err,'put is successfully');

});
  • //info and ecf are family

  • //click and name is qualifier

  • //beijing is value

  • timestamp is now Date() and this value also by coustom

##putRow(table,row,columns,value,callback)##

hbaseClient.putRow('users','row1','info:name','phoneqq.com',function(err){ 
    //put users table
    
    if(err){
        console.log('error:',err);
        return;
    }
    
    console.log(err,'put is successfully');
});

##putRow(table,row,columns,value,timestamp,callback)##

hbaseClient.putRow('users','row1','info:name','phoneqq.com',1414140874929,function(err){ 
    //put users table
    
    if(err){
        console.log('error:',err);
        return;
    }
    
    console.log(err,'put is successfully');
});
  • //'users' is table name

  • //row1 is rowKey

  • //'info:name' is right. info is family, name is qualifier

  • //function is callback function

  • //phoneqq.com is value

  • //1414140874929 is timestamp ,not must,if not so auto generate new Date()


#4 . Use inc or incRow function to update data

##inc( table, inc, callback)##

var inc = hbaseClient.Inc('row1');    //row1 is rowKey

inc.add('info','counter');

inc.add('info','counter2');

hbaseClient.inc('users',inc,function(err,data){ 
    //inc users table

    if(err){
        console.log('error:',err);
        return;
    }

    console.log(err,data);

});

##incRow( table, rowKey, family:qualifier, callback)##

hbaseClient.incRow('users','row1','info:counter',function(err,data){ //inc users table

    if(err){
        console.log('error:',err);
        return;
    }

    console.log(err,data);
    //data is return new counter object
});

#5 . Use del or delRow function to delete data

##del( table, del, callback)##

var del = hbaseClient.Del('row1');    //row1 is rowKey

//del.addFamily('ips');   //delete family ips
//del.addColumn('info','click2'); //delete family and qualifier info:click2
//del.addTimestamp('info','click3',1414136046864); //delete info:click3 and timestamp

//or Recommend this function add

del.add('info');    //delete all family info
del.add('info','name');   //delete family and qualifier info:name
del.add('info','tel',1414136046864); //delete info:tel and timestamp

del.add('ecf'); //delete other family ecf
del.add('ecf','name');  //delete family and qualifier ecf:name
del.add('ecf','tel',1414136119207); //delete info:tel and timestamp

//del.add('ips'); //is error ,because this family ips is not exist

hbaseClient.del('users',del,function(err){ //put users table
    if(err){
        console.log('error:',err);
        return;
    }
    console.log(err,'del is successfully');
});

##delRow( table, rowKey, family:qualifier, callback)##

hbaseClient.delRow('users','row1','info:name',function(err){ 
    //put users table
    
    if(err){
        console.log('error:',err);
        return;
    }
    
    console.log(err,'del is successfully');
});

##delRow( table, rowKey, family:qualifier, timestamp, callback)##

hbaseClient.delRow('users','row1','info:name',1414137991649,function(err){ 
    //put users table
    
    if(err){
        console.log('error:',err);
        return;
    }
    
    console.log(err,'del is successfully');
});

#6 . Use scan or scanRow function to query data
var scan = hbaseClient.Scan();

//get.addFamily('cf');  //add not found column is error

//scan.addFamily('info');  //add all family

//scan.addStartRow('row1');   //start rowKey

//scan.addStopRow('row1p');   //stop rowKey

//scan.addColumn('info','name');  //add family and qualifier

//scan.addColumn('ecf','name');   //add other family

//scan.setMaxVersions(1); //set maxversions

//scan.addNumRows(10); //search how much number rows

//or Recommend this function add

scan.addStartRow('row1');   //start rowKey

scan.addStopRow('row1p');   //stop rowKey

scan.add('info');    //scan all family info

scan.add('info','name');   //scan family and qualifier info:name

scan.add('ecf'); //scan other family ecf

scan.add('ecf','name');  //scan family and qualifier ecf:name

scan.setMaxVersions(1); //set maxversions

scan.addNumRows(10); //search how much number rows

hbaseClient.scan('users',scan,function(err,data){ //get users table
    if(err){
        console.log('error:',err);
        return;
    }
    console.log(err,data);

//    console.log(err,data[0].columnValues);
});

##scanRow(table,startRow,stopRow,columns,numRows,callback)##

  • //table is search tableName,must

  • //startRow is first rowKey,must

  • //stopRow is end rowKey,must

  • //columns is family or family and qualifier,is not must //example : ['info:name','ecf']

  • //numRows is count rows, is not must,if none the default is 10.

  • //callback is function

##scanRow(table,startRow,stopRow,callback)##

hbaseClient.scanRow('users','row1','row1b',function(err,data){ 
    //get users table
    
    if(err){
        console.log('error:',err);
        return;
    }
    
    console.log(err,data);
});

##scanRow(table,startRow,stopRow,colmuns,callback)##

hbaseClient.scanRow('users','row1','row1b',['info:name','ecf'],function(err,data){ 
    //get users table
    
    if(err){
        console.log('error:',err);
        return;
    }
    
    console.log(err,data);
});

##scanRow(table,startRow,stopRow,columns,numRows,callback)##

hbaseClient.scanRow('users','row1','row1b',['info:name','ecf'],10,function(err,data){ 
    //get users table
    
    if(err){
        console.log('error:',err);
        return;
    }
    
    console.log(err,data);
});