Apache HBase 作為 Hadoop 生態系中的分布式、可擴展的 NoSQL 資料庫,廣泛應用於大數據存儲場景。本文以 HBase 0.90.2 版本為例,詳細介紹如何在 Windows 系統上,配合 Eclipse IDE 搭建 Java 開發環境,並實現表的創建、刪除、數據增刪改查等核心客戶端操作。
在開始編碼前,請確保已安裝以下組件:
HBaseClient。Properties → Java Build Path → Libraries → Add External JARs。hbase-0.90.2.jar、hbase-0.90.2-tests.jar 以及 lib/ 目錄下所有 JAR 包加入 Classpath。將集群的 hbase-site.xml 放入專案 Classpath(如 src/ 目錄)。配置範例如下:
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://hostname:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>zk1.example.com,zk2.example.com,zk3.example.com</value>
</property>
<property>
<name>hbase.defaults.for.version</name>
<value>0.90.2</value>
</property>
</configuration>
以下程式碼片段展示了常用操作,實際專案中建議封裝為工具類,並妥善處理資源關閉與異常。
private static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
}
public void createTable(String tableName, String[] columnFamilies) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)) {
System.out.println("表已存在:" + tableName);
return;
}
HTableDescriptor desc = new HTableDescriptor(tableName);
for (String cf : columnFamilies) {
desc.addFamily(new HColumnDescriptor(cf));
}
admin.createTable(desc);
System.out.println("表創建成功:" + tableName);
}
public void deleteTable(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("表刪除成功:" + tableName);
}
}
public void putRow(String tableName, String rowKey, String[] columnFamilies) throws IOException {
try (HTable table = new HTable(conf, tableName)) {
Put put = new Put(Bytes.toBytes(rowKey));
for (String cf : columnFamilies) {
put.add(Bytes.toBytes(cf), Bytes.toBytes("qualifier"), Bytes.toBytes("value"));
}
table.put(put);
}
}
public void deleteRow(String tableName, String rowKey) throws IOException {
try (HTable table = new HTable(conf, tableName)) {
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
System.out.println("行刪除成功:" + rowKey);
}
}
public void getRow(String tableName, String rowKey) throws IOException {
try (HTable table = new HTable(conf, tableName)) {
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
for (KeyValue kv : result.raw()) {
System.out.printf("Row: %s, Family: %s, Qualifier: %s, Timestamp: %d, Value: %s%n",
Bytes.toString(kv.getRow()),
Bytes.toString(kv.getFamily()),
Bytes.toString(kv.getQualifier()),
kv.getTimestamp(),
Bytes.toString(kv.getValue()));
}
}
}
public void scanTable(String tableName) throws IOException {
try (HTable table = new HTable(conf, tableName)) {
Scan scan = new Scan();
try (ResultScanner scanner = table.getScanner(scan)) {
for (Result result : scanner) {
for (KeyValue kv : result.raw()) {
System.out.printf("Row: %s, Family: %s, Qualifier: %s, Value: %s%n",
Bytes.toString(kv.getRow()),
Bytes.toString(kv.getFamily()),
Bytes.toString(kv.getQualifier()),
Bytes.toString(kv.getValue()));
}
}
}
}
}
ConnectionFactory)管理 Connection 與 Table 實例,避免頻繁創建開銷。Table、Admin、Connection,防止資源洩漏。BufferedMutator 或批量 Put 列表提升吞吐。掌握上述基礎操作後,即可在 Windows + Eclipse 環境下高效開發 HBase Java 客戶端應用。進階主題如 Coprocessor、Filter 進階過濾、效能調優等可參考官方文檔深入學習。