引入依赖
首先需要引入HBase相关依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.4</version>
</dependency>
连接工具类
连接工具类需要设计成单例的,创建完成之后整个生命周期都可以使用
因为获取连接比较耗时,所以使用饿汉式一开始就创建了
定义两个静态的方法,分别用于获取连接和获取表
- 针对表结构的操作,获取连接进行操作
- 针对表内的操作,可以直接获取表进行操作
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import java.io.IOException;
/**
* 〈获取连接的单例的类〉
* 因为连接是贯穿整个操作的,所以采用饿汉式直接就创建了单例
* 不常用的可以用静态内部类,枚举类或者双重锁创建懒汉式单例
*
* @author Chkl
* @create 2020/5/5
* @since 1.0.0
*/
public class HBaseConn {
private static final HBaseConn INSTANCE = new HBaseConn();
private static Configuration configuration;
private static Connection connection;
//构造方法中初始化连接配置
private HBaseConn() {
try {
if (configuration == null) {
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "localhost");
configuration.set("hbase.zookeeper.property.clientPort", "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 getHBaseConn() {
return INSTANCE.getConnection();
}
//获取表对象
public static Table getTable(String tableName) throws IOException {
return INSTANCE.getConnection().getTable(TableName.valueOf(tableName));
}
//关闭连接
public static void closeConn(){
if (connection!=null){
try {
connection.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
基础工具类
- 创建表
/**
* 创建Hbase表
*
* @param tableName 表名
* @param cfs 列族数组
* @return 是否创建成功
*/
public static boolean createTable(String tableName, String[] cfs) {
try (HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHBaseConn().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 (IOException e) {
e.printStackTrace();
}
return true;
}
- 删除表
public static boolean deleteTable(String tableName){
try (HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHBaseConn().getAdmin()) {
admin.disableTable(tableName);//disable表
admin.deleteTable(tableName);//删除表
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
- 插入数据
/**
* hbase中插入一条数据
*
* @param tableName 表名
* @param rowKey 唯一标识
* @param cfName 列族名
* @param qualifier 列名
* @param data 数据
* @return 是否插入成功
*/
public static boolean putRow(String tableName, String rowKey,
String cfName, String qualifier, String data) {
try (Table table = HBaseConn.getTable(tableName)) {
//创建table对象
Put put = new Put(Bytes.toBytes(rowKey));//创建put对象
put.addColumn(Bytes.toBytes(cfName),
Bytes.toBytes(qualifier),
Bytes.toBytes(data));//封装put对象
table.put(put);//put数据
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
/**
* 插入多条数据
*
* @param tableName 表名
* @param puts 封装好的put集合
* @return 是否成功
*/
public static boolean putRows(String tableName, List<Put> puts) {
try (Table table = HBaseConn.getTable(tableName)) {
//创建table对象
table.put(puts);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
- 获取单条数据
/**
* 获取单条数据
*
* @param tableName 表名
* @param rowKey 唯一标识
* @return 查询结果
*/
public static Result getRow(String tableName, String rowKey) {
try (Table table = HBaseConn.getTable(tableName)) {
//创建table对象
Get get = new Get(Bytes.toBytes(rowKey));
return table.get(get);
} catch (IOException 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 = HBaseConn.getTable(tableName)) {
//创建table对象
Get get = new Get(Bytes.toBytes(rowKey));
get.setFilter(filterList);
return table.get(get);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
- 通过scanner扫描表中数据
/**
* 全表扫描
*
* @param tableName 表名
* @return ResultScanner
*/
public static ResultScanner getScanner(String tableName) {
try (Table table = HBaseConn.getTable(tableName)) {
//创建table对象
Scan scan = new Scan();
scan.setCaching(1000);//缓存条数
return table.getScanner(scan);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 带过滤器的扫描
* @param tableName 表名
* @param filterList 过滤器
* @return ResultScanner
*/
public static ResultScanner getScanner(String tableName,FilterList filterList) {
try (Table table = HBaseConn.getTable(tableName)) {
//创建table对象
Scan scan = new Scan();
scan.setCaching(1000);//缓存条数
scan.setFilter(filterList);
return table.getScanner(scan);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 按区间扫描
* @param tableName 表名
* @param startKey 起始rowkey
* @param endKey 终止rowKey
* @return ResultScanner
*/
public static ResultScanner getScanner(String tableName, String startKey, String endKey) {
try (Table table = HBaseConn.getTable(tableName)) {
//创建table对象
Scan scan = new Scan();
scan.setCaching(1000);//缓存条数
scan.setStartRow(Bytes.toBytes(startKey));
scan.setStopRow(Bytes.toBytes(endKey));
return table.getScanner(scan);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 带过滤器的按区间扫描
* @param tableName 表名
* @param startKey 起始rowkey
* @param endKey 终止rowKey
* @param filterList 过滤器
* @return ResultScanner
*/
public static ResultScanner getScanner(String tableName, String startKey, String endKey,
FilterList filterList) {
try (Table table = HBaseConn.getTable(tableName)) {
//创建table对象
Scan scan = new Scan();
scan.setCaching(1000);//缓存条数
scan.setStartRow(Bytes.toBytes(startKey));
scan.setStopRow(Bytes.toBytes(endKey));
scan.setFilter(filterList);
return table.getScanner(scan);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
- 删除行
/**
* hbase删除一行记录
* @param tableName 表名
* @param rowKey 唯一标识
* @return 是否成功
*/
public static boolean deleteRow(String tableName,String rowKey){
try (Table table = HBaseConn.getTable(tableName)) {
//创建table对象
Delete delete = new Delete(Bytes.toBytes