import org.apache.hadoop.conf.
Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class HBaseExample {
private static final TableName TABLE_NAME = TableName.valueOf("my_table");
private static final String COLUMN_FAMILY = "cf";
public static void main(String[] args) throws IOException {
Configuration config = HBaseConfiguration.create();
// Create connection
try (Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin()) {
// 1. Create Table
if (!admin.tableExists(TABLE_NAME)) {
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TABLE_NAME)
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(COLUMN_FAMILY))
.build();
admin.createTable(tableDescriptor);
System.out.println("Table created.");
} else {
System.out.println("Table already exists.");
// 2. Insert Data
try (Table table = connection.getTable(TABLE_NAME)) {
Put put = new Put("row1".getBytes());
put.addColumn(COLUMN_FAMILY.getBytes(), "name".getBytes(), "Alice".getBytes());
put.addColumn(COLUMN_FAMILY.getBytes(), "age".getBytes(), "30".getBytes());
table.put(put);
System.out.println("Data inserted.");
// 3. Retrieve Data
try (Table table = connection.getTable(TABLE_NAME)) {
Get get = new Get("row1".getBytes());
Result result = table.get(get);
byte[] name = result.getValue(COLUMN_FAMILY.getBytes(), "name".getBytes());
byte[] age = result.getValue(COLUMN_FAMILY.getBytes(), "age".getBytes());
System.out.println("Retrieved => Name: " + new String(name) + ", Age: " + new String(age));
// 4. Delete Row
try (Table table = connection.getTable(TABLE_NAME)) {
Delete delete = new Delete("row1".getBytes());
table.delete(delete);
System.out.println("Row deleted.");
} catch (IOException e) {
e.printStackTrace();