用java代码在创建hbase表时指定region的范围

 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     }

 

posted @ 2017-12-12 16:15  再也伤不起  阅读(2132)  评论(0编辑  收藏  举报