pandazx's blog

データ分析など雑多な技術ブログ

HBaseでsplit定義ありのテーブル作成

以下のコードでHBaseに a100, b100 でsplitされたregionを3つ持つtable_nameのテーブルが作成されます。
(サンプルコードなので以下のコードでは動作確認してません)

Configuration config = HBaseConfiguration.create();
config.addResource(new Path("/etc/hbase/conf/hbase-site.xml"));
HBaseAdmin admin = new HBaseAdmin(config);

//テーブル定義
HTableDescriptor hTable = new HTableDescriptor("table_name");
HColumnDescriptor hCF = new HColumnDescriptor("column_family");
hTable.addFamily(hCF);
hCF.setCompressionType(Algorithm.GZ); // 圧縮なしはAlgorithm.NONE
hCF.setBlocksize(1 * 1024 * 1024); // 1MB
hCF.setMaxVersions(1);

// regionのsplit定義
String[] splitValueList = {"a", "b"};
byte[] splitValue2 = Bytes.toBytes((short)100);

int size = splitValueList.length;
byte[][] split = new byte[size][];

for(int index = 0; index < size; index++){
  byte[] splitValue = Bytes.toBytes(splitValueList[index]);
  byte[] key = new byte[3];
  key[0] = splitValue[0];
  key[1] = splitValue2[0]; // shortは2byte
  key[2] = splitValue2[1];
  split[index] = key;
}

// テーブル作成
admin.createTable(hTable , split);

上記では、splitで指定するrow keyが単純なキーではなく、コンポジットキーのような複合キーを想定しています。

コンパイル

javac -classpath `hbase classpath` -d classes *.java
jar -cvf hbase_sample.jar -C classes .

上記プログラム実行後は hbase shell で
list して存在を確認
describe table_name で定義確認

splitは http://hostname:60010 のHBase管理ページから辿れるテーブルの詳細ページで確認