本文hbase版本基于hbase-1.0.0-cdh5.6.1。Admin接口主要操作表创建,删除,列族的增删,表的清空,region的增删,合并操作。
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.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.util.Bytes;
public class AdminTest {
public static Configuration configuration;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", zk_ips);
configuration.set("hbase.rpc.timeout", "120000");
configuration.set("hbase.client.scanner.timeout.period", "120000");
configuration.set("hbase.regionserver.lease.period", "120000");
}
// 创建表
public void createTable(String tableName) {
System.out.println("create table ......");
Connection con = null;
Admin hBaseAdmin = null;
try {
con = ConnectionFactory.createConnection(configuration);
hBaseAdmin = con.getAdmin();// 新用法
// HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration); //老用法
if (hBaseAdmin.tableExists(TableName.valueOf(tableName))) {// 如果存在要创建的表,那么先删除,再创建
hBaseAdmin.disableTable(TableName.valueOf(tableName));
hBaseAdmin.deleteTable(TableName.valueOf(tableName));
System.out.println(tableName + " is exist,delete....");
}
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor("cf1"));
tableDescriptor.addFamily(new HColumnDescriptor("cf2"));
tableDescriptor.addFamily(new HColumnDescriptor("cf3"));
hBaseAdmin.createTable(tableDescriptor);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(hBaseAdmin != null){
try {
hBaseAdmin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("end create table ......");
}
/**
* 删除整个列族的数据
*/
public void deleteColumnFamily(String tableName, String cf) {
Connection con = null;
Admin hBaseAdmin = null;
try {
con = ConnectionFactory.createConnection(configuration);
hBaseAdmin = con.getAdmin();// 新用法
hBaseAdmin.deleteColumn(TableName.valueOf(tableName), Bytes.toBytes(cf));
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(hBaseAdmin != null){
try {
hBaseAdmin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 删除一张表
*
* @param tableName
*/
public void dropTable(String tableName) {
Connection con = null;
Admin admin = null;
try {
con = ConnectionFactory.createConnection(configuration);
admin = con.getAdmin();
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(admin != null){
try {
admin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 添加一个列族
*/
public void addCF(String tableName, String cf){
Connection con = null;
Admin admin = null;
try {
con = ConnectionFactory.createConnection(configuration);
admin = con.getAdmin();
HColumnDescriptor hd = new HColumnDescriptor(cf);
admin.addColumn(TableName.valueOf(tableName.getBytes()), hd);
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 查询所有的表名
*/
public void listTables(){
Connection con = null;
Admin admin = null;
try {
con = ConnectionFactory.createConnection(configuration);
admin = con.getAdmin();
List tableNames = Arrays.asList(admin.listTableNames());
tableNames.forEach(t -> System.out.println(t));
} catch (MasterNotRunningException e) {
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
String tablename = "h_test";
AdminTest admin = new AdminTest();
admin.createTable(tablename);
//删除列族cf3
admin.deleteColumnFamily(tablename, "cf3");
//增加列族cf4
admin.addCF(tablename, "cf4");
//列出所有的表名
admin.listTables();
}
}