Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Heibaiying BigData Notes HBaseUtils Read Operations

From Leeroopedia


Knowledge Sources
Domains NoSQL, Big_Data
Last Updated 2026-02-10 10:00 GMT

Overview

Concrete tool for reading data from HBase tables using Get and Scan operations provided by the HBaseUtils utility class.

Description

The HBaseUtils class provides five read methods covering the full spectrum of HBase read patterns:

  • getRow -- Retrieves a complete row by its exact row key, returning a Result object.
  • getCell -- Retrieves a single cell value by row key, column family, and qualifier, returning the value as a String.
  • getScanner (no filter) -- Returns a ResultScanner that iterates over all rows in the table.
  • getScanner (with FilterList) -- Returns a ResultScanner that iterates over rows matching the server-side filters.
  • getScanner (with row key range) -- Returns a ResultScanner that iterates over rows within the specified start and end row key range.

Each method obtains a Table handle from the shared connection, constructs the appropriate Get or Scan object, and executes the read.

Usage

Choose the appropriate method based on the read pattern:

  • Use getRow or getCell for point reads by exact row key.
  • Use getScanner with a FilterList for filtered scans.
  • Use getScanner with start/end keys for bounded range scans.

Code Reference

Source Location

code/Hbase/hbase-java-api-2.x/src/main/java/com/heibaiying/HBaseUtils.java (Lines 126-224)

Signature

// Retrieve a full row by row key
public static Result getRow(String tableName, String rowKey) throws IOException

// Retrieve a single cell value as a String
public static String getCell(String tableName, String rowKey,
                             String columnFamily, String qualifier) throws IOException

// Full table scan (no filter)
public static ResultScanner getScanner(String tableName) throws IOException

// Scan with server-side filters
public static ResultScanner getScanner(String tableName,
                                       FilterList filterList) throws IOException

// Scan with row key range (startRowKey inclusive, endRowKey exclusive)
public static ResultScanner getScanner(String tableName,
                                       String startRowKey,
                                       String endRowKey) throws IOException

Import

import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;

I/O Contract

Inputs (getRow)

Name Type Required Description
tableName String Yes The name of the HBase table to read from.
rowKey String Yes The exact row key to retrieve.

Inputs (getCell)

Name Type Required Description
tableName String Yes The name of the HBase table to read from.
rowKey String Yes The exact row key to retrieve.
columnFamily String Yes The column family containing the desired cell.
qualifier String Yes The column qualifier identifying the specific cell.

Inputs (getScanner with FilterList)

Name Type Required Description
tableName String Yes The name of the HBase table to scan.
filterList FilterList Yes A FilterList containing one or more server-side filters to apply during the scan.

Inputs (getScanner with row key range)

Name Type Required Description
tableName String Yes The name of the HBase table to scan.
startRowKey String Yes The start row key (inclusive) for the scan range.
endRowKey String Yes The end row key (exclusive) for the scan range.

Outputs

Name Type Description
result (getRow) org.apache.hadoop.hbase.client.Result A Result object containing all cells for the requested row. Returns an empty Result if the row does not exist.
value (getCell) String The cell value as a String, or null if the cell does not exist.
scanner (getScanner variants) org.apache.hadoop.hbase.client.ResultScanner An iterable scanner that lazily retrieves matching rows. Must be closed after use.

Usage Examples

// 1. Get a full row
Result result = HBaseUtils.getRow("users", "row1");
for (Cell cell : result.rawCells()) {
    System.out.println(
        Bytes.toString(CellUtil.cloneFamily(cell)) + ":" +
        Bytes.toString(CellUtil.cloneQualifier(cell)) + " = " +
        Bytes.toString(CellUtil.cloneValue(cell))
    );
}

// 2. Get a single cell value
String name = HBaseUtils.getCell("users", "row1", "info", "name");
System.out.println("Name: " + name);

// 3. Full table scan
ResultScanner scanner = HBaseUtils.getScanner("users");
for (Result r : scanner) {
    System.out.println("Row: " + Bytes.toString(r.getRow()));
}
scanner.close();

// 4. Scan with server-side filters
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(new PrefixFilter(Bytes.toBytes("row")));
ResultScanner filtered = HBaseUtils.getScanner("users", filterList);
for (Result r : filtered) {
    System.out.println("Filtered row: " + Bytes.toString(r.getRow()));
}
filtered.close();

// 5. Scan with row key range
ResultScanner ranged = HBaseUtils.getScanner("users", "row1", "row5");
for (Result r : ranged) {
    System.out.println("Range row: " + Bytes.toString(r.getRow()));
}
ranged.close();

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment