1 Packagecom.liveyc.common.utils;2 3 Importjava.util.List;4 5 Importorg.apache.hadoop.hbase.util.Bytes;6 Importcom.liveyc.datarecover.utils.FileToHbase;7 Public classnewtable {8 Public Static voidMain (string[] args)throwsException {9CreateTable ("20171201", "20181201");Ten } One Public Static voidCreateTable (String bdate,string eDate)throwsexception{ AList<string> datelist =filetohbase.datelist (bdate, eDate); - byte[] Regions =New byte[Datelist.size ()] []; - //get region based on time period, create new table the for(String d:datelist) { -Regions[datelist.indexof (d)] =Bytes.tobytes (d); - } -String[] family = {"Fileinfo", "Archiveinfo"}; +String tableName = "View_store"; - hbaseutils.creattable (tablename,family, regions); + } A}
1 /**2 * Create a table3 * @paramtableName Table name4 * @paramFamily Column Family5 * @paramRegions Region6 * @throwsException7 */8 Public Static voidCreattable (String tableName, string[] family,byte[] regions)9 throwsException {TenHbaseadmin admin =Newhbaseadmin (conf); OneHtabledescriptor desc =NewHtabledescriptor (tableName); A for(inti = 0; i < family.length; i++) { -Desc.addfamily (NewHcolumndescriptor (Family[i])); - } the if(Admin.tableexists (tableName)) { -System.out.println ("Table exists!"); - //system.exit (0); -}Else { + if(Regions! =NULL){ - admin.createtable (desc,regions); +SYSTEM.OUT.PRINTLN ("CREATE TABLE success!"); A}Else{ at admin.createtable (DESC); -SYSTEM.OUT.PRINTLN ("CREATE TABLE success!"); - } - - - } in}
Specify the range of region when creating an HBase table with Java code