1、初始化hbase連接
1 Configuration conf = HbaseCommonsUnit.initConfiguration(); 2 Connection conn = ConnectionFactory.createConnection(conf); 3 Table meta_table = conn.getTable(TableName.META_TABLE_NAME); 4 HTable table = new HTable(conf, Bytes.toBytes(tablename)); //HTabel負責跟記錄相關的操作如增刪改查 5 HBaseAdmin admin = new HBaseAdmin(conf); //HBaseAdmin負責跟表相關的操作如create,drop等
2、刪除Hbase表中Region中StartKey為2014000的Region及Meta中Region的元數據
A、先關閉regionserver中的Region
B、刪除Region在HDFS上的文件
C、刪除Meta表中Region數據記錄信息
String startKey = "2014000"; HRegionInfo regionInfo = table.getRegionLocation(startKey).getRegionInfo(); String tableNameDataDir = "/data/default/" + tablename;
FileSystem fs = FileSystem.get(conf);
Path rootDir = new Path(conf.get("hbase.rootdir") + tableNameDataDir);
HRegionFileSystem.deleteRegionFromFileSystem(conf, fs, rootDir, regionInfo);
String regionNameAsString = regionInfo.getRegionNameAsString();
ServerName serverName_temp = StrategyImplementUnit.getServerNameByRegionName(regionNameAsString);//獲取region的regionServerName
admin.closeRegion(serverName_temp,regionInfo);
List list = new ArrayList();
Delete d1 = new Delete(regionNameAsString.getBytes());
list.add(d1); meta_table.delete(list); meta_table.close();
3、創建一個新的region,其中Startkey為NULL
1 String new_end_key = "2013222"; 2 TableName tableName = TableName.valueOf(tablename); 3 HRegionInfo regionInfo = new HRegionInfo(tableName, Bytes.toBytes(""), Bytes.toBytes(new_end_key)); 4 HRegionFileSystem.createRegionOnFileSystem(conf, fs, rootDir, regionInfo); 5 6 Put put = MetaTableAccessor.makePutFromRegionInfo(regionInfo); 7 meta_table.put(put); 8 meta_table.close();
4、關閉hbase的連接
if ( table != null) { try { table.close(); } catch (IOException e) { e.printStackTrace(); } }