This is article 37 in the Big Data series. This article demonstrates how to use HBase Client API for table management and CRUD operations through complete Java code.
Complete illustrated version: CSDN Original | Juejin
Maven Dependencies
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
Connection Configuration
All operations require establishing a Connection first. It is recommended to reuse Connection globally (it is thread-safe, while Table is not):
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "h121.wzk.icu,h122.wzk.icu");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
Connection connection = ConnectionFactory.createConnection(configuration);
Create Table
public class Test01 {
public static void main(String[] args) throws IOException {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "h121.wzk.icu,h122.wzk.icu");
Connection connection = ConnectionFactory.createConnection(configuration);
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf("test01"));
descriptor.addFamily(new HColumnDescriptor("base_info"));
admin.createTable(descriptor);
System.out.println("test01 table created successfully");
admin.close();
connection.close();
}
}
Insert Data
public class Test02 {
public static void main(String[] args) throws IOException {
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "h121.wzk.icu,h122.wzk.icu");
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf("test01"));
Put put = new Put(Bytes.toBytes("rk1"));
put.addColumn(
Bytes.toBytes("base_info"),
Bytes.toBytes("name"),
Bytes.toBytes("wuzikang")
);
table.put(put);
table.close();
System.out.println("Data inserted successfully");
connection.close();
}
}
Delete Data
public class Test03 {
public static void main(String[] args) throws IOException {
// ... establish connection (same as above)
Table table = connection.getTable(TableName.valueOf("test01"));
Delete delete = new Delete(Bytes.toBytes("rk1"));
table.delete(delete);
table.close();
System.out.println("Row deleted successfully");
connection.close();
}
}
Get Query (By Column Family)
public class Test04 {
public static void main(String[] args) throws IOException {
HTable table = (HTable) connection.getTable(TableName.valueOf("test01"));
Get get = new Get(Bytes.toBytes("rk1"));
get.addFamily(Bytes.toBytes("base_info"));
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
String cf = Bytes.toString(CellUtil.cloneFamily(cell));
String column = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(cf + ":" + column + " => " + value);
}
table.close();
}
}
Full Table Scan
public class Test05 {
public static void main(String[] args) throws IOException {
HTable table = (HTable) connection.getTable(TableName.valueOf("test01"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
System.out.println("rowKey: " + rowkey);
}
}
table.close();
}
}
Range Scan
public class Test06 {
public static void main(String[] args) throws IOException {
HTable table = (HTable) connection.getTable(TableName.valueOf("test01"));
Scan scan = new Scan();
scan.setStartRow("rk1".getBytes());
scan.setStopRow("rk3".getBytes()); // Left-closed right-open
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
// Process each row
}
table.close();
}
}
setStartRow/setStopRowuse a left-closed right-open interval, sork3itself will not be returned.
Key Considerations
| Point | Description |
|---|---|
Connection Reuse | Thread-safe, use as application-level singleton, avoid frequent creation/destruction |
Table Not Thread-Safe | Call table.close() after each operation |
Bytes.toBytes() | HBase stores all values as byte arrays, decode accordingly when reading |
| Batch Writes | For large batch inserts, use BufferedMutator to reduce RPC calls |
| Range Scan Performance | Always set startRow/stopRow to avoid full table scans |