Skip to content

Instantly share code, notes, and snippets.

@AhianZhang
Created June 4, 2018 14:12
Show Gist options
  • Save AhianZhang/25c6ee36019649e66a9126d2fe128d0f to your computer and use it in GitHub Desktop.
Save AhianZhang/25c6ee36019649e66a9126d2fe128d0f to your computer and use it in GitHub Desktop.
HBaseConn
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
/**
* Created by AhianZhang on 2018/5/29.
*/
public class HbaseConn
{
private static final HbaseConn INSTANCE = new HbaseConn();
private static Configuration configuration;
private static Connection connection;
private HbaseConn(){
if (configuration==null){
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.qurun","hadoop01:9000");
}
}
private Connection getConnection() throws IOException
{
if (connection==null||connection.isClosed()){
connection = ConnectionFactory.createConnection(configuration);
}
return connection;
}
public static Connection getHBaseConn() throws IOException
{
return INSTANCE.getConnection();
}
public static Table getTable(String tableName) throws IOException
{
return INSTANCE.getConnection().getTable(TableName.valueOf(tableName));
}
public static void closeConn() throws IOException
{
if (connection !=null){
connection.close();
}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment