本文主要是介绍hbase预分区案例-连续和分散两种方法(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
6.4.1 创建hb_yonghu_index索引表
Hb_yonghu_index表的Rowkey策略:
提前创建预分区。划分10个区域,预计100个数据量,每个分区10个
* *用户id小于10的都划分在“00”
* *用户id大于10且小于20的都划分在“10区域”
* 依次类推.....
* rowkey的策略:
* 分区编号(根据用户id所在划分的分区编号获得,见上面解释)+"-u"+u.getUserId();
/*** 创建索引表*/public static void createIndexTsble(){String tableNameIndex= "hb_yonghu_index";String familyArray[]={"index-rw"};initUserTable(tableNameIndex, familyArray,true);Table tableIndex=HbaseConnectionUtils.getInstance().getTable( tableNameIndex);batchPutIndex(tableIndex);//索引表}/**提前创建预分区。划分10个区域,预计100数据量,每个分区10* *用户id小于10的都划分在“00”* *用户id大于10且小于20的都划分在“10区域”* 依次类推.....* rowkey的策略:* 分区编号(根据用户id所在划分的分区编号获得,见上面解释)+"-u"+u.getUserId();*/public static void batchPutIndex(Table hTable) {List<Put> list = new ArrayList<Put>();for (int i = 1; i <= 100; i++) {String regionNo="00";regionNo= getRegionNo(i,regionNo);String k=i+"";if(k.length()<3){StringBuffer sb=new StringBuffer();for(int m=0;m<3-k.length();m++){sb.append("0");}k=sb.toString()+k;}byte[] rowkey = Bytes.toBytes(regionNo +"-u"+k);Put put = new Put(rowkey);put.addColumn("index-rw".getBytes(), "info".getBytes(), Bytes.toBytes("zs" + i));list.add(put);}try {hTable.put(list);} catch (IOException e) {e.printStackTrace();}list.clear();log.info("添加数据成功..........................");}public static String getRegionNo(int i,String regionNo){if(i<10){regionNo="00";}else if(i>=10&&i<20){regionNo="10";}else if(i>=20&&i<30){regionNo="20";}else if(i>=30&&i<40){regionNo="30";}else if(i>=50&&i<60){regionNo="40";}else if(i>=50&&i<60){regionNo="50";}else if(i>=60&&i<70){regionNo="60";}else if(i>=70&&i<80){regionNo="70";}else if(i>=80&&i<90){regionNo="80";}else{regionNo="90";}return regionNo;}public static void initUserTable(String tableName,String familyArray[],boolean partionFlag){List<String> list=new ArrayList<String>();try {Admin hadmin = HbaseConnectionUtils.getInstance().getConnection().getAdmin();TableName tm = TableName.valueOf(tableName);if (!hadmin.tableExists(TableName.valueOf(tableName))) {HTableDescriptor hTableDescriptor = new HTableDescriptor(tm);for(String colFa:familyArray){HColumnDescriptor family = new HColumnDescriptor(colFa);family.setMaxVersions(1);hTableDescriptor.addFamily(family);}if(partionFlag){hadmin.createTable(hTableDescriptor, getSplitKeys());}else {hadmin.createTable(hTableDescriptor);//不分区}hadmin.close();}else {log.info("................新建表:"+tableName+"已存在..........................");}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}log.info("................................................create hbase table "+tableName+" successful..........");}public static byte[][] getSplitKeys() {String[] keys = new String[] {"10|", "20|", "30|", "40|", "50|", "60|", "70|", "80|", "90|" };byte[][] splitKeys = new byte[keys.length][];TreeSet<byte[]> rows = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);//升序排序for (int i = 0; i < keys.length; i++) {rows.add(Bytes.toBytes(keys[i]));}Iterator<byte[]> rowKeyIter = rows.iterator();int i=0;while (rowKeyIter.hasNext()) {byte[] tempRow = rowKeyIter.next();rowKeyIter.remove();splitKeys[i] = tempRow;i++;}return splitKeys;}
查看hbase:可以看到用户的id,0-9在“00”区域,10-19在“10”区域,.......
同一分区彼此连续,不同分区彼此连续
登录web页面查看:
6.4.2 创建hb_yonghu表
hb_yonghu表的rowkey策略:
* #创建表,提前创建预分区。划分10个区域
* 用户id末位为0的都划分在“10|”
*用户id末位为1的都划分在“10|-20|”
*用户id末位为2的都划分在“20|-30|”
......
* #设计rowkey
* rowkey=分区号(userId的最后一位+补充0,组成两位数)-注册时间的时间戳(13位)-u+userId(按千万量计算,加前缀u,共6位)
* rowkey的长度=2+1+13+1+6=23位
/*** 创建信息表*/public static void createInfoTable(){String tableName= "hb_yonghu";String familyArray[]={"index-rw"};initUserTable(tableName, familyArray,true);Table table=HbaseConnectionUtils.getInstance().getTable( tableName);batchPut(table);//信息表}/*** #创建表,提前创建预分区。划分10个区域* 用户id末位为0的都划分在“10|”*用户id末位为1的都划分在“10|-20|”*用户id末位为2的都划分在“20|-30|”** #设计rowkey* rowkey=分区号(userId的最后一位+补充0,组成两位数)-注册时间的时间戳(13位)-u+userId(按千万量计算,加前缀u,共9位)* rowkey的长度=2+1+13+1+9=26位* @param hTable*/public static void batchPut(Table hTable) {List<Put> list = new ArrayList<Put>();for (int i = 1; i <= 10000; i++) {String k=i+"";if(k.length()<6){StringBuffer sb=new StringBuffer();for(int m=0;m<6-k.length();m++){sb.append("0");}k=sb.toString()+k;}String n=k.substring(k.length()-1);n=n+"0";//分区号(userId的最后一位+补充0,组成两位数)byte[] rowkey = Bytes.toBytes(n + "-" +System.currentTimeMillis()+"-u"+k);//分区号(userId的最后一位+补充0,组成两位数)Put put = new Put(rowkey);put.addColumn("index-rw".getBytes(), "info".getBytes(), Bytes.toBytes("zs" + i));list.add(put);}try {hTable.put(list);} catch (IOException e) {e.printStackTrace();}list.clear();log.info("添加数据成功..........................");}public static void initUserTable(String tableName,String familyArray[],boolean partionFlag){List<String> list=new ArrayList<String>();try {Admin hadmin = HbaseConnectionUtils.getInstance().getConnection().getAdmin();TableName tm = TableName.valueOf(tableName);if (!hadmin.tableExists(TableName.valueOf(tableName))) {HTableDescriptor hTableDescriptor = new HTableDescriptor(tm);for(String colFa:familyArray){HColumnDescriptor family = new HColumnDescriptor(colFa);family.setMaxVersions(1);hTableDescriptor.addFamily(family);}if(partionFlag){hadmin.createTable(hTableDescriptor, getSplitKeys());}else {hadmin.createTable(hTableDescriptor);//不分区}hadmin.close();}else {log.info("................新建表:"+tableName+"已存在..........................");}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}log.info("................................................create hbase table "+tableName+" successful..........");}public static byte[][] getSplitKeys() {String[] keys = new String[] {"10|", "20|", "30|", "40|", "50|", "60|", "70|", "80|", "90|" };byte[][] splitKeys = new byte[keys.length][];TreeSet<byte[]> rows = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);//升序排序for (int i = 0; i < keys.length; i++) {rows.add(Bytes.toBytes(keys[i]));}Iterator<byte[]> rowKeyIter = rows.iterator();int i=0;while (rowKeyIter.hasNext()) {byte[] tempRow = rowKeyIter.next();rowKeyIter.remove();splitKeys[i] = tempRow;i++;}return splitKeys;}
Hbase shell 查看
web页面:
这篇关于hbase预分区案例-连续和分散两种方法(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!