hbase merge工具

在hbase中,表中可能会存在很多小的region,如果不需要那么多小的region,可以将他们就行合并。

代码的逻辑是将相邻的两个小region进行合并,需要注意以下几点

1、当个region不宜过大,如果两个合并后超过当个region 的最大值,比如7G+7G=14G,超过region配置的最大值10G,这样的话哪怕合并了,也会重新分裂。

2、必须要相邻的两个region,才可以执行命令,不明白原理,不可随意修改代码

复制代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.ArrayList;

public class HbaseMerge {
    public static void main(String[] args) throws Exception {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.property.clientPort", "2181");
        config.set("hbase.zookeeper.quorum", "h1,h2,h3");
        Connection connection = ConnectionFactory.createConnection(config);
        Table table = connection.getTable(TableName.valueOf("hbase:meta"));


        //merge表名
        String tableName = "库名:表名";

        Admin admin = connection.getAdmin();

        Scan scan = new Scan();
        scan.withStartRow(Bytes.toBytes(tableName));
        scan.withStopRow(Bytes.toBytes(tableName + "_"));

        ResultScanner scanner = table.getScanner(scan);

        ArrayList<String> regions = new ArrayList<>();


        for (Result result : scanner) {
            String[] split = result.toString().split("\\./");

            if (split.length > 1) {
                String s = split[0];
                String[] split1 = s.split("\\.");
                String region = split1[split1.length - 1];
                regions.add(region);

            }
        }

        System.out.println("共有region: " + regions.size() + "个-----------------");


        for (int i = 0; i < regions.size() - 1; i += 2) {
            String region1 = regions.get(i);
            String region2 = regions.get(i + 1);
            try {
                admin.mergeRegions(region1.getBytes(), region2.getBytes(), false);
                System.out.println(region1 + " - " + region2 + " merge is ok");
            } catch (Exception e) {

                System.out.println(region1 + " - " + region2 + " merge is fail fail fail");
            }
        }


        //admin.majorCompact(TableName.valueOf(tableName)); 执行major,看是否需要开启

        admin.close();
        connection.close();


    }
}
相关推荐
川石课堂软件测试3 分钟前
MySQL数据库之DBA命令
数据库·网络协议·mysql·http·单元测试·prometheus·dba
ybb_ymm2 小时前
mysql8在linux下的默认规则修改
linux·运维·数据库·mysql
倔强的石头_2 小时前
Navicat Premium 与金仓数据库融合实践:高效管理国产数据库新方案
数据库
程序新视界3 小时前
为什么要尽量将MySQL表字段要设置为NOT NULL?
数据库·mysql·dba
zskj_qcxjqr3 小时前
七彩喜艾灸机器人:当千年中医智慧遇上现代科技
大数据·人工智能·科技·机器人
怪兽20143 小时前
SQL优化手段有哪些
java·数据库·面试
lypzcgf4 小时前
FastbuildAI后端数据库模块注册分析
数据库·ai应用·ai创业·智能体平台·ai应用平台·agent平台·fastbuildai
xyy20255 小时前
Spring事务的传播方式
java·数据库·spring
非凡的世界5 小时前
Thinkphp8 Redis队列与消息队列topthink/think-queue 原创
数据库·redis·bootstrap·thinkphp
yookay zhang5 小时前
DM线程的管理知识学习
数据库