Hadoop期末复习贴-Hbase

若本文对你有帮助,请记得点赞、关注我喔!

Hbase是一个HBase是一个分布式的、面向列的开源数据库

…,
讲完理论,现在进入Hbase用Java实现API
静态配置
在这里插入图片描述

  • 创建表
/**
     * 一个admin,一个表构造器tableDescriptor,n个列族构造器
     *
     * @param tablename
     * @param info
     * @throws IOException
     */
    public static void CreateTable(String tablename, String info) throws IOException {
        TableName tableName = TableName.valueOf(tablename);
        Admin admin = connection.getAdmin(); //建表操作一定要通过connection.getAdmin产生一个admin
        TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);//表构造器
        ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build();//列构造器
        tableDescriptor.setColumnFamily(family);//表构造器中添加列构造器
        admin.createTable(tableDescriptor.build());//!!!一定要有这句
        admin.close();
    }
  • 插入数据
    /**
     * connect这次不是获得.getAdmin,而是.getTable
     * 插入数据核心:Put put = new Put()
     * 强调:Hbase中任何数据都是Bytes类型
     * 基本模型 :一个table 一个put 三个值放put里 113
     * connect.getTable->table
     * new Put(byte的row)->put
     * put.addColumn(列族,列族属性,值)->put
     * table.put(put)->table
     *
     * @param tablename
     * @param Row
     * @param ColumnFamily
     * @param Qualifier
     * @param Value
     * @throws IOException
     */
    public static void InsertInfo(String tablename, String Row, String ColumnFamily, String Qualifier, String Value) throws IOException {
        TableName tableName = TableName.valueOf(tablename);
        Table table = connection.getTable(tableName);
        try {
            byte[] row = Bytes.toBytes(Row);
            Put put = new Put(row);
            put.addColumn(Bytes.toBytes(ColumnFamily), Bytes.toBytes(Qualifier), Bytes.toBytes(Value));
            table.put(put);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 查询数据
    public static void queryTableInfo(String tablename, String Row) throws IOException {
        TableName tableName = TableName.valueOf(tablename);
        Table table = connection.getTable(tableName);
        try {
            Get get = new Get(Bytes.toBytes(Row));//通过某一行Bytes的产生的get对象
            Result result = table.get(get);
            //返回值是Bytes
            byte[] valueBytes = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
            String valueStr = new String(valueBytes, "utf-8");
            System.out.println(valueStr);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 删除表
    /**
     * 删除表是从connect.getAdmin
     * 禁用,再删除
     */
    public static void deleteTable(String tablename) throws IOException {
        Admin admin = connection.getAdmin();
        TableName tableName = TableName.valueOf(tablename);
        admin.disableTable(tableName);
        admin.deleteTable(tableName);
    }
  • 获得多行数据
    /**
     * 也就是说Result result = table.get(get),其中形参get,不仅仅可以是一个对象,而且可以是一个对象集合
     * 所以将2008,2009两行循环都转化Bytes,append进gets,最后gets作为形参转入table.get()
     *
     * @param tablename
     * @throws Exception
     */
    public static void batchGet(String tablename) throws Exception {
        TableName tableName = TableName.valueOf(tablename);//?
        Table table = connection.getTable(tableName);//获取Table对象

        String[] rows = new String[]{"1001", "1002"};
        List<Get> gets = new ArrayList<>();
        for (String str : rows) {
            gets.add(new Get(Bytes.toBytes(str)));
        }
        //可以直接将Get对象集合传入
        Result[] results = table.get(gets);
        for (Result result:results){
            System.out.println("Row:"+Bytes.toString(result.getRow()));
            for (Cell kv:result.rawCells()){ //将同一个Row的值遍历完
                String family = Bytes.toString(CellUtil.cloneFamily(kv));
                String qualifier = Bytes.toString(CellUtil.cloneQualifier(kv));
                String value = Bytes.toString(CellUtil.cloneValue(kv));
                System.out.println(family + ":" + qualifier + "\t" + value);
            }
        }
    }

下午补看书补理论hhhh

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值