RegionServer節點擴充後,需要將一部分原有Region遷移到新的RegionServer中,使各RegionServer負載平衡。
為了驗證多了一個節點後的HBase的寫效能提升,需要使每次put時List中的RowKey平均分配到現有的所有Region中,以達到使所有RegionServer並發處理的目的。
下面的代碼是這種均勻RowKey構建的元代碼:
import java.util.ArrayList;
import java.util.List;
public class externTest {
public static long TOTAL_NUMS = 145;
public static int REGION_NUMS = 24;
public static long EACH_PUT_NUMS = 48;
public static void buildString() {
int addition = 0;
String str = null;
List<String> list = new ArrayList<String>();
long curNum = 0;
long putNums = TOTAL_NUMS/EACH_PUT_NUMS; //通常等於總put數-1;
long loopsInOnePut = EACH_PUT_NUMS/REGION_NUMS; //一次put所需的內層迴圈數,也即是curNum自增數
// 處理迴圈內的
for (long k = 0; k < putNums; k++) {
for (long i = 0; i < loopsInOnePut; i++) {
for (int j = 0; j < REGION_NUMS; j++) {
//A-Z使用同一個數值
addition = j % REGION_NUMS;
str = num2ABC(addition);
//構建本條記錄字串
System.out.println(str + curNum);
list.add(str);
}
curNum++; //一次迴圈後當前尾數+1
}
// TODO: 執行一次put
System.out.println("put");
list.clear();
}
// 處理迴圈外的,肯定小於EACH_PUT_NUMS,最後一次put操作
long lastNums = TOTAL_NUMS % EACH_PUT_NUMS; //還剩多少記錄要put
long lastloops = lastNums / REGION_NUMS; //curNum還要自增多少
long numPlus = lastNums % REGION_NUMS; //最後額外補充多少條記錄
for (long i = 0; i < lastloops; i++) {
for (int j = 0; j < REGION_NUMS; j++) {
//A-Z使用同一個數值
addition = j % REGION_NUMS;
str = num2ABC(addition);
//構建本條記錄字串
System.out.println(str + curNum);
list.add(str);
}
curNum++; //一次迴圈後當前尾數+1
}
// 將迴圈外
for (int j = 0; j < numPlus; j++) {
//A-Z使用同一個數值
addition = j % REGION_NUMS;
str = num2ABC(addition);
//構建本條記錄字串
System.out.println(str + curNum);
list.add(str);
}
// TODO: 執行一次put
System.out.println("put");
list.clear();
return;
}
public static String num2ABC(int num) {
String str = null;
switch (num) {
case 0:
str = new String("A");
break;
case 1:
str = new String("B");
break;
case 2:
str = new String("C");
break;
case 3:
str = new String("D");
break;
case 4:
str = new String("E");
break;
case 5:
str = new String("F");
break;
case 6:
str = new String("G");
break;
case 7:
str = new String("H");
break;
case 8:
str = new String("I");
break;
case 9:
str = new String("J");
break;
case 10:
str = new String("K");
break;
case 11:
str = new String("L");
break;
case 12:
str = new String("M");
break;
case 13:
str = new String("N");
break;
case 14:
str = new String("O");
break;
case 15:
str = new String("P");
break;
case 16:
str = new String("Q");
break;
case 17:
str = new String("R");
break;
case 18:
str = new String("S");
break;
case 19:
str = new String("T");
break;
case 20:
str = new String("U");
break;
case 21:
str = new String("V");
break;
case 22:
str = new String("W");
break;
case 23:
str = new String("X");
break;
default:
str = new String("Z");
break;
}
return str;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Test my Java!");
buildString();
}
}