hbase中Connection创建,

HConnectionManager.getConnection(HBaseConfiguration.create());

HConnectionManager.createConnection(HBaseConfiguration.create();

//1.3.x及以前使用一下方式创建,HConnectionManager是ConnectionFactory子类,2.x及以上使用一下方式创建链接

Connection hbaseConn = ConnectionFactory.createConnection(HBaseConfiguration.create());

Admin admin = hbaseConn.getAdmin();

通过源码可以看到,Connection接口继承Abortable, Closeable。

Abortable接口,提供两个方法,用于检测服务端或者客户端链接及终止。

hbase开发-Connection连接创建

Closeable接口,提供关闭资源方法。

hbase开发-Connection连接创建

Connection接口:

hbase开发-Connection连接创建

HBaseConfiguration.create(): 加载hbase配置文件属性,默认加载当前类根目录下的配置文件hbase-default.xml,然后加载hbase-site.xml,后者会覆盖前者中的属性信息。

hbase开发-Connection连接创建

hbase开发-Connection连接创建

ConnectionFactory:链接工厂,用于创建Connection链接,如果没有conf配置,则默认加载类根据下配置文件,如果有则使用当前配置。提供以下多种方式创建链接。

hbase开发-Connection连接创建

客户端调用使用自带配置文件hbase-site.xml,将改文件放在项目根目录下即可。该文件中至少需要配置一下信息:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property>
        <name>hbase.rootdir</name>
        <value>hdfs://192.168.2.219:8020/hbase</value>
    </property>
    <property>
        <name>hbase.zookeeper.quorum</name>
        <value>192.168.2.219</value>
    </property>
    <property>
        <name>hbase.zookeeper.property.clientPort</name>
        <value>2181</value>
    </property>
    <property>
        <name>zookeeper.znode.parent</name>
        <value>/hbase</value>
    </property>

</configuration>

hbase开发-Connection连接创建

hbase开发-Connection连接创建

hbase开发-Connection连接创建

ConnectionImplementation:  创建Connection接口实现类(org.apache.hadoop.hbase.client.ConnectionImplementation)对象

hbase开发-Connection连接创建

constructor.newInstance(conf, pool, user);通过反射创建 ConnectionImplementation对象

hbase开发-Connection连接创建

相关文章: