Basic HBase Shell Queries
Basic HBase Shell Queries
As we know, HBase is a non-relational database, so it does not support SQL. Instead, it uses its
query engine to perform operations like fetch, update, and delete.
Before applying any operations, we need to first enter code inside the cluster to execute the shell
commands:
Desktop> hbase shell
If we get the following in our terminal/command line, we are inside the HBase Shell and we are
good to execute shell commands:
hbase(main):001:0>
Let’s create a table product with two column-families, “shoe” and “tshirt” —
hbase(main):001:0> create 'product', 'shoe', 'tshirt'
0 row(s) in 2.8130 seconds
2. LIST
The ‘list’ command is used to list all tables within the shell.
hbase(main):001:0> list
TABLE
product
2 row(s) in 0.0310 seconds
3. PUT
At a time, we can add or update only one column of one rowkey with a PUT query.
# Add or update color column of rowkey = 1
put 'TABLE_NAME','ROW_KEY','COLUMN_FAMILY:COLUMN','VALUE'
Let’s first insert some data and try to see the update
# Insertion Query
put 'product', '1', 'shoe:title', 'Adidas Shoe'
put 'product', '1', 'shoe:description', 'Running Shoes For Men'
put 'product', '1', 'shoe:color', 'black'
put 'product', '1', 'shoe:price', '40'
put 'product', '1', 'shoe:currency', 'USD'
put 'product', '1', 'shoe:size', '28'
put 'product', '2', 'tshirt:title', 'Puma Tshirt'
put 'product', '2', 'tshirt:description', 'Graphic Print Men Round Neck Pink T-Shirt'
put 'product', '2', 'tshirt:color', 'pink'
put 'product', '2', 'tshirt:price', '20'
put 'product', '2', 'tshirt:currency', 'USD'
put 'product', '2', 'tshirt:size', 'M'
Now, let’s update product table with rowkey = 1, column_family = shoe, column = color.
put 'product','1','shoe:color','white'
It will update the color column from black to white. The equivalent SQL query for a relational
database is as follows—
UPDATE table_name
SET column_name = value
WHERE id = primary_key;
4. SCAN
Unlike “get”, “scan” applies to all the rowkey and in turn all columns within the specified table.
# Get all data of a table
scan 'TABLE_NAME'
Example
scan 'product'
5. GET
The “get” keyword is used to fetch data associated with a particular row key.
# Get all data associated with on row key
get 'TABLE_NAME', 'ROW_KEY'
Example:
# Get product data associated with rowKey=1
get 'product', '1'
The above query fetches all the columns associated with one rowkey. The equivalent SQL query
for relational databases is:
SELECT * FROM table_name WHERE id = primary_key
If we want to query one particular column or aggregate of multiple columns, we need to apply
filters.
6. LIMIT
If we want to get first n rows’ data, LIMIT fits in perfectly. This is applicable only for SCAN.
# Fetch all columns for n rows
scan 'TABLE_NAME', {LIMIT => n}
where ‘n’ is any positive integer.
Example:
# Fetch all columns for first 1 row
scan 'product', {LIMIT => 1}
7. DELETE TABLE
We cannot delete the HBase table directly. First, we need to disable the table, then we can delete
it safely.
disable 'table_name'
drop 'table_name'
Example:
disable 'product'
drop 'product'