Data and AI Platforms

Automating HBase Major Compactions with Cron and Kerberos

By
Major compactions in HBase can be scheduled during low-traffic hours to reduce load on RegionServers. This guide shows how to trigger a compaction from the HBase shell using a simple Ruby script and how to wrap it in a Kerberos-aware cron job. It also shows the targeted option: find the region that holds a given row key with locate_region and compact only that region instead of the whole table. It reflects common operational practice in legacy Hadoop clusters where maintenance windows still matter.

Why Schedule Major Compactions?

Major compactions rewrite all store files of an HBase table, improving read performance but putting additional pressure on the cluster. Many administrators run them during off-peak windows. By default HBase runs time-based major compactions on its own (hbase.hregion.majorcompaction, every 7 days with some jitter), which can hit at peak hours. To control the timing, set hbase.hregion.majorcompaction to 0 to disable the automatic runs and trigger them yourself with cron or at.

Ruby Script for HBase Shell

HBase shell executes commands through JRuby, so a simple script triggers the compaction:

# m_compact.rb
major_compact 't1'
exit

Cron-Compatible Shell Wrapper

Below is an example daily_compact script that refreshes a Kerberos ticket and runs the compaction via the HBase shell. Adapt table names, realms and keytabs to your environment.

#!/bin/bash

HBASE_USER=hbase
HBASE_USER_HOME=$(eval echo ~$HBASE_USER)

# Kerberos settings
KEYTAB=/etc/hbase/conf/hbase.keytab
HOST=$(hostname)
REALM=ALO.ALT

LOG=/var/log/daily_compact

# Acquire Kerberos ticket
sudo -u $HBASE_USER kinit -k -t $KEYTAB $HBASE_USER/$HOST@$REALM

# Trigger major compaction
sudo -u $HBASE_USER hbase shell $HBASE_USER_HOME/m_compact.rb 2>&1 | tee -a $LOG

Log Output

All messages are appended to /var/log/daily_compact. Example output may look like:

WARN conf.Configuration: hadoop.native.lib is deprecated
12 row(s) in 0.7800 seconds

Compacting a Single Region for One Row Key

Compacting a whole table every night is not always what you want. Sometimes one region is skewed or full of deleted cells, and you only want to clean up that one. Find the region for the row key and compact just that.

Look at a few rows, or at one specific row key:

scan 'your_table', { LIMIT => 5 }
get 'your_table', "\x00\x01"

Find the region that holds that row. This returns the region name, start key, end key and hosting RegionServer:

locate_region 'your_table', "\x00\x01"

Or list all regions of the table:

list_regions 'your_table'

Then compact only that region, using the region name you got back:

major_compact 'your_table,,1712087434000.abc123'

You can put the same command in m_compact.rb instead of the table name if your cron job should only touch one region. These shell commands replace the old Java API approach with HTable and HBaseAdmin, which no longer works in recent releases.

Operational Notes (Modern Context)

  • Major compactions increase disk and network load; avoid running on overloaded RegionServers.
  • Consider minor compactions and compaction policies before scheduling forced majors.
  • In cloud migrations or HBase-on-EMR clusters, perform compactions during autoscaling-stable hours.
  • In Kerberos environments, get a fresh ticket in every run (as the script does). Tickets expire; keytabs do not, but a keytab stops working when the principal's key is changed, for example after someone re-exports it with a new key version.
  • Region-level compaction is safer than table-wide compaction for large tables. Don’t schedule majors blindly; use them for targeted cleanup.
  • Region names can be found via locate_region, status 'simple' or the HBase Master UI.

Need help with distributed systems, backend engineering or data platforms? See my consulting services.

Alexander Alten

Alexander Alten

Founder of Scalytics, co-creator and PMC member of Apache Wayang, and author of KafScale. I write about Kafka, streaming, data platforms, IoT and AI agent infrastructure in production.