packageHBaseTest;/*** Created by root on 11/11/22.*/
importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.HBaseAdmin;importorg.apache.hadoop.hbase.client.HTable;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.util.Bytes;public classHBaseTestCase {/***@paramargs*/
public static voidmain(String[] args) {//TODO Auto-generated method stub
String tableName = "test";
String columnFamily= "cf";try{if (true ==HBaseTestCase.delete(tableName)) {
System.out.println("Delete Table " + tableName + " success!");
}
HBaseTestCase.create(tableName, columnFamily);
HBaseTestCase.put(tableName,"row1", columnFamily, "column1","data1");
HBaseTestCase.put(tableName,"row2", columnFamily, "column2","data2");
HBaseTestCase.put(tableName,"row3", columnFamily, "column3","data3");
HBaseTestCase.put(tableName,"row4", columnFamily, "column4","data4");
HBaseTestCase.put(tableName,"row5", columnFamily, "column5","data5");
HBaseTestCase.get(tableName,"row1");
HBaseTestCase.scan(tableName);
}catch(Exception e) {
e.printStackTrace();
}
}static Configuration cfg =HBaseConfiguration.create();static{
System.out.println(cfg.get("hbase.master"));
}public static voidcreate(String tableName, String columnFamily)throwsException {
HBaseAdmin admin= newHBaseAdmin(cfg);if(admin.tableExists(tableName)) {
System.out.println(tableName+ " exists!");
}else{
HTableDescriptor tableDesc= newHTableDescriptor(tableName);
tableDesc.addFamily(newHColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println(tableName+ " create successfully!");
}
}public static voidput(String tablename, String row, String columnFamily,
String column, String data)throwsException {
HTable table= newHTable(cfg, tablename);
Put put= newPut(Bytes.toBytes(row));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(data));
table.put(put);
System.out.println("put '" + row + "', '" + columnFamily + ":" +column+ "', '" + data + "'");
}public static void get(String tablename, String row) throwsException {
HTable table= newHTable(cfg, tablename);
Get get= newGet(Bytes.toBytes(row));
Result result=table.get(get);
System.out.println("Get: " +result);
}public static void scan(String tableName) throwsException {
HTable table= newHTable(cfg, tableName);
Scan s= newScan();
ResultScanner rs=table.getScanner(s);for(Result r : rs) {
System.out.println("Scan: " +r);
}
}public static boolean delete(String tableName) throwsIOException {
HBaseAdmin admin= newHBaseAdmin(cfg);if(admin.tableExists(tableName)) {try{
admin.disableTable(tableName);
admin.deleteTable(tableName);
}catch(Exception e) {
e.printStackTrace();return false;
}
}return true;
}
}