Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 36 ยท Shell and Basic Operations

Shell: Namespaces, TTL and Snapshots

Organize HBase tables with namespaces, expire old data with TTL, and take, clone, restore and export snapshots with the shell to back up and copy tables safely.

Three features turn a collection of tables into something you can actually operate: namespaces keep tables organized and let you set limits, TTL removes old data without a cleanup job, and snapshots give you cheap, consistent backups and copies. All three are driven from the shell, and all shell listings below are read-only (the site has no cluster). Java stand-ins reproduce the logic.

Namespaces

A namespace is a logical group of tables, like a schema in a relational database. Table names are written namespace:table; a table without a prefix lives in the default namespace. The hbase namespace holds system tables such as hbase:meta.

Terminal
create_namespace 'shop'
create 'shop:users', 'info'
create 'shop:orders', 'd'
list_namespace
list_namespace_tables 'shop'
describe_namespace 'shop'

You cannot drop a namespace that still has tables; drop or move them first:

Terminal
disable 'shop:users'
drop 'shop:users'
drop_namespace 'shop'

Why bother? Namespaces support grouping for permissions (grant a team access to one namespace), quotas (limit the number of tables and regions in a namespace, or the space and request rates) and region server groups in some deployments. They also keep test and production tables apart on a shared cluster.

Terminal
create_namespace 'analytics', {'hbase.namespace.quota.maxtables' => '50', 'hbase.namespace.quota.maxregions' => '500'}

The quota properties limit the number of tables and regions the namespace may create (the quota feature must be enabled on the cluster).

TTL: data that expires

A time to live is a family setting in seconds. Cells older than the TTL are invisible to reads at once and are physically removed at the next compaction.

Terminal
create 'sessions', {NAME => 's', TTL => 86400}          # keep one day
alter 'sessions', {NAME => 's', TTL => 3600}            # change to one hour

Remember these rules:

  • TTL is measured from the cell timestamp, not from when you wrote it. Writing a cell with an old explicit timestamp can expire it instantly.
  • TTL applies per family, to every cell in it.
  • MIN_VERSIONS keeps that many newest versions even after the TTL has passed (useful for "last known value").
  • Cells expire individually, so a row disappears only when all its cells have.
  • Whole HFiles whose newest cell is expired can be dropped without rewriting; this makes TTL very cheap for time-series data. This works best when files are time-ordered, one reason write-time order matters.

A cell-level TTL also exists: a Put can carry its own TTL attribute (in milliseconds) through the Java API, used rarely.

The stand-in applies family TTL and MIN_VERSIONS with an explicit clock.

Java Stand-in: TTL with MIN_VERSIONS

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Snapshots

A snapshot is a named, consistent picture of a table's files at one moment. Taking one does not copy data: it records which HFiles belong to the table (metadata only), so it is nearly instant and costs almost no space at first. HFiles are immutable, and HBase keeps the files that a snapshot references alive (even after compactions would normally delete them) until the snapshot is deleted.

Terminal
snapshot 'shop:users', 'users-2025-01-15'
list_snapshots
list_snapshots 'users-.*'
delete_snapshot 'users-2025-01-15'

Uses:

  • Backup before a risky change (an alter, a bulk load, an application release).
  • Clone a copy for tests: clone_snapshot 'users-2025-01-15', 'shop:users_copy' creates a new, independent table backed by the same files (they are copied lazily as the clone changes).
  • Restore a table to an earlier state: disable, then restore_snapshot, then enable.
  • Export to another cluster or to cloud storage, using the MapReduce-based tool run from the OS shell (not the HBase shell):
Terminal
hbase snapshot export -snapshot users-2025-01-15 -copy-to hdfs://backup-cluster:8020/hbase -mappers 8

Restoring a snapshot discards every write made after it. Test a restore before you need it.

Terminal
disable 'shop:users'
restore_snapshot 'users-2025-01-15'
enable 'shop:users'

Snapshots capture flushed data by default (the snapshot first flushes memstores). They are table-level; there is no consistent snapshot across several tables.

Modeling the idea of a snapshot

The stand-in shows why snapshots are cheap: files are immutable, so a snapshot is a list of file names. New writes create new files and compaction replaces old ones, but the snapshot keeps its own list alive.

Java Stand-in: a snapshot is a list of immutable files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Operational advice

  • Name snapshots with the table and a timestamp, and delete old ones: they pin HFiles, so disk usage grows as the table diverges from the snapshot.
  • Practice clone_snapshot into a scratch namespace before trusting a backup plan.
  • Combine snapshots with ExportSnapshot for off-cluster copies; a snapshot on the same HDFS does not protect against losing the HDFS cluster.
  • For quotas and permissions, set them at namespace level instead of per table.

The second stand-in builds snapshot names and a retention rule (keep the newest N), a common small script.

Java Stand-in: snapshot names and retention

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How do you refer to a table called users in the namespace shop?

    HBase uses a colon between namespace and table.
  2. 2. From what is a cell's TTL measured?

    Cells whose timestamp is older than now minus the TTL are treated as expired, so old explicit timestamps can expire immediately.
  3. 3. Why is taking a snapshot fast?

    HFiles are immutable, so a snapshot is a manifest of file names; the files are kept until the snapshot is deleted.
  4. 4. What must you do before restore_snapshot on an existing table?

    Restore replaces the table's contents, so it requires the table to be disabled.
  5. 5. What does a snapshot NOT protect you from?

    Snapshots on the same HDFS share its fate. Export them to another cluster for real disaster recovery.

Summary

  • Namespaces group tables (ns:table), enable permissions and quotas, and must be empty before drop_namespace.
  • TTL expires cells by timestamp per family; MIN_VERSIONS keeps a floor of recent versions.
  • Snapshots are metadata-only, consistent per table, and support clone_snapshot, restore_snapshot and export.
  • Restoring discards later writes; exported copies protect against cluster loss.
  • Delete old snapshots because they pin HFiles and consume space.

Next: Java Client: Connections and Tables