1. 程式人生 > >用java代碼在創建hbase表時指定region的範圍

用java代碼在創建hbase表時指定region的範圍

pos gpo size dmi span throw apache tab data

 1 package com.liveyc.common.utils;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.hadoop.hbase.util.Bytes;
 6 import com.liveyc.datarecover.utils.FileToHbase;
 7 public class NewTable {
 8     public static void main(String[] args) throws Exception {
 9         createTable("20171201","20181201");
10 } 11 public static void createTable(String bDate,String eDate) throws Exception{ 12 List<String> dateList = FileToHbase.dateList(bDate, eDate); 13 byte[][] regions = new byte[dateList.size()][]; 14 //根據時間段獲取region,新建表 15 for(String d : dateList){ 16 regions[dateList.indexOf(d)] = Bytes.toBytes(d);
17 } 18 String[] family = {"Fileinfo","Archiveinfo"}; 19 String tableName = "view_store"; 20 HbaseUtils.creatTable(tableName,family ,regions); 21 } 22 }
 1  /**
 2      * 創建表
 3      * @param tableName 表名
 4      * @param family 列族
 5      * @param regions region
6 * @throws Exception 7 */ 8 public static void creatTable(String tableName, String[] family,byte[][] regions) 9 throws Exception { 10 HBaseAdmin admin = new HBaseAdmin(conf); 11 HTableDescriptor desc = new HTableDescriptor(tableName); 12 for (int i = 0; i < family.length; i++) { 13 desc.addFamily(new HColumnDescriptor(family[i])); 14 } 15 if (admin.tableExists(tableName)) { 16 System.out.println("table Exists!"); 17 //System.exit(0); 18 } else { 19 if(regions != null){ 20 admin.createTable(desc,regions); 21 System.out.println("create table Success!"); 22 }else{ 23 admin.createTable(desc); 24 System.out.println("create table Success!"); 25 } 26 27 28 } 29 }

用java代碼在創建hbase表時指定region的範圍