HBase学习---JAVA API基础使用(2)

使用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();
        }

    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码拉松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值