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