CRUD operations in HBase in Hadoop - Time & Space Complexity
When working with HBase, it is important to understand how the time to perform create, read, update, and delete actions changes as the data grows.
We want to know how the time needed grows when we add more data or perform more operations.
Analyze the time complexity of the following HBase CRUD operations.
// Put operation to add or update a row
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual"), Bytes.toBytes("value"));
table.put(put);
// Get operation to read a row
Get get = new Get(Bytes.toBytes("row1"));
Result result = table.get(get);
// Delete operation to remove a row
Delete delete = new Delete(Bytes.toBytes("row1"));
table.delete(delete);
This code shows how to add or update a row, read a row, and delete a row in HBase.
Look at what repeats or takes time in these operations.
- Primary operation: Accessing a single row by its key.
- How many times: Each operation works on one row at a time, so it happens once per operation.
As the number of rows in the table grows, each operation still targets one row by key.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 step per operation |
| 100 | About 1 step per operation |
| 1000 | About 1 step per operation |
Pattern observation: The time to perform each operation stays roughly the same no matter how many rows exist.
Time Complexity: O(1)
This means each create, read, update, or delete action takes about the same time regardless of table size.
[X] Wrong: "CRUD operations get slower as the table grows because there are more rows to check."
[OK] Correct: HBase uses keys and indexes to jump directly to the row, so it does not scan all rows each time.
Understanding that HBase CRUD operations run in constant time helps you explain how big data systems handle large datasets efficiently.
"What if we tried to scan the entire table instead of accessing by row key? How would the time complexity change?"
Practice
Solution
Step 1: Understand HBase CRUD operations
HBase uses Get to read data, Put to create or update, Delete to remove, and Scan to read multiple rows.Step 2: Identify the read operation
Get is the specific operation to retrieve data for a single row.Final Answer:
Get -> Option AQuick Check:
Read = Get [OK]
- Confusing Put with Get
- Using Delete to read data
- Thinking Scan is for single row read
Solution
Step 1: Review correct Put syntax in HBase Java API
Put requires a row key as bytes, then addColumn with column family, qualifier, and value as bytes, then call table.put(put).Step 2: Check each option
Put put = new Put(Bytes.toBytes("row1")); put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual"), Bytes.toBytes("value")); table.put(put); matches correct syntax. Get get = new Get(Bytes.toBytes("row1")); table.put(get); wrongly uses Get with put. Delete delete = new Delete(Bytes.toBytes("row1")); table.put(delete); uses Delete with put. Put put = new Put("row1"); put.add("cf", "qual", "value"); table.get(put); uses wrong constructors and methods.Final Answer:
Put put = new Put(Bytes.toBytes("row1")); put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual"), Bytes.toBytes("value")); table.put(put); -> Option CQuick Check:
Put syntax correct = Put put = new Put(Bytes.toBytes("row1")); put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual"), Bytes.toBytes("value")); table.put(put); [OK]
- Using Get or Delete with put()
- Not converting strings to bytes
- Wrong method names like add() instead of addColumn()
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
table.put(put);
Get get = new Get(Bytes.toBytes("row1"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(value));Solution
Step 1: Insert data with Put operation
The code puts value "Alice" in column family "cf" and qualifier "name" for row "row1".Step 2: Retrieve data with Get operation
The Get fetches the row "row1" and extracts the value for "cf:name" which is "Alice".Final Answer:
Alice -> Option DQuick Check:
Put then Get returns inserted value [OK]
- Expecting null if not flushed
- Confusing column family and qualifier
- Forgetting to convert bytes to string
Delete delete = new Delete("row1");
delete.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual"));
table.delete(delete);Solution
Step 1: Check Delete constructor parameter type
The Delete constructor requires a byte array for the row key, not a plain string.Step 2: Verify method usage
addColumn is valid for deleting a specific column. table.delete() is correct method.Final Answer:
Delete constructor argument should be bytes, not string -> Option AQuick Check:
Delete row key must be bytes [OK]
- Passing string directly instead of bytes
- Confusing addColumn with addColumns
- Thinking table.delete() is invalid
Solution
Step 1: Use Put to update the column value
In HBase, Put operation overwrites existing values for the same column, so create a Put with the new "status" value for "user123".Step 2: Save and verify update
Call table.put(put) to save the update, then use table.get(get) to read back the updated value immediately.Final Answer:
Create a Put with new value, call table.put(put), then call table.get(get) to read updated value -> Option BQuick Check:
Put updates value, Get reads updated value [OK]
- Deleting before putting is unnecessary
- Getting before putting returns old value
- Calling get before put misses update
Ready to prove it?
Pass the test and this pattern moves unknown
