概述
Java API写数据的一般方法static void put(TableName tableName) throws Exception {
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
Table table = NULL;
try {
table = connection.getTable(tableName);
Put put = new Put(Bytes.toBytes("row"));
put.add(fam, qual, HConstants.LATEST_TIMESTAMP, value);
put.setCellVisibility(new CellVisibility(labelExp));
table.put(put);
} finally {
if (table != null) {
table.flushCommits();
}
}
}
主要流程如下:构造配置对象Configuration;
获取连接对象Connection;
获取Table;
构造Put对象
调用table.put提交
下面分步介绍put的实现过程
构造配置对象Configuration
org.apache.hadoop.hbase.HBaseConfiguration#create()
org.apache.hadoop.hbase.HBaseConfiguration#addHbaseResources
org.apache.hadoop.hbase.HBaseConfiguration#checkDefaultsVersion
利用反射创建连接对象Connection
org.apache.hadoop.hbase.client.ConnectionFactory#createConnection
使用传递的conf实例创建一个新的Connection实例。 Connection封装了与集群连接的所有内务管理。
从返回的连接创建的所有表和接口共享zookeeper连接,meta缓存以及连接到RegionServer和HMaster
默认的实现类是org.apache.hadoop.hbase.client.ConnectionImplementation
org.apache.hadoop.hbase.client.ConnectionImplementation#ConnectionImplementationConnectionImplementation(Configuration conf,
ExecutorService pool, User user) throws IOException {
this.conf = conf;
this.user = user;
this.batchPool = pool;
this.connectionConfig = new ConnectionConfiguration(conf);
this.closed = false;
this.pause = conf.getLong(HConstants.HBASE_CLIENT_PAUSE,
HConstants.DEFAULT