1.配置文件和连接信息
// 两个变量的声明:
private static Connection connection = null;
private static Admin admin = null;
static{
try {
// 1.获取配置文件信息
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");
// 2.创建连接对象
connection = ConnectionFactory.createConnection(configuration);
// 3.创建admin对象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
2.判断表是否存在
// 4.判断表是否存在
public static boolean isTableExist(String tableName) throws IOException {
// 5.判断表是否存在
boolean exists = admin.tableExists(TableName.valueOf(tableName));
// 6.返回结果
return exists;
}
3.删除表
// 13.删除表
public static void dropTable(String tableName) throws IOException {
// 13.1判断表是否存在
if(!isTableExist(tableName)){
System.out.println(tableName + "表不存在!");
return;
}
// 13.2使表下线
admin.disableTable(TableName.valueOf(tableName));
// 13.3删除表
admin.deleteTable(TableName.valueOf(tableName));
}
4.单独创建关闭资源
public static void close(){
if(admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
5.测试和关闭
public static void main(String[] args) throws IOException {
// 8.测试表是否存在
System.out.println(isTableExist("stu1"));
// 14.删除表测试
dropTable("stu1");
// 12.创建完测试表是否存在
System.out.println(isTableExist("stu1"));
// 9.关闭资源的调用
close();
}
整体详情:
package test;
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
public class DeleteTable {
// 两个变量的声明:
private static Connection connection = null;
private static Admin admin = null;
static{
try {
// 1.获取配置文件信息
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum","vincen,vincen1,vincen2");
// 2.创建连接对象
connection = ConnectionFactory.createConnection(configuration);
// 3.创建admin对象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
// 4.判断表是否存在
public static boolean isTableExist(String tableName) throws IOException {
// 5.判断表是否存在
boolean exists = admin.tableExists(TableName.valueOf(tableName));
// 6.返回结果
return exists;
}
// 13.删除表
public static void dropTable(String tableName) throws IOException {
// 13.1判断表是否存在
if(!isTableExist(tableName)){
System.out.println(tableName + "表不存在!");
return;
}
// 13.2使表下线
admin.disableTable(TableName.valueOf(tableName));
// 13.3删除表
admin.deleteTable(TableName.valueOf(tableName));
}
// 7.关闭资源
public static void close(){
if(admin != null){
try {
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
// 8.测试表是否存在
System.out.println(isTableExist("stu1"));
// 14.删除表测试
dropTable("stu1");
// 12.创建完测试表是否存在
System.out.println(isTableExist("stu1"));
// 9.关闭资源的调用
close();
}
}