测试类
package test1;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class MainHB {
public static void main(String[] args) {
try {
HBaseOp ho = new HBaseOp();
String[] name = new String[2];
String tab = "Tab1";
name[0] = "f1";
name[1] = "f2";
ho.createTable(tab, name);
/*
* String[] name1=new String[2]; String tab1="Tab11"; name1[0]="f1";
* name1[1]="f2"; ho.createTable(tab1, name1);
*/
;
System.out.println("建表成功 OK");
} catch (Exception e) {
e.printStackTrace();
}
try { HBaseOp ho1=new HBaseOp();
ho1.deleteTable("Tab1");
System.out.println("删除成功");
}
catch(Exception e){
e.printStackTrace();
}
}
}
方法类
package test1;
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.client.HBaseAdmin;
public class HBaseOp {
// TODO Auto-generated method stub
Configuration conf=HBaseConfiguration.create();
public void createTable(String strTab,String[] arrCf) throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(strTab)) {
System.out.println("Table"+strTab+"exists.");
}
else {
HTableDescriptor fs=new HTableDescriptor(strTab);
for(String cf:arrCf) {
HColumnDescriptor ff=new HColumnDescriptor(cf);
ff.setTimeToLive(10000);
fs.addFamily(ff);
}
admin.createTable(fs);
}
admin.close();
}
public void deleteTable(String strTab)throws Exception
{
HBaseAdmin admin = new HBaseAdmin(conf);
if (!admin.tableExists(strTab)) {//判断表是否存在
System.out.println(strTab + "不存在");
}
else if(admin.isTableEnabled(strTab)) {//如果表处于disable
admin.disableTable(strTab);
admin.deleteTable(strTab);
System.out.println(strTab + " deleted");
}
else
{
admin.deleteTable(strTab);
System.out.println(strTab + " deleted");
}
admin.close();
}
}