Hbase Table的拆分合并原理与实践 (七)
拆分表
help 'split'
Split entire table or pass a region to split individual region. With the
second parameter, you can specify an explicit split key for the region.
Examples:
split 'tableName'
split 'namespace:tableName'
split 'regionName' # format: 'tableName,startKey,id'
split 'tableName', 'splitKey'
split 'regionName', 'splitKey'
区间分界左开右闭
拆分的时候会产生一个临列,拆分完毕后会自动删除,命令执行结束不代表拆分结束
合并表
help 'merge_region'
Merge two regions. Passing 'true' as the optional third parameter will force
a merge ('force' merges regardless else merge will fail unless passed
adjacent regions. 'force' is for expert use only).
NOTE: You must pass the encoded region name, not the full region name so
this command is a little different from other region operations. The encoded
region name is the hash suffix on region names: e.g. if the region name were
TestTable,0094429456,1289497600452.527db22f95c8a9e0116f0cc13c680396. then
the encoded region name portion is 527db22f95c8a9e0116f0cc13c680396
Examples:
hbase> merge_region 'ENCODED_REGIONNAME', 'ENCODED_REGIONNAME'
hbase> merge_region 'ENCODED_REGIONNAME', 'ENCODED_REGIONNAME', true
Hbase可以自动拆分(数据达到一定量),加true强制合并
编程实现
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.util.Bytes;
public class TestSplitRegion {
private static Configuration conf = null;
private static Connection conn = null;
static{
try {
conf = HBaseConfiguration.create();
conn = ConnectionFactory.createConnection(conf);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
findRegions();
}
public static void findRegions() throws Exception{
Admin admin = conn.getAdmin();
List<HRegionInfo> list = admin.getTableRegions(TableName.valueOf("test"));
System.out.println(list.size());
}
public static void splitTable() throws Exception{
Admin admin = conn.getAdmin();
//split是一个异步方法,执行完立即返回,但不一定拆分完成
admin.split(TableName.valueOf("test"));
System.out.println("over");
}
public static void splitRegion() throws Exception{
Admin admin = conn.getAdmin();
//splitRegion也是一个异步方法,执行完立即返回,但不一定拆分完成
//splitRegion的参数为ENCODED
admin.splitRegion(b("22f4093e06f2a1942396a57e16d6fc27"));
System.out.println("over");
}
public static byte[] b(String str){
return Bytes.toBytes(str);
}
}
hbase拆分表和拆分区域有什么区别?