zoukankan      html  css  js  c++  java
  • 通过删除hbase表中的region来达到删除表中数据


    公司最近在搞一个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 }
  • 相关阅读:
    vue(5)联动+tab页
    vue(4)hello world
    vue(3)IDE
    vue(2)创建项目
    vue(1)安装
    线程池(6)简单创建线程3种实现
    logback日志简记
    [省选前集训2021] 模拟赛2
    无标号无根树计数
    [省选前集训2021] 模拟赛1
  • 原文地址:https://www.cnblogs.com/xuyou551/p/7986832.html
Copyright © 2011-2022 走看看