Hbase java client operations

Source: Internet
Author: User

1. Create a table

2. put operation

Public class myHbase {

Private static Configuration conf = null;
Static {
Conf = HBaseConfiguration. create ();
}
/**
* Column familys add column <---> hbase columnName
* @ Param tableName
* @ Param familys
* @ Throws Exception
*/
Public void createTable (String tableName, String [] familys) throws Exception {
HBaseAdmin admin = new HBaseAdmin (conf );
If (! Admin. tableExists (tableName )){
HTableDescriptor desc = new HTableDescriptor (tableName );
For (int I = 0; I <familys. length; I ++ ){
Desc. addFamily (new HColumnDescriptor (familys [I]);
}
Admin. createTable (desc );
} Else {
System. out. println ("the table:" + tableName + "exsists ");
};
}
/**
* @ Param tableName
* @ Param rowKey
* @ Param family
* @ Param qualifier
* @ Param value
* @ Throws Exception
*/
Public void putData (String tableName, String rowKey, String family,
String qualifier, String value) throws Exception {
HTable ht = new HTable (conf, tableName );
Put put = new Put (Bytes. toBytes (rowKey ));
Put. add (Bytes. toBytes (family), Bytes. toBytes (qualifier), Bytes. toBytes (value ));
Ht. put (put );
} Www.2cto.com
Public static void main (String [] agrs) throws Exception {
MyHbase h = new myHbase ();
String [] familys = {"family1", "family2 "};
H. createTable ("mytable", familys );
H. putData ("mytable", "row", "family1", "qualifier: name1", "value1 ");
}
}


Run the java application: log on to hbase shell and find that you can get and scan data:


Hbase (main): 016: 0> scan 'mytable'
Row column + CELL
Row column = family1: qualifier: name1, timestamp = 1354559928275, v
Alue = value1
1 row (s) in 0.0410 seconds

Hbase (main): 017: 0> get 'mytable', 'row', 'family1: qualifier: name1'
COLUMN CELL
Family1: qualifier: na timestamp = 1354559928275, value = value1
Me1
1 row (s) in 0.0120 seconds

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.