Hbase预分区种子生成

提前生成Hbase预分区种子,在创建Hbase表时也进行相应的预分区,同时设置预分区的个数,预分区的范围对应Hbase监控页面的Region Server的start key与End key,从而使数据能够均匀的分布于各个Region中.给捷哥赞一个~

private static final String[] PARTITIONS = generatPartitionSeed();


//生成3844个分区种子
public static String[] generatPartitionSeed(){
    List<Character>seeds = List.newArrayList();
    for(int i='0';i<='9';i++){
        seeds.add((char) i);
    }
    for(int i = 'A';i<='Z';i++){
        seeds.add((char) i);
    }
    for(int i = 'a';i<='z';i++){
        seeds.add((char) i);
    }
    int k = 0;
   //获得总共的分区个数
    String[] partions = new String[seeds.size()*seeds.get(j));
    for(int i =0;i<seeds.size();i++){
        for(int j=0;j<seeds.size();j++){
           //两两组合成为分区start key
            partions[k] = StringUtil.join(seeds.get(i),seeds.get(j));
            k++;
        }
    }
    return partions;
}    

//按指定数量生成分区种子
public static String[] generatPartitionSeed(int limit){
    int size = PARTITIONS.length;
    int[] space = new int[limit];
    for(int pt = 0;pt<size;){
        for(int j=0;j<space.length;j++){
            ++space[j];
            pt++;
            if(pt == size){
                break;
            }
        }
    }
String[] seed = new String[limit+1];
int position = 0;
for(int i =0;i<space.length;i++){
      seed[i] = PARTITIONS[position];
      position += space[i];
}
seed[seed.length -1] = PARTITIONS[PARTITIONS.length -1];
return seed;
}

//生成后的rowkey
public static String generatRow(String str){
    int i = Math.abs(str.hashCode() % PARTITION.length);
    return StringUtils.join(PARTITIONS[i],"-",str);
}

public static void main(String[] args){
    String[] arr = generatPartitionSeed(100);
    for(int i = 0;i<arr.length;i++){
        System.out.println(arr[i]);
    }
}

 

posted on 2016-05-25 16:16  松伯  阅读(838)  评论(0编辑  收藏  举报