公司最近在搞一個hbase刪除數據,由於在建表的時候是通過region來對每日的數據進行存儲的,所以要求在刪除的時候直接通過刪除region的來刪除數據
(最好的方案是只刪除region中的數據,不把region刪掉,但是百度了很久沒找到只刪除region中數據的解決方法,實在遺憾,最終也就通過刪除region來刪除數據了
這樣的弊端是在hbase 中執行scan全表的時候 會報錯,找不到某某region,只能通過rowkey來查詢別的數據 真的很煩~~~ 以后有時間在來研究這個region吧)
1 // 聲明靜態配置
2 static Configuration conf = null;
3 static {
4 conf = HBaseConfiguration.create();
5 //conf.set("hbase.zookeeper.quorum", "172.16.71.171");
6 conf.set("hbase.zookeeper.quorum", "com23.authentication,com22.authentication,com21.authentication");
7 conf.set("hbase.zookeeper.property.clientPort", "2181");
8 conf.set("hbase.zookeeper.quorum", "172.16.71.171");
9 conf.set("hbase.master", "172.16.71.171:600000");
10 conf.set("hbase.client.keyvalue.maxsize", "524288000");// 最大500m
11 conf.set("hbase.rootdir", "hdfs://h1.hadoop:8020/hbase");// 最大500m
12 }
1 @SuppressWarnings("deprecation")
2 public static boolean deletByRegions(String tableName,String startKey) {
3 boolean flag = true;
4 Connection conn = null;
5 Table meta_table =null;
6 HTable table = null;
7 try {
8 conn = ConnectionFactory.createConnection(conf);
9 meta_table = conn.getTable(TableName.META_TABLE_NAME);
10 table = new HTable(conf, Bytes.toBytes(tableName)); //HTabel負責跟記錄相關的操作如增刪改查
11 HBaseAdmin admin = new HBaseAdmin(conf); //HBaseAdmin負責跟表相關的操作如create,drop等
12 HRegionInfo regionInfo = table.getRegionLocation(startKey).getRegionInfo();
13 String tableNameDataDir = "/data/default/" + tableName;
14 FileSystem fs = FileSystem.get(HdfsUtils.conn());
15 Path rootDir = new Path(conf.get("hbase.rootdir") + tableNameDataDir);
16 HRegionFileSystem.deleteRegionFromFileSystem(conf, fs, rootDir, regionInfo);
17
18 String regionNameAsString = regionInfo.getRegionNameAsString();
19 HConnection connection = HConnectionManager.getConnection(conf);
20 //獲取region的regionServerName
21 HRegionLocation locateRegion = connection.locateRegion(Bytes.toBytes(regionNameAsString));
22 ServerName serverName_temp = locateRegion.getServerName();
23 admin.closeRegion(serverName_temp,regionInfo);
24 List list = new ArrayList();
25 Delete d1 = new Delete(regionNameAsString.getBytes());
26 list.add(d1);
27 meta_table.delete(list);
28 meta_table.close();
29 } catch (IOException e) {
30 flag = false;
31 e.printStackTrace();
32 } finally {
33 if ( table != null) {
34 try {
35 table.close();
36 } catch (IOException e) {
37 flag = false;
38 e.printStackTrace();
39 }
40 }
41 }
42 return flag;
43 }