9.3 客户端

来源:互联网 发布:外国人吃西洋参吗 知乎 编辑:程序博客网 时间:2024/04/28 15:01

HBase的客户端HTable负责寻找承载感兴趣的行的RegionServer,它通过查找.META.和-ROOT-表来完成这项工作。在定位了需要的region后,这些信息会被缓存在客户端,以便后续查询无需重复之前的查找过程。当region由于master运行的负载均衡或所在RegionServer的死掉等原因被重新分配时,客户端将重新查找目录表以确定用户所需region的新位置。

管理方法是通过HBaseAdmin实现的。

9.3.1 连接

连接配置信息可以查看Section 2.3.4, “Client configuration and dependencies connecting to an HBase cluster”

HTable实例不是现车安全的,在同一时刻一个线程仅使用一个HTable实例。当创建HTable实例时,建议使用相同的HBaseConfiguration实例。这可以确保能够共享你想要的ZooKeeper和socket实例。比如,我们跟推荐一下做法:

HBaseConfiguration conf = HBaseConfiguration.create();HTable table1 = new HTable(conf, "myTable");HTable table2 = new HTable(conf, "myTable");

而非以下做法:

HBaseConfiguration conf1 = HBaseConfiguration.create();HTable table1 = new HTable(conf1, "myTable");HBaseConfiguration conf2 = HBaseConfiguration.create();HTable table2 = new HTable(conf2, "myTable");

更多有关HBase客户端连接的信息可以查看HConnectionManager。

9.3.1.1 连接池

对于要求实现多线程访问的应用程序(比如web服务器),一种方案是使用HTablePool。但正如目前所写的,当使用HTablePool时很难控制客户端消耗的资源量。另一种解决方法是使用HConnection。

// Create a connection to the cluster.HConnection connection = HConnectionManager.createConnection(Configuration);HTableInterface table = connection.getTable("myTable");// use table as needed, the table returned is lightweighttable.close();// use the connection for other access to the clusterconnection.close();

创建HTableInterface实现是非常轻量级的,而且资源时可控的。

9.3.2 写缓存和批处理方法

如果HTable的AutoFlush关闭了,那么Put实例将在写缓存满时将数据发送给RegionServer。写缓存默认是2MB。在一个HTable废弃之前,应调用close()或flushCommits()以确保Put中的数据被写出了,而不丢失。

注意:htable.detete(Delete);不作用于写缓存中,它仅作用与Put。

9.3.3 外部客户端

有关非Java的客户端及协议可以查阅 Chapter 10,Apache HBase External APIs

9.3.4 RowLocks

RowLocks在客户端应用程序接口中,但并不鼓励使用它。因为,若使用不慎将到时RegionServer被锁。


http://hbase.apache.org/book/client.html

0 0
原创粉丝点击