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 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.
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:
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> 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”:
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”:
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.





