一、添加依賴
首先我們在maven項目中添加下面兩個依賴
<!-- hbase依賴 -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.4.10</version>
</dependency>
<!-- 單元測試依賴 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
二、連接HBase數據庫
1. 編寫連接數據庫的實現
/**
* @title HBaseConnection
* @date 2019/12/8 20:46
* @description 連接HBase數據庫
*/
public class HBaseConnection {
private static final HBaseConnection INSTANCE = new HBaseConnection();
private static Configuration configuration;
private static Connection connection;
private HBaseConnection(){
try {
if(configuration == null){
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "localhost:2181");
}
}catch (Exception e){
e.printStackTrace();
}
}
private Connection getConnection(){
if (connection == null || connection.isClosed()){
try{
connection = ConnectionFactory.createConnection(configuration);
}catch (Exception e){
e.printStackTrace();
}
}
return connection;
}
public static Connection getHBaseConnection(){
return INSTANCE.getConnection();
}
public static Table getTable(String tableName) throws IOException {
return INSTANCE.getConnection().getTable(TableName.valueOf(tableName));
}
public static void closeConnection(){
if(connection != null){
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. 編寫數據庫連接測試類
/**
* @title HBaseTest
* @date 2019/12/8 21:35
* @description //todo
*/
public class HBaseConnectionTest {
@Test
public void getConnectionTest(){
Connection hBaseConnection = HBaseConnection.getHBaseConnection();
System.out.println(hBaseConnection.isClosed());
HBaseConnection.closeConnection();
System.out.println(hBaseConnection.isClosed());
}
@Test
public void getTableTest(){
try{
Table table = HBaseConnection.getTable("US_POPULATION");
System.out.println(table.getName().getNameAsString());
table.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
三、使用Java實現HBase常見操作
1. 編寫操作數據庫的實現
/**
* @title HBaseUtil
* @date 2019/12/9 11:14
* @description 操作HBase工具類
*/
public class HBaseUtil {
/**
* 創建HBase表
* @param tableName 表名
* @param cfs 列族的數據
* @return 是否創建成功
*/
public static boolean createTable(String tableName, String[] cfs){
try (HBaseAdmin admin = (HBaseAdmin)HBaseConnection.getHBaseConnection().getAdmin()){
if(admin.tableExists(tableName)){
return false;
}
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
Arrays.stream(cfs).forEach(cf -> {
HColumnDescriptor columnDescriptor = new HColumnDescriptor(cf);
columnDescriptor.setMaxVersions(1);
tableDescriptor.addFamily(columnDescriptor);
});
admin.createTable(tableDescriptor);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
/**
* 刪除HBase表
* @param tableName 表名
* @return
*/
public static boolean deleteTable(String tableName){
try (HBaseAdmin admin = (HBaseAdmin)HBaseConnection.getHBaseConnection().getAdmin()){
admin.disableTable(tableName);
admin.deleteTable(tableName);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
/**
* HBase表中插入一條數據
* @param tableName 表名
* @param roeKey 唯一標識
* @param cfName 列族名
* @param qualifier 列標識
* @param data 數據
* @return 是否插入成功
*/
public static boolean putRow(String tableName, String roeKey, String cfName, String qualifier, String data){
try (Table table = HBaseConnection.getTable(tableName)){
Put put = new Put(Bytes.toBytes(roeKey));
put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));
table.put(put);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
/**
* HBase表中批量插入數據
* @param tableName
* @param puts
* @return
*/
public static boolean putRows(String tableName, List<Put> puts){
try (Table table = HBaseConnection.getTable(tableName)){
table.put(puts);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
/**
* 獲取單條數據
* @param tableName 表名
* @param rowKey 唯一表標識
* @return 查詢結果
*/
public static Result getRow(String tableName, String rowKey){
try (Table table = HBaseConnection.getTable(tableName)){
Get get = new Get(Bytes.toBytes(rowKey));
return table.get(get);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 根據過濾器來獲取單條數據
* @param tableName 表名
* @param rowKey 唯一標識
* @param filterList 過濾器
* @return 查詢結果
*/
public static Result getRow(String tableName, String rowKey, FilterList filterList){
try (Table table = HBaseConnection.getTable(tableName)){
Get get = new Get(Bytes.toBytes(rowKey));
get.setFilter(filterList);
return table.get(get);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 通過Scan來檢索數據
* @param tableName 表名
* @return 查詢結果
*/
public static ResultScanner getScanner(String tableName){
try (Table table = HBaseConnection.getTable(tableName)){
Scan scan = new Scan();
scan.setCaching(1000);
return table.getScanner(scan);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 批量檢索數據
* @param tableName 表名
* @param startRowKey 起始rowKey
* @param endRowKey 終止rowKey
* @return 查詢結果
*/
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey){
try (Table table = HBaseConnection.getTable(tableName)){
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes(startRowKey));
scan.withStopRow(Bytes.toBytes(endRowKey));
scan.setCaching(1000);
return table.getScanner(scan);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 使用過濾器批量檢索數據
* @param tableName 表名
* @param startRowKey 起始rowKey
* @param endRowKey 終止rowKey
* @param filterList 過濾器
* @return 查詢結果
*/
public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey, FilterList filterList){
try (Table table = HBaseConnection.getTable(tableName)){
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes(startRowKey));
scan.withStopRow(Bytes.toBytes(endRowKey));
scan.setFilter(filterList);
scan.setCaching(1000);
return table.getScanner(scan);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* HBase刪除一行結果
* @param tableName 表名
* @param rowKey 唯一標識
* @return 是否刪除
*/
public static boolean deleteRow(String tableName, String rowKey){
try (Table table = HBaseConnection.getTable(tableName)){
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
/**
* 刪除一個列族
* @param tableName 表名
* @param cfName 列族名
* @return 是否刪除
*/
public static boolean deleteColumnFamily(String tableName, String cfName){
try (HBaseAdmin admin = (HBaseAdmin)HBaseConnection.getHBaseConnection().getAdmin()){
admin.deleteColumn(tableName, cfName);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
/**
* 刪除某一列的qualifier
* @param tableName 表名
* @param rowKey 唯一標識
* @param cfName 列族名
* @param qualifier
* @return
*/
public static boolean deleteQualifier(String tableName, String rowKey, String cfName, String qualifier){
try (Table table = HBaseConnection.getTable(tableName)){
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier));
table.delete(delete);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
}
2. 編寫相關測試類進行測試
/**
* @title HBaseUtilTest
* @date 2019/12/9 11:55
* @description //todo
*/
public class HBaseUtilTest {
@Test
public void createTable(){
HBaseUtil.createTable("FileTable", new String[]{"fileInfo", "saveInfo"});
}
@Test
public void addFileDetails(){
HBaseUtil.putRow("FileTable", "rowkey1", "fileInfo", "name", "file1.txt");
HBaseUtil.putRow("FileTable", "rowkey1", "fileInfo", "type", "txt");
HBaseUtil.putRow("FileTable", "rowkey1", "fileInfo", "size", "1024");
HBaseUtil.putRow("FileTable", "rowkey1", "saveInfo", "creator", "suiwo1");
HBaseUtil.putRow("FileTable", "rowkey2", "fileInfo", "name", "file2.jpg");
HBaseUtil.putRow("FileTable", "rowkey2", "fileInfo", "type", "jpg");
HBaseUtil.putRow("FileTable", "rowkey2", "fileInfo", "size", "2048");
HBaseUtil.putRow("FileTable", "rowkey2", "saveInfo", "creator", "suiwo2");
}
@Test
public void getFileDetails(){
Result result = HBaseUtil.getRow("FileTable", "rowkey1");
if(result != null){
System.out.println("rowkey = " + Bytes.toString(result.getRow()));
System.out.println("fileName = " + Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("name"))));
}
}
@Test
public void scanFileDetail(){
ResultScanner scanner = HBaseUtil.getScanner("FileTable", "rowkey2", "rowkey2");
if(scanner != null){
scanner.forEach(result -> {
System.out.println("rowkey = " + Bytes.toString(result.getRow()));
System.out.println("fileName = " + Bytes.toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("name"))));
});
scanner.close();
}
}
@Test
public void deleteRow(){
HBaseUtil.deleteRow("FileTable", "rowkey1");
}
@Test
public void deleteTable(){
HBaseUtil.deleteTable("FileTable");
}
}