使用HBase API练习统计某个用户某段时间内的消费记录。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class HBaseDemo2 {
TableName tableName = TableName.valueOf("consume");
Admin admin;
Table table;
Connection connection;
@Before
public void init() throws IOException {
//配置文件对象
Configuration configuration = HBaseConfiguration.create();
//zookeeper配置
configuration.set("hbase.zookeeper.quorum", "node02,node03,node04");
//获取连接
connection = ConnectionFactory.createConnection(configuration);
//管理对象
admin = connection.getAdmin();
//操作table对象
table = connection.getTable(tableName);
createTable();
}
public void createTable() throws IOException {
//表的描述
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
//列族描述
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes());
//设置表的列族
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
//如果表存在就删除
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
//创建表
admin.createTable(tableDescriptorBuilder.build());
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Random random = new Random();
/**
* 模拟随机消费金额
* @return
*/
private String getConsumeMoney() {
return String.valueOf(new Random().nextInt(1000));
}
/**
* 模拟一年内随机消费时间
* @return
*/
private String getConsumeDate() {
return "2020" + String.format("%02d%02d%02d%02d%02d", random.nextInt(12) + 1, random.nextInt(28), random.nextInt(24), random.nextInt(60), random.nextInt(60));
}
/**
* 模拟随机用户账号
* @return
*/
private String getAccount() {
return String.format("%08d", random.nextInt(99999999));
}
/**
* 生成1年内消费记录
*
* 1、随机生成10个用户
* 2、模拟每个用户1年内随机消费1000次
* @throws Exception
*/
@Test
public void consumingRecords() throws Exception {
List<Put> putList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
String userAccount = getAccount();
for (int j = 0; j < 1000; j++) {
String date = getConsumeDate();
String money = getConsumeMoney();
String rowKey = userAccount + "_" + (Long.MAX_VALUE - sdf.parse(date).getTime());
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("date"), Bytes.toBytes(date));
put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("money"), Bytes.toBytes(money));
putList.add(put);
}
}
table.put(putList);
}
/**
* 查询某个用户某段时间内的消费记录
*
* 比如查询95935637用户在2020年12月1号-2020年12月31号的消费记录
* @throws Exception
*/
@Test
public void consumingRecordsScan() throws Exception {
Scan scan = new Scan();
scan.withStartRow(Bytes.toBytes("95935637_" + (Long.MAX_VALUE - sdf.parse("20201231000000").getTime())));
scan.withStopRow(Bytes.toBytes("95935637_" + (Long.MAX_VALUE - sdf.parse("20201201000000").getTime())));
ResultScanner rs = table.getScanner(scan);
for (Result result : rs) {
Cell c1 = result.getColumnLatestCell(Bytes.toBytes("cf"), Bytes.toBytes("date"));
Cell c2 = result.getColumnLatestCell(Bytes.toBytes("cf"), Bytes.toBytes("money"));
String date = Bytes.toString(CellUtil.cloneValue(c1));
String money = Bytes.toString(CellUtil.cloneValue(c2));
System.out.println("消费时间:" + DateFormat.getDateTimeInstance().format(sdf.parse(date)) + ",消费金额:" + money);
}
}
@After
public void destory() {
try {
table.close();
admin.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}