zoukankan      html  css  js  c++  java
  • 删除hbase的region步骤和代码

    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();
                    }
                }
  • 相关阅读:
    最短路径 一 Dijkstra 模板(O(n^2))
    【转】STL中的set容器的一点总结
    水题 等差数列HDU 5400 Arithmetic Sequence
    贪心+等价转化 HDU 1489
    POJ 3258 最小值最大化 二分搜索
    【转】二分查找算法学习札记
    UVa 714 Copying books 贪心+二分 最大值最小化
    湖南程序设计竞赛赛题总结 XTU 1237 Magic Triangle(计算几何)
    并查集基础 模板题 hdu1232 畅通工程
    数论 最简分数 Farey序列求最简分数+POJ3374
  • 原文地址:https://www.cnblogs.com/jinniezheng/p/6384170.html
Copyright © 2011-2022 走看看