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();


    }
}
相关推荐
渣波1 小时前
拒绝 SQL 焦虑!手把手带你用 NestJS + Prisma + DTO 写出“防弹”级后端代码
javascript·数据库·后端
大大大大晴天1 小时前
Hudi技术内幕:RecordPayload到RecordMerger
大数据
SelectDB16 小时前
秒级弹性、最高降本 70%:SelectDB Serverless 如何重塑云数仓资源效率
大数据·后端·云原生
WhoAmI16 小时前
MapReduce框架原理解析一:InputFormat
大数据·hadoop
WhoAmI16 小时前
MapReduce框架原理解析三:OutputFormat
大数据·hadoop
WhoAmI17 小时前
MapReduce框架原理解析二:Shuffle
大数据·hadoop
倔强的石头_1 天前
KingbaseES 新版MySQL 兼容版体验:旧版迁移 + 功能实测
数据库
大大大大晴天2 天前
Hudi技术内幕:Key Generation原理与实践
大数据
倔强的石头_4 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab5 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm