バージョン毎のHBaseアクセス方法

とりあえずめもっとく

0.90系。馬本に書かれているのはこのパターン。

		…
		HTablePool pool = new HTablePool(...);
		...

		HTableInterface table = null;
		try {
			Put put = new Put(...);
			...
			table = pool.getTable(…);
			table.put(put);
		} catch (IOException e) {
			...
		} finally {
			if (table != null) {
				pool.putTable(table);
			}
		}

0.94系

		…
		HTablePool pool = new HTablePool(...);
		...
		ResultScanner scanner = null;
		HTableInterface table = null;
		try {
			table = pool.getTable(…);
			Get get = new Get(...);
			...
			Scan scan = new Scan(get);
			...
			scanner = table.getScanner(scan);
			for (Result result : scanner) {
				List<KeyValue> keyValueList = result.list();
				...
			}
			...
		} catch (IOException e) {
			...
		} finally {
			if(scanner != null) {
				scanner.close();
			}
			if(table != null) {
				try {
					table.close();
				} catch (IOException e) {
					...
				}
			}
		}

ただし[HBASE-6580] Deprecate HTablePool in favor of HConnection.getTable(...) - ASF JIRAによると0.94.11以降HTablePoolはdeprecateになったのでそれ以降ならHConnectionを使う。

		…
		HConnection connection = HConnectionManager.createConnection(…)
		...
		ResultScanner scanner = null;
		HTableInterface table = null;
		try {
			table = connection.getTable(…);
			Get get = new Get(...);
			...
			Scan scan = new Scan(get);
			...
			scanner = table.getScanner(scan);
			for (Result result : scanner) {
					List<KeyValue> keyValueList = result.list();
					...
			}
			...
		} catch (IOException e) {
			...
		} finally {
			if(scanner != null) {
				scanner.close();
			}
			if(table != null) {
				try {
					table.close();
				} catch (IOException e) {
					...
				}
			}
		}

番外編としてasynchbaseを使うという手もある。
これを使えばHBase 0.90でビルドしたものでもHBase 0.94にアクセスできるし逆も出来る。

			HBaseClient hbaseClient = new HBaseClient(…)
			GetRequest request = new GetRequest(...);
			...
			Deferred<ArrayList<KeyValue>> deferred = hbaseClient.get(request);
			deferred.join();
			...

HBase 0.94系からHBase 0.90のテーブルにHTable経由でアクセスするとこんなエラーがでるけどasynchbaseを使えば解決

org.apache.hadoop.hbase.client.NoServerForRegionException: Unable to find region for …  after ... tries.
        at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegionInMeta(HConnectionManager.java:1095)
        at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:1000)
        at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegionInMeta(HConnectionManager.java:1102)
        at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:1004)
        at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:961)