42 lines
1.3 KiB
Java
42 lines
1.3 KiB
Java
|
|
import org.apache.hadoop.conf.Configuration;
|
||
|
|
import org.apache.hadoop.hbase.HBaseConfiguration;
|
||
|
|
import org.apache.hadoop.hbase.Connection;
|
||
|
|
import org.apache.hadoop.hbase.ConnectionFactory;
|
||
|
|
import org.apache.hadoop.hbase.client.Admin;
|
||
|
|
|
||
|
|
public class HBaseConnectionManager {
|
||
|
|
private static Configuration configuration;
|
||
|
|
private static Connection connection;
|
||
|
|
private static Admin admin;
|
||
|
|
|
||
|
|
private HBaseConnectionManager() {}
|
||
|
|
|
||
|
|
public static void init() {
|
||
|
|
configuration = HBaseConfiguration.create();
|
||
|
|
try {
|
||
|
|
connection = ConnectionFactory.createConnection(configuration);
|
||
|
|
admin = connection.getAdmin();
|
||
|
|
System.out.println("Connected to HBase Successfully!");
|
||
|
|
} catch (IOException e) {
|
||
|
|
throw new RuntimeException("HBase连接初始化失败", e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void close() {
|
||
|
|
try {
|
||
|
|
if (admin != null) admin.close();
|
||
|
|
if (connection != null) connection.close();
|
||
|
|
System.out.println("HBase Connection Closed.");
|
||
|
|
} catch (IOException e) {
|
||
|
|
throw new RuntimeException("HBase连接关闭失败", e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Connection getConnection() {
|
||
|
|
return connection;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Admin getAdmin() {
|
||
|
|
return admin;
|
||
|
|
}
|
||
|
|
}
|