Hbase入门篇

HBase:

  1. 数据库:是一种面向列族存储的非关系型数据库
  2. 用于存储结构化和非结构化数据:适用于单表非关系型数据的存储,不适合做关联查询,类似于JOIN等操作
  3. 基于HDFS:数据持久化存储的体现形式是HFile,存放于DataNode中,被ResionServer以Region的形式进行管理
  4. 延迟较低,接入在线业务使用:面对大量的企业数据,HBase可以直线单表大量数据的存储,同时提供了高效的数据访问速度。 
package csdn.dreamzuora;


import com.sun.istack.internal.logging.Logger;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

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


/**
 * Title:
 * Description:
 *
 * @version 1.0
 * @author: weijie
 * @date: 2020/11/10 18:02
 */
public class HbaseUtils {

    public static Configuration conf;

    private Logger logger = Logger.getLogger(HbaseUtils.class);

    static{
        //使用 HBaseConfiguration 的单例方法实例化
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "39.96.204.209");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
    }

    public Connection getConnection() throws IOException {
        return ConnectionFactory.createConnection(conf);
    }

    public Admin getAdmin() throws IOException {
        Connection connection = getConnection();
        return connection.getAdmin();
    }

    public Table getTable(TableName tableName) throws IOException {
        Connection connection = getConnection();
        return connection.getTable(tableName);
    }

    /**
     * 判断表是否存在
     * @param tableName
     * @return
     * @throws IOException
     */
    public boolean isTableExist(String tableName) throws IOException {
        Admin admin = getAdmin();
        return admin.tableExists(TableName.valueOf(tableName));
    }


    /**
     * 创建表
     * @param tableName
     * @param columnFamily
     * @throws IOException
     */
    public void createTable(String tableName, String... columnFamily) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        //判断表是否存在
        if (isTableExist(tableName)){
            logger.info("表" + tableName + "已存在");
        }else {
            //创建表属性对象,表名需要转字节
            HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
            //创建多个列族
            for (String cf : columnFamily){
                hTableDescriptor.addFamily(new HColumnDescriptor(cf));
            }
            //根据对表的配置,创建表
            admin.createTable(hTableDescriptor);
            logger.info("表" + tableName + "创建成功!");
        }
    }

    /**
     * 删除表
     */
    public void dropTable(String tableName) throws IOException {
        Admin admin = getAdmin();
        if (isTableExist(tableName)){
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            logger.info("表" + tableName + "删除成功!");
        }else {
            logger.info(tableName + "表不存在");
        }
    }

    /**
     * 向表中插入数据
     */
    public void addRowData(String tableName, String rowkey, String columnFamily, String column, String value) throws IOException {
        //创建表对象
        Table table = getTable(TableName.valueOf(tableName));
        //向表中插入数据
        Put put = new Put(Bytes.toBytes(rowkey));
        //向Put对象中组装数据
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        table.put(put);
        table.close();
        logger.info("数据插入成功");
    }

    /**
     * 删除多行数据
     */
    public void deleteMultiRow(String tableName, String... rows) throws IOException {
        Table table = getTable(TableName.valueOf(tableName));
        List<Delete> deleteList = new ArrayList<Delete>();
        for (String row : rows){
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteList.add(delete);
        }
        table.delete(deleteList);
        table.close();
    }

    /**
     * 获取所有数据
     */
    public void getAllRows(String tableName) throws IOException {
        Table table = getTable(TableName.valueOf(tableName));
        //得到用于扫描region的对象
        Scan scan = new Scan();
        //使用Htable得到resultcanner实现类的对象
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner){
            Cell[] cells = result.rawCells();
            for (Cell cell : cells){
                //得到rowkey
                logger.info("行健: " + Bytes.toString(CellUtil.cloneRow(cell)));
                //得到列族
                logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
                //列名
                logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
                //列值
                logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

    /**
     * 获取某一行数据
     */
    public void getRow(String tableName, String rowkey) throws IOException {
        Table table = getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowkey));
        /**
         * get.setMaxVersions() 显示所有版本
         * get.setTimeStamp(timeStamp) 显示指定时间戳版本
         */
        Result result = table.get(get);
        for (Cell cell : result.rawCells()){
            logger.info("行键: " + Bytes.toString(result.getRow()));
            //得到列族
            logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
            //列名
            logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
            //列值
            logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

    /**
     * 获取某一行指定"列族:列"的数据
     */
    public void getRowQualifier(String tableName, String rowkey, String family, String qualifier) throws IOException {
        Table table = getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowkey));
        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        Result result = table.get(get);
        for (Cell cell : result.rawCells()){
            logger.info("行键: " + Bytes.toString(result.getRow()));
            //得到列族
            logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));
            //列名
            logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
            //列值
            logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

}

视频学习地址:https://www.bilibili.com/video/BV1Y4411B7jy?p=7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HBase从入门到精通》是一本关于HBase数据库的学习指南。它主要介绍了HBase的基本概念、架构和使用方法,以及如何进行高级配置和优化。这本书适合那些想要深入了解HBase技术的读者,无论是初学者还是有一定经验的开发人员。 首先,这本书会帮助读者了解HBase的起源和背景,介绍Hadoop生态系统中的HBase定位和关键特性。然后,它会详细解释HBase的数据模型和各个组件的功能。读者将学习如何设计数据表和列族,以及如何使用HBase的查询语言进行数据检索。 接下来,这本书会介绍HBase的架构和工作原理。读者将了解Region Server、Master Server和ZooKeeper等关键组件的功能和作用。同时,它还会讲解HBase的数据分布和复制机制,以及如何进行故障恢复和集群管理。 除了基础知识,这本书还会深入探讨HBase的高级应用和优化技巧。读者将学习如何构建高性能的HBase应用程序,包括数据插入、读取和更新的最佳实践。同时,它还会介绍HBase的二级索引、过滤器和缓存机制等高级功能,以及如何进行数据压缩和性能调优。 总的来说,通过学习《HBase从入门到精通》,读者将能够全面掌握HBase的核心概念和技术,从而能够独立设计、开发和管理HBase数据库。无论是对于个人技能提升还是实际项目应用,这本书都是一份非常有价值的学习资料。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值