package com.wzy.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;public classHbaseApp {public static voidmain(String[] args) {switch (args[0]) {case "create":newHbaseApp().createTable();break;case "put":newHbaseApp().put();break;case "get":new HbaseApp().get();default:
System.out.println("enter true args");
}
}public voidcreateTable(){try{
Configuration conf=HBaseConfiguration.create();
HBaseAdmin admin= newHBaseAdmin(conf);//表的名字
TableName tablename = TableName.valueOf("test3");
HTableDescriptor tdesc= newHTableDescriptor(tablename);//列族的名字
HColumnDescriptor hcd = new HColumnDescriptor("data");
tdesc.addFamily(hcd);
admin.createTable(tdesc);
System.out.println("create table over ! !");
}catch(Exception e){
}
}
@SuppressWarnings("deprecation")public voidput(){try{
HTable table= new HTable(HBaseConfiguration.create(),"test3");
Put put= new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("data"), Bytes.toBytes("1"), Bytes.toBytes("value01"));
put.addColumn(Bytes.toBytes("data"), Bytes.toBytes("2"), Bytes.toBytes("value02"));
put.addColumn(Bytes.toBytes("data"), Bytes.toBytes("3"), Bytes.toBytes("value03"));
table.put(put);
table.close();
System.out.println("put data over ! !");
}catch(Exception e){
}
}
@SuppressWarnings("deprecation")public void get(){try{
HTable table= new HTable(HBaseConfiguration.create(), "test3");
Getget = new Get(Bytes.toBytes("row1"));get.addColumn(Bytes.toBytes("data"), Bytes.toBytes("1"));get.addColumn(Bytes.toBytes("data"), Bytes.toBytes("2"));
Result r= table.get(get);
KeyValue kv= r.getColumnLatest(Bytes.toBytes("data"), Bytes.toBytes("1"));
System.out.println("data:1.key"+Bytes.toString(kv.getKey()));
System.out.println("data:1.value"+Bytes.toString(kv.getValue()));
table.close();
System.out.println("get over !");
}catch(Exception e){
}
}public voidscan(){try{
HTable table= new HTable(HBaseConfiguration.create(), "test3");
Scan scan= newScan();
ResultScanner rs=table.getScanner(scan);for(Result r : rs){
System.out.println(Bytes.toString(r.getRow())+":"+Bytes.toString(r.getColumnLatestCell(Bytes.toBytes("data"), Bytes.toBytes("1")).getValue()));
}
rs.close();
table.close();
}catch(Exception e){
}
}public voiddelete(){try{
HTable table= new HTable(HBaseConfiguration.create(), "test3");
Delete d= new Delete(Bytes.toBytes("row1"));
table.delete(d);
table.close();
}catch(Exception e){
}
}public voiddrop(){try{
Configuration conf=HBaseConfiguration.create();
HBaseAdmin admin= newHBaseAdmin(conf);
admin.disableTable("test3");
admin.deleteTable("test3");
admin.close();
}catch(Exception e){
}
}
}