feat: 添加 TableExist 类用于检查 HBase 表是否存在
- 新增 TableExist 类,包含主方法和初始化、关闭连接的方法 - 通过用户输入表名,判断表是否存在并输出结果 - 使用 HBaseConfiguration 和 ConnectionFactory 实现连接 - 添加异常处理和资源关闭逻辑
This commit is contained in:
parent
eefcadd7a4
commit
a25459eae7
52
src/main/java/TableExist.java
Normal file
52
src/main/java/TableExist.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import org.apache.hadoop.conf.Configuration;
|
||||||
|
import org.apache.hadoop.hbase.*;
|
||||||
|
import org.apache.hadoop.hbase.client.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
public class TableExist{
|
||||||
|
public static Configuration configuration;
|
||||||
|
public static Connection connection;
|
||||||
|
public static Admin admin;
|
||||||
|
public static void main(String[] args)throws IOException{
|
||||||
|
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.print("tableName:");
|
||||||
|
String tableName = sc.next();
|
||||||
|
init();
|
||||||
|
|
||||||
|
TableName tn = TableName.valueOf(tableName);
|
||||||
|
if(admin.tableExists(tn)) {
|
||||||
|
System.out.println("table " + tableName + " does exist!");
|
||||||
|
} else {
|
||||||
|
System.out.println("table " + tableName + " don't exist!");
|
||||||
|
}
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
//建立连接
|
||||||
|
public static void init(){
|
||||||
|
//根据 hbase-site.xml文件初始化Configuration 对象
|
||||||
|
configuration = HBaseConfiguration.create();
|
||||||
|
try{
|
||||||
|
//根据 Configuration对象初始化Connection 对象
|
||||||
|
connection = ConnectionFactory.createConnection(configuration);
|
||||||
|
//获取Admin 对象实例
|
||||||
|
admin = connection.getAdmin();
|
||||||
|
}catch (IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
System.out.println("Connect to HBase successfully!");
|
||||||
|
}
|
||||||
|
//关闭连接
|
||||||
|
public static void close(){
|
||||||
|
try{
|
||||||
|
if(admin != null){
|
||||||
|
admin.close();
|
||||||
|
}
|
||||||
|
if(null != connection){
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
}catch (IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user