Hbase 手动执行MajorCompation
阅读原文时间:2023年07月08日阅读:1

Major Compaction 的作用:

1.将一个Region下的所有StoreFile合并成一个StoreFile文件

2.对于删除、过期、多余版本的数据进行清除

由于MajorCompation期间对集群的磁盘IO占和带宽占用较大,所以线上系统一般会禁用此功能,在集群负载较低时手动执行

方式1:

(Hbase shell)major_compact  'tablename'

方式2:

通过代码合并

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;

/**
* majorCompact
*/
public class MajorCompact {
public static void main(String[] args) throws IOException {
String[] tableList = {
"tablename1",
"tablename2"
};

Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "zk1,zk2,zk3");

Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();

for (String table : tableList) {
TableName table_name = TableName.valueOf(table);
if (admin.tableExists(table_name)) {
admin.majorCompact(table_name);
System.out.println("[------]major compact " + table_name.getNameAsString());
}
}
admin.close();
connection.close();
}
}