一、HbaseUtil
在HbaseUtil中具体要做这些事,封装一下方法:创建命名空间,创建表,判断表是否存在,分区键生成,rowkey设计
重点的是分区键的设计和rowkey的生成
1、创建命名空间和判断表存不存在
比较简单
public static void createNamespace(String ns) throws IOException {
//获取连接&Admin对象
Connection connection = ConnectionFactory.createConnection(Constant.CONF);
Admin admin = connection.getAdmin();
//创建命名空间描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
//创建命名空间
admin.createNamespace(namespaceDescriptor);
//关闭资源
admin.close();
connection.close();
}
//判断表是否存在
private static boolean tableExist(String tableName) throws IOException {
//获取连接&Admin
Connection connection = ConnectionFactory.createConnection(Constant.CONF);
Admin admin = connection.getAdmin();
//判断
boolean exists = admin.tableExists(TableName.valueOf(tableName));
//关闭资源
admin.close();
connection.close();
//返回true或者false
return exists;
}
2、创建表
//创建表
public static void createTable(String tableName, String... cfs) throws IOException {
//获取连接&Admin
Connection connection = ConnectionFactory.createConnection(Constant.CONF);
Admin admin = connection.getAdmin();
//判断表是否存在
if (tableExist(tableName)) {
return;
}
//创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//循环添加列描述器
for (String cf : cfs) {
//创建列描述器
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hTableDescriptor.addFamily(hColumnDescriptor);
}
int regions = Integer.parseInt(PropertyUtil.getProperty("hbase.regions"));
//创建表操作
admin.createTable(hTableDescriptor, getSplits(regions));
//关闭资源
admin.close();
connection.close();
}
3、分区键的设计和rowkey的设计
见博客https://blog.csdn.net/student__software/article/details/81782375
二、Hbasedao
作用:
* 1.创建命名空间
* 2.创建表
* 3.封装批量插入HBASE数据的puts方法***
public class HbaseDAO {
//命名空间名称
private String ns = null;
//表名
private String tableName = null;
//预分区数
private int regions;
//put集合
private List<Put> puts;
private Connection connection = null;
private Table table;
public HbaseDAO() throws IOException {
//初始化相应的属性
ns = PropertyUtil.getProperty("hbase.namespace");
tableName = PropertyUtil.getProperty("hbase.table.name");
regions = Integer.parseInt(PropertyUtil.getProperty("hbase.regions"));
puts = new ArrayList<>();
connection = ConnectionFactory.createConnection(Constant.CONF);
table = connection.getTable(TableName.valueOf(tableName));
//创建命名空间&表
HbaseUtil.createNamespace(ns);
HbaseUtil.createTable(tableName, PropertyUtil.getProperty("hbase.cf"));
}
public void puts(String line) throws IOException {
//1.判断数据是否合法
if (line.split(",").length < 4) {
return;
}
//2.切割
String[] splits = line.split(",");
String call1 = splits[0];
String call2 = splits[1];
String buildTime = splits[2];
String duration = splits[3];
//3.封装Put对象
//获取分区号
String parId = HbaseUtil.getParId(call1, buildTime, regions);
//拼接rowkey
String row = HbaseUtil.getRow(parId, call1, buildTime, call2, duration);
//创建put对象
Put put = new Put(Bytes.toBytes(row));
//添加数据
put.addColumn(Bytes.toBytes(PropertyUtil.getProperty("hbase.cf")), Bytes.toBytes("call1"), Bytes.toBytes(call1));
put.addColumn(Bytes.toBytes(PropertyUtil.getProperty("hbase.cf")), Bytes.toBytes("call2"), Bytes.toBytes(call2));
put.addColumn(Bytes.toBytes(PropertyUtil.getProperty("hbase.cf")), Bytes.toBytes("buildTime"), Bytes.toBytes(buildTime));
put.addColumn(Bytes.toBytes(PropertyUtil.getProperty("hbase.cf")), Bytes.toBytes("duration"), Bytes.toBytes(duration));
//4.将put对象缓存到集合中
puts.add(put);
//5.根据集合大小,写到hbase,清空集合
if (puts.size() >= 20) {
table.put(puts);
puts.clear();
}
}
public void close() throws IOException {
table.put(puts);
table.close();
connection.close();
}
public void timePut() throws IOException {
table.put(puts);
}
}