HBase2.x 的操作工具类-Java
在学习HBase的适合,没有相关的SpringBoot集成包,就自己写了一个连接工具,如下:
package com.imooc.passbook.util;
import com.imooc.passbook.mapper.RowMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
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;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @descriptions: Hbase操作工具
* @author: Hcat
* @date: 2020/7/10 17:49
* @version: 1.0
*/
@Slf4j
public class HBaseConnectUtil {
private static final String ZOOKEEPER_PORT = "2181";
private static final String ZOOKEEPER_HOST = "192.168.8.141";
private static Configuration configuration;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", ZOOKEEPER_PORT);
configuration.set("hbase.zookeeper.quorum", ZOOKEEPER_HOST);
}
/**
* 获取HBase连接
* @return {@link Connection}
*/
public static Connection getConnect() {
try {
return ConnectionFactory.createConnection(configuration);
} catch (IOException e) {
log.error("HBaseConnectUtil,创建HBase连接失败!");
e.printStackTrace();
}
return null;
}
/**
* 根据表名获取Table实例
* @param tableName
* @return
*/
public static Table getTableByName(String tableName) {
Connection conn = HBaseConnectUtil.getConnect();
Table table = null;
try {
table = conn.getTable(TableName.valueOf(tableName));
} catch (IOException e) {
log.error("Table对象获取失败!");
e.printStackTrace();
}
return table;
}
/**
* 根据表名添推送记录
* @param tableName
* @param put
*/
public static void putRow(String tableName, Put put) {
Table table = getTableByName(tableName);
putRow(table, put);
}
/**
* 根据Table对象推送记录
* @param table
* @param put
*/
public static void putRow(Table table, Put put) {
try {
table.put(put);
} catch (IOException e) {
log.error("HBase记录插入失败!");
e.printStackTrace();
} finally {
closeTable(table);
}
}
/**
* 关闭表对象
* @param table
*/
public static void closeTable(Table table) {
try {
table.close();
} catch (IOException e) {
log.error("table对象关闭失败!");
e.printStackTrace();
}
}
/**
* 扫描表,然后转换成List列表
* @param tableName
* @param scan
* @param rowMapper
* @param <T>,要转换的实体类型
* @return
*/
public static <T> List<T> scan(String tableName, Scan scan, RowMapper rowMapper) {
Table table = getTableByName(tableName);
List<T> entities = null;
try {
ResultScanner resultScanner = table.getScanner(scan);
entities = new ArrayList<>();
for (Result rs : resultScanner) {
// 将Result的键值转换成Java实体
entities.add((T) rowMapper.mapRow(rs));
}
} catch (IOException e) {
log.error("HBase表扫描失败!");
e.printStackTrace();
} finally {
closeTable(table);
}
return entities;
}
/**
* 获取行记录
* @param tableName
* @param rowKey
* @param rowMapper
* @param <T>
* @return
*/
public static <T> T getRow(String tableName, String rowKey, RowMapper rowMapper) {
Table table = getTableByName(tableName);
Get get = new Get(Bytes.toBytes(rowKey));
Result result = null;
try {
result = table.get(get);
} catch (IOException e) {
e.printStackTrace();
} finally {
closeTable(table);
}
return (T) rowMapper.mapRow(result);
}
/**
* 扫描全表
* @param tableName
* @param rowMapper
* @param <T>
* @return
*/
public static <T> List<T> getAllRow(String tableName, RowMapper rowMapper) {
Table table = getTableByName(tableName);
Scan scan = new Scan();
List<T> entities = null;
try {
ResultScanner resultScanner = table.getScanner(scan);
entities = new ArrayList<>();
for (Result rs : resultScanner) {
// 将Result的键值转换成Java实体
entities.add((T) rowMapper.mapRow(rs));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
closeTable(table);
}
return entities;
}
/**
* 删除指定行
* @param tableName
* @param rowKey
* @return
*/
public static boolean deleteRow(String tableName, String rowKey) {
Table table = getTableByName(tableName);
Delete delete = new Delete(Bytes.toBytes(rowKey));
try {
table.delete(delete);
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}