通过删除hbase表中的region来达到删除表中数据

公司最近在搞一个hbase删除数据,由于在建表的时候是通过region来对每日的数据进行存储的,所以要求在删除的时候直接通过删除region的来删除数据
(最好的方案是只删除region中的数据,不把region删掉,但是百度了很久没找到只删除region中数据的解决方法,实在遗憾,最终也就通过删除region来删除数据了
这样的弊端是在hbase 中执行scan全表的时候 会报错,找不到某某region,只能通过rowkey来查询别的数据 真的很烦~~~ 以后有时间在来研究这个region吧)
  // 声明静态配置
static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
//conf.set("hbase.zookeeper.quorum", "172.16.71.171");
conf.set("hbase.zookeeper.quorum", "com23.authentication,com22.authentication,com21.authentication");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "172.16.71.171");
conf.set("hbase.master", "172.16.71.171:600000");
conf.set("hbase.client.keyvalue.maxsize", "524288000");// 最大500m
conf.set("hbase.rootdir", "hdfs://h1.hadoop:8020/hbase");// 最大500m
}

 1 @SuppressWarnings("deprecation")
public static boolean deletByRegions(String tableName,String startKey) {
boolean flag = true;
Connection conn = null;
Table meta_table =null;
HTable table = null;
try {
conn = ConnectionFactory.createConnection(conf);
meta_table = conn.getTable(TableName.META_TABLE_NAME);
table = new HTable(conf, Bytes.toBytes(tableName)); //HTabel负责跟记录相关的操作如增删改查
HBaseAdmin admin = new HBaseAdmin(conf); //HBaseAdmin负责跟表相关的操作如create,drop等
HRegionInfo regionInfo = table.getRegionLocation(startKey).getRegionInfo();
String tableNameDataDir = "/data/default/" + tableName;
FileSystem fs = FileSystem.get(HdfsUtils.conn());
Path rootDir = new Path(conf.get("hbase.rootdir") + tableNameDataDir);
HRegionFileSystem.deleteRegionFromFileSystem(conf, fs, rootDir, regionInfo); String regionNameAsString = regionInfo.getRegionNameAsString();
HConnection connection = HConnectionManager.getConnection(conf);
//获取region的regionServerName
HRegionLocation locateRegion = connection.locateRegion(Bytes.toBytes(regionNameAsString));
ServerName serverName_temp = locateRegion.getServerName();
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();
} catch (IOException e) {
flag = false;
e.printStackTrace();
} finally {
if ( table != null) {
try {
table.close();
} catch (IOException e) {
flag = false;
e.printStackTrace();
}
}
}
return flag;
}
上一篇:Andriod post Api与返回值


下一篇:使用FileUtils简化你的文件操作