HBase Client通过查询hbase:meta表找到感兴趣的RegionServers,这些RegionServers服务于特定的行范围。找到region(s)后,Client联络服务此region(s)的RegionServer,而不是联络master来处理读写请求。当执行负载均衡或RegionServer死亡时,master就会将region(s)重新分配,Client就需要重新查询目录表确定用户region的新位置。
1 集群连接
连接配置信息的API在HBase 1.0有所改变,参考Client configuration and dependencies connecting to an HBase cluster。
1.1 HBase 1.0 API
HBase 1.0简洁地返回接口而不是特定类型,通过ConnectionFactory
获取Connection
对象,然后在需要时获取Table
,Admin
,RegionLocator
等实例,用完后关闭实例,最后在退出时清理Connection
实例。Connections
是重量级对象但是线程安全,所有你只需创建一个该实例为你的应用程序。Table
,Admin
,RegionLocator
等实例是轻量级的,创建完使用,使用完关闭。
1.2 HBase 1.0 API前
HBase 1.0 API前运用HTable
与HBase集群交互,Table
实例不是线程安全的,任何时候只允许一个线程操作该实例。为了共享ZooKeeper和socket实例,运用同样的HBaseConfiguration实例,如下:
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");
连接池
当应用程序需要高并发访问时,可以预先创建Connection
,如下:
// Create a connection to the cluster.
Configuration conf = HBaseConfiguration.create();
try (Connection connection = ConnectionFactory.createConnection(conf)) {
try (Table table = connection.getTable(TableName.valueOf(tablename)) {
// use table as needed, the table returned is lightweight
}
}
构建HTableInterface
实现非常轻量级,资源也是可控的。
HTablePool
在HBase 0.94,0.95,0.96中废弃,0.98.1中移除,HConnection
在HBase 1.0中废弃,代替使用Connection
。
2 写缓存和批处理
在HBase 1.0及之后,废弃支持Table的HTable
,Table不在自动autoflush,运用BufferedMutator
类。
HTable
废弃前,通过调用close()
,flushCommits()
,刷写缓存。
更细粒度批量Puts
和Deletes
,参见batch。
3 外部客户端
非Java客户端和自定义协议相关信息,参见Apache HBase External APIs。