/*通过直接预分区的方式建表*/ private static void printTableRegion(String tableName)throws IOException{ System.out.println("Prtint region of table:"+tableName);//打印reginon帮助信息 HTable table=new HTable(Bytes.Bytes(tableName); Pair<byte[][],byte[][]> pair=table.getStartEndKeys();//打印region起始行健和终止行健列表 for(int n;n<pair.getFirst().length;n++) { byte[] sk=pair.getFirst()[n]; byte[] ek=pair.getSecond()[n]; System.out.println("["+n+1)+"]"+"start key"+(sk.length=8?Bytes.toLong(sk):Bytes.toStringBinary(sk))+",endkey"+ (ek.length=8?Bytes.toLong(ek):Bytes.toStringBinary(ek))); } } public static void main(Sting)[] args)throws IOException,Interrupted Exception{ Configuration conf=new HBaseConfiguration.create(); HBaseAdmin admin=new HBaseAdmin(conf); HtableDescriptor desc=new HtableDescriptor(Bytes.toBytes("tabletest")); HColumnDescriptor coldef=new HColumnDescriptor(Bytes.toBytes("colfam1")); desc.addFamily(coldef); admin.createTable(desc,Bytes.toBytes(1L),Bytes.toBytes(100L),10);//这就是执行建表,并同时分配region的语句 printTableRegions("tabletest"); byte[][] regions=new byte[][]{ Bytes.toBytes("A"); Bytes.toBytes("D"); } desc.setName(Bytes.toBytes("tabletest2")); admin.createTable(desc.regions); printTableRegion("tabletest2"); }
首先第一个region的起始行键和最后一region的终止行键都是空字节,这是HBase默认的规则。
主要语句是createTable(HTableDescriptor desc,byte[] startKey,byte[] endKey,int numRegions)
这个方法能以特定的numRegions来拆分特定的其实行键和终止行键,同时创建表,startKey要小于endKey,这是显然的,同时要注意numRegions不能小于3,这可能也是HBase之前设定好的。
用tableExists()方法检查是否创建成果,