0
0
Hadoopdata~7 mins

CRUD operations in HBase in Hadoop

Choose your learning style9 modes available
Introduction

CRUD operations let you create, read, update, and delete data in HBase. This helps manage big data easily.

When you want to add new records to a large database.
When you need to find specific data quickly.
When you want to change existing information in your database.
When you want to remove outdated or wrong data.
When working with big data that needs fast access and updates.
Syntax
Hadoop
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qualifier"), Bytes.toBytes("value"));
table.put(put);

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

Delete delete = new Delete(Bytes.toBytes("row1"));
table.delete(delete);

Use Put to add or update data.

Use Get to read data.

Use Delete to remove data.

Examples
Adds a new row with a column 'name' set to 'Alice'.
Hadoop
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
table.put(put);
Reads the value of 'name' from row 'row1' and prints it.
Hadoop
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));
Updates the 'name' column in 'row1' to 'Bob'.
Hadoop
Put put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("Bob"));
table.put(put);
Deletes the entire row 'row1'.
Hadoop
Delete delete = new Delete(Bytes.toBytes("row1"));
table.delete(delete);
Sample Program

This program connects to HBase, adds a row, reads it, updates it, reads again, deletes it, and checks deletion.

Hadoop
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseCRUDExample {
    public static void main(String[] args) throws Exception {
        org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
        try (Connection connection = ConnectionFactory.createConnection(config)) {
            TableName tableName = TableName.valueOf("my_table");
            try (Table table = connection.getTable(tableName)) {
                // Create (Put)
                Put put = new Put(Bytes.toBytes("row1"));
                put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
                table.put(put);

                // Read (Get)
                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("Read value: " + Bytes.toString(value));

                // Update (Put)
                Put putUpdate = new Put(Bytes.toBytes("row1"));
                putUpdate.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("Bob"));
                table.put(putUpdate);

                // Read updated value
                Result updatedResult = table.get(get);
                byte[] updatedValue = updatedResult.getValue(Bytes.toBytes("cf"), Bytes.toBytes("name"));
                System.out.println("Updated value: " + Bytes.toString(updatedValue));

                // Delete
                Delete delete = new Delete(Bytes.toBytes("row1"));
                table.delete(delete);

                // Try to read deleted row
                Result deletedResult = table.get(get);
                if (deletedResult.isEmpty()) {
                    System.out.println("Row deleted successfully.");
                } else {
                    System.out.println("Row still exists.");
                }
            }
        }
    }
}
OutputSuccess
Important Notes

Always close HBase connections and tables to avoid resource leaks.

Put operation can add new data or update existing data.

Delete removes entire rows or specific columns if specified.

Summary

CRUD means Create, Read, Update, Delete data in HBase.

Use Put for create and update, Get for read, Delete for removal.

These operations help manage big data efficiently.