这是一篇译文,原作者为carterp@google.com。
为什么有接口变化
把用户所需(功能)与(这些功能)如何递送区分开来(Separate what a user needs from how it’s delivered)释放实现,实现能够单独演化使注入客户代码变得容易(如基准测试,数据装饰等)API变化初瞥
连接管理已经移到类ConnectionFactory新增接口:Connection,Table,RegionLocator,和Admin原始构造函数被标记为过时以支持工厂方法 一些冗余被清理:清理了一些不增加功能的重载函数 表现在只能用TableName,而不是String或byte[]没有了tableNameOrRegionName参数表上的数据操作:
Result get(Get get)ResultScanner getScanner(Scan scan)void put(Put put)void delete(Delete delete)还有其它有条件的修改,批操作,协程服务代用等。
Region信息: RegionLocator
HRegionLocation getRegionLocation(final byte [] row)Pair<byte[][],byte[][]> getStartEndKeys()以及一些以上调用的变体。
元数据操作:Admin
boolean isMasterRunning()HTableDescriptor[] listTables(Pattern pattern)void createTable(HTableDescriptor desc)void deleteTable(final TableName tableName)void compact(final TableName tableName)void snapshot(final String snapshotName,final TableName tableName)等。
调用接口工厂:Connection
这个调用接口是连接句柄,但把它视为所有调用接口的工厂可能会更适合。
Table getTable(TableName tableName)Table getTable(TableName tableName,ExecutorService pool)RegionLocator getRegionLocator(TableName tableName)Admin getAdmin()
如何使用它呢--以前的做法:
Configuration cfg = HBaseConfiguration.create(); HTable t = new HTable(cfg, “mytable”); t.put(new Put(row).add(fam, qual, value)); t.close(); 如何使用它呢--新的做法: Connection conn = ConnectionFactory.createConnection(); TableName tablename = TableName.valueOf(“mytable”); Table t = conn.getTable(tablename); t.put(new Put(row).add(fam, qual, value)); t.close(); conn.close(); public class Putter { Connection conn_; public Putter(Connection conn) { conn_ = conn; } public void put(TableName tablename, byte[] row, byte[] fam, byte[] qual, byte[] value) { Table t = conn.getTable(tablename); t.put(new Put(row).add(fam, qual, value)); t.close(); } } 对象生命周期 接口Connection Table, Admin, RegionLocator 重量级重轻创建代价高昂廉价线程安全性是否使用方法进程内创建一个并共享单线程上下文中随需创建
问题?