HBase Query Example: put(), get() & scan() Commands

โšก Smart Summary

HBase put, get, and scan are the core commands for writing and reading data, letting you store a cell value by row and column, fetch a single row, or browse many rows from the shell or Java API.

  • โœ๏ธ Write with put: The put command stores a value at a table, row, column, and optional timestamp.
  • ๐Ÿ” Read with get: The get command returns a single row or cell, with TIMERANGE, VERSIONS, and FILTERS options.
  • ๐Ÿ“œ Browse with scan: The scan command lists many rows and their column values across a table.
  • โ˜• Java API: The same operations run in Java using Put, Get, Scan, and the Bytes helper class.
  • ๐Ÿงฌ Coordinates: Every value is addressed by row key, column family, and column qualifier.
  • ๐Ÿค– AI assist: AI assistants draft put, get, and scan code and suggest efficient row-key designs.

HBase put(), get() and scan() query commands in shell and Java

Write Data to HBase Table: Shell

These examples assume HBase is installed and running, the HBase shell is open, and a table named guru99 already exists with the column families education and projects.

The put command is used to store data into a table.

Syntax:  put <'tablename'>,<'rowname'>,<'columnvalue'>,<'value'>

This command is used for the following things:

  • It will put a cell ‘value’ at a defined or specified table or row or column.
  • It will optionally coordinate a time stamp.

For example, here we are placing values into table “guru99” under row r1 and column c1:

hbase> put 'guru99', 'r1', 'c1', 'value', 10

We have placed three values, 10, 15, and 30, in table “guru99”, as shown in the screenshot below.

HBase shell put command inserting the values 10, 15 and 30 into table guru99

Suppose the table “guru99” has a table reference, such as g. You can also run the command on the table reference, like this:

hbase> g.put 'guru99', 'r1', 'c1', 'value', 10

The output appears as shown in the screenshot above after placing values into “guru99”.

Read Data from HBase Table: Shell

In this section, we check the following in the HBase shell:

  • Values that are inserted into HBase table “guru99”.
  • Column names with the values present in HBase table guru99.

The scan output below lists every value inserted into “guru99”, together with its row and column names:

HBase shell scan command output listing rows and column values in table guru99

From the above screenshot, we can infer the following:

  • If we run the “scan” command in the HBase shell, it will display the inserted values in “guru99” as follows.
  • In the HBase shell, it will display the values inserted by our code, with the column and row names.
  • Here we can see the column names inserted are “education” and “projects”.
  • The values inserted are “BigData” and “HBase Tutorials” into the mentioned columns.

You can also use the get command to read data from a table.

Syntax: get <'tablename'>, <'rowname'>, {< Additional parameters>}

Here the additional parameters include TIMERANGE, TIMESTAMP, VERSIONS, and FILTERS. Using this command, you get a row or the cell contents present in the table. You can add additional parameters, such as TIMESTAMP, TIMERANGE, VERSIONS, or FILTERS, to fetch a particular row or cell content. The table below summarizes these parameters:

Parameter Purpose
TIMERANGE Returns cells whose timestamp falls within a start and end time.
TIMESTAMP Returns only the cell version stored at an exact timestamp.
VERSIONS Sets how many versions of a cell to return (the default is 1).
FILTERS Applies a filter to restrict which rows or columns are returned.
COLUMN Limits the result to specific column families or qualifiers.

Here are some worked examples of the get command:

hbase> get 'guru99', 'r1', {COLUMN => 'c1'}

For table “guru99”, row r1 and column c1 values will display using this command, as shown in the screenshot below.

HBase shell get command returning a single row r1 from table guru99

hbase> get 'guru99', 'r1'

For table “guru99”, row r1 values will be displayed using this command.

hbase> get 'guru99', 'r1', {TIMERANGE => [ts1, ts2]}

For table “guru99”, row r1 values in the time range ts1 to ts2 will be displayed using this command.

hbase> get 'guru99', 'r1', {COLUMN => ['c1', 'c2', 'c3']}

For table “guru99”, row r1 and column families c1, c2, and c3 values will be displayed using this command.

Write Data to HBase Table: JAVA API

In this step, we are going to write data into HBase table “guru99”.

First, we have to write code to insert and retrieve values from HBase, using the HBaseLoading.java program. For creating and inserting values into a table at the column level, you code as shown below.

The screenshot below shows the Java code that builds the HBase configuration and inserts values into “guru99”:

Java code creating an HBase configuration and inserting values with the Put API

From the above screenshot:

  • When we create the HBase configuration, it points to whatever configurations we set in the hbase-site.xml and hbase-default.xml files during HBase installation.
  • Creation of table “guru99” using the HTable method.
  • Adding row1 to table “guru99”.
  • Specifying the column names “education” and “projects” and inserting values into the column names in the respective row1. The values inserted here are BigData and “HBaseTutorials”.

Read Data from HBase Table: Java API

Whatever values we placed in the HBase table in the section above, here we are going to fetch and display those values.

The console output below shows the data being read back from HBase table “guru99”:

Java console output reading education and projects column values from HBase table guru99

For retrieving results stored in “guru99”:

  • Here we are going to fetch the values that are stored in the column families, that is, “education” and “projects”.
  • Using the “get” command, we are going to fetch the stored values in the HBase table.
  • Scanning results using the “scan” command. The values that are stored in row1 will display on the console.

Once the code is written, you run the Java application like this:

Right-click on HBaseLoading.java -> Run As -> Java Application.

After running “HBaseLoading.java”, the values are inserted into “guru99” in each column in HBase, and in the same program it can retrieve values as well.

Here is the complete code:

import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseLoading
{
public static void main(String[] args) throws IOException
{
/* When you create a HBaseConfiguration, it reads in whatever you've set into your hbase-site.xml and in hbase-default.xml, as long as these can be found on the CLASSPATH*/

org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();

/*This instantiates an HTable object that connects you to the "test" table*/

HTable table = new HTable(config, "guru99");

/* To add to a row, use Put. A Put constructor takes the name of the row you want to insert into as a byte array.*/

Put p = new Put(Bytes.toBytes("row1"));

/*To set the value you'd like to update in the row 'row1', specify the column family, column qualifier, and value of the table cell you'd like to update. The column family must already exist in your table schema. The qualifier can be anything.*/

p.add(Bytes.toBytes("education"), Bytes.toBytes("col1"),Bytes.toBytes("BigData"));
p.add(Bytes.toBytes("projects"),Bytes.toBytes("col2"),Bytes.toBytes("HBaseTutorials"));

// Once you've adorned your Put instance with all the updates you want to make, to commit it do the following

table.put(p);

// Now, to retrieve the data we just wrote.

Get g = new Get(Bytes.toBytes("row1"));
Result r = table.get(g);

byte [] value = r.getValue(Bytes.toBytes("education"),Bytes.toBytes("col1"));
byte [] value1 = r.getValue(Bytes.toBytes("projects"),Bytes.toBytes("col2"));
String valueStr = Bytes.toString(value);

String valueStr1 = Bytes.toString(value1);
System.out.println("GET: " +"education: "+ valueStr+"projects: "+valueStr1);

Scan s = new Scan();

s.addColumn(Bytes.toBytes("education"), Bytes.toBytes("col1"));
s.addColumn(Bytes.toBytes("projects"), Bytes.toBytes("col2"));
ResultScanner scanner = table.getScanner(s);
try
{
for (Result rr = scanner.next(); rr != null; rr = scanner.next())
{
System.out.println("Found row : " + rr);
}
} finally
{
// Make sure you close your scanners when you are done!

scanner.close();
}
}
}

Note on API versions: The example above uses the HTable class and its add() method, which reflect the older HBase client API. Since HBase 1.0, HTable is deprecated. Modern code obtains a Table through ConnectionFactory.createConnection(…).getTable(…) and builds a Put with addColumn() instead of add(). The logic of put, get, and scan stays the same.

FAQs

The get command retrieves one row by its row key and returns quickly. The scan command reads many rows sequentially across a table or a key range, optionally filtered by columns or time. Use get for single lookups and scan for browsing or exporting multiple rows.

A column family is a named group of columns stored together and defined when you create the table. A column qualifier is the individual column name inside that family, such as education:col1. Families must exist in the schema; qualifiers can be added freely at write time.

Use the delete command to remove one cell, for example delete ‘guru99’, ‘r1’, ‘c1’. Use deleteall to remove an entire row: deleteall ‘guru99’, ‘r1’. To empty a table completely, disable it, then run truncate ‘guru99’, which drops and recreates it.

HBase keeps multiple timestamped versions of each cell. The number retained is set per column family by the VERSIONS attribute, which defaults to 1. Add {VERSIONS => 3} to a get or scan to return several versions, or a TIMESTAMP to fetch one exact version.

Run list to see every table in HBase. Run count ‘guru99’ to count the rows in a table, optionally with an INTERVAL for progress. Use describe ‘guru99’ to view column families, and status ‘simple’ to check cluster health.

HBase has no native SQL, but Apache Phoenix adds a SQL layer that compiles queries into native put, get, and scan operations. It supports familiar SELECT, UPSERT, and JOIN syntax, making HBase easier to query for teams already fluent in SQL.

AI coding assistants and large language models can draft HBase shell commands and Java client code from a plain-language prompt, suggest row-key designs, and explain scan filters. Machine learning also helps tune performance by predicting hot regions, though an engineer should verify every generated query.

Yes. GitHub Copilot can generate HBase put, get, and scan snippets in Java or the shell from a short comment, including Bytes.toBytes calls and Scan filters. Review the output for the correct table name, column families, and the current Table API before running it.

Summarize this post with: