Hbase中通过代码API查询删除添加遍历示例

1.说明

Hbase这里采用的是完全分布式,hadoop中测试用到的是hdfs的存储,因为没有用到相关的计算,可以不用开启yarn

  • 使用了3个Zookeeper、3个Hbase、1个hadoop

在本地电脑中先配置hosts文件

  • 格式:ip  ip对应的主机名

2.代码示例

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HBaseDemo {

    private Configuration conf;

    @Before
    public void init() {
        // 获取HBase的配置
        conf = HBaseConfiguration.create();
        // 连接Zookeeper
        conf.set("hbase.zookeeper.quorum",
                "10.42.72.28:2181,10.42.118.45:2181:10.42.168.208:2181");
                //"hadoop01:2181,hadoop02:2181,hadoop03:2181");
    }

    // 建表
    @Test
    public void createTable() throws IOException {
        // 获取管理权
        HBaseAdmin admin = new HBaseAdmin(conf);
        // create 'user', 'info', 'expand'
        // 创建表描述器
        HTableDescriptor table =
                new HTableDescriptor(TableName.valueOf("user"));
        // 创建列描述器
        HColumnDescriptor c1 = new HColumnDescriptor("info");
        HColumnDescriptor c2 = new HColumnDescriptor("expand");
        // 添加列族
        table.addFamily(c1);
        table.addFamily(c2);
        // 建表
        admin.createTable(table);
        // 关闭管理权
        admin.close();
    }

    // 添加/修改数据
    @Test
    public void putData() throws IOException {
        // put 'user', 'u1', 'info:username', 'Alex'
        // put 'user', 'u1', 'info:password', '123456'
        // 指定表
        HTable table = new HTable(conf, "user");
        // 封装put对象
        Put put = new Put("u1".getBytes());
        put.addColumn("info".getBytes(),
                "username".getBytes(), "Alex".getBytes());
        put.addColumn("info".getBytes(),
                "password".getBytes(), "123456".getBytes());
        // 添加数据
        table.put(put);
        // 关流
        table.close();
    }

    // 测试:添加百万条数据
    @Test
    public void putMillionData() throws IOException {
        long begin = System.currentTimeMillis();
        HTable table = new HTable(conf, "user");
        List<Put> list = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            Put put = new Put(("u" + i).getBytes());
            put.addColumn("info".getBytes(),
                    "password".getBytes(), initPassword().getBytes());
            list.add(put);
            if (list.size() >= 2000) {
                table.put(list);
                list.clear();
            }
        }
        table.close();
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
    }

    public String initPassword() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            char c = (char) (Math.random() * 26 + 65);
            sb.append(c);
        }
        return sb.toString();
    }

    // 删除数据
    @Test
    public void deleteData() throws IOException {
        // 指定表
        HTable table = new HTable(conf, "user");
        // 封装Delete对象
        // 如果只指定行键表示删除一行数据
        Delete del = new Delete("u1".getBytes());
        // 如果指定列,则只删除这一个数据
        del.addColumn("info".getBytes(), "password".getBytes());
        // 删除数据
        table.delete(del);
        // 关流
        table.close();
    }

    // 查询数据
    @Test
    public void getData() throws IOException {
        // 指定表
        HTable table = new HTable(conf, "user");
        // 封装Get对象
        // get 'user', 'u1'
        Get get = new Get("u1".getBytes());
        // get 'user', 'u1', 'info:username'
        // get.addColumn("info".getBytes(), "username".getBytes());
        // 查询数据
        // 将结果封装成Result对象
        Result r = table.get(get);
        // 从Result获取数据
        byte[] value = r.getValue("info".getBytes(), "password".getBytes());
        System.err.println(new String(value));
        // 关流
        table.close();
    }

    // 遍历数据
    @Test
    public void scanData() throws IOException {
        // 指定表
        HTable table = new HTable(conf, "user");
        // 封装Scan对象
        // Scan scan = new Scan();
        // 指定起始行键,遍历到结尾
        // Scan scan = new Scan("u90000".getBytes());
        // 指定遍历范围
        Scan scan = new Scan("u90000".getBytes(), "u91000".getBytes());
        // 获取到结果扫描仪
        ResultScanner rs = table.getScanner(scan);
        // 转化为迭代器遍历
        Iterator<Result> it = rs.iterator();
        byte[] family = "info".getBytes();
        byte[] column = "password".getBytes();
        // 遍历
        while (it.hasNext()) {
            Result r = it.next();
            byte[] value = r.getValue(family, column);
            System.err.println(new String(value));
        }
        // 关流
        table.close();
    }

    // 过滤
    @Test
    public void filter() throws IOException {
        HTable table = new HTable(conf, "user");
        Scan scan = new Scan();
        // 封装Filter
        // 第一个参数指定比较方式
        // 第二个参数指定比较规则
        Filter f = new ValueFilter(
                CompareFilter.CompareOp.EQUAL,
                new RegexStringComparator(".*AAAA.*"));
        // 设置过滤器
        scan.setFilter(f);
        ResultScanner rs = table.getScanner(scan);
        Iterator<Result> it = rs.iterator();
        byte[] family = "info".getBytes();
        byte[] column = "password".getBytes();
        while (it.hasNext()) {
            Result r = it.next();
            byte[] value = r.getValue(family, column);
            System.err.println(new String(value));
        }
        table.close();
    }

    // 删表
    @Test
    public void dropTable() throws IOException {
        // 获取管理权
        HBaseAdmin admin = new HBaseAdmin(conf);
        // 禁用表
        admin.disableTable("user");
        // 删除表
        admin.deleteTable("user");
        // 关闭管理权
        admin.close();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值