0601-HBase

第一章 HBase简介

第二章 HBase安装

2.1 Zookeeper正常部署

2.2 Hadoop正常部署

2.3 解压

2.4 修改配置文件

2.4.1 hbase-env.sh

export JAVA_HOME=/opt/module/jdk1.8.0_144
export HBASE_MANAGES_ZK=false
JDK1.8需要注释
#export HBASE_MASTER_OPTS。。。。
#export HBASE_REGIONSERVER_OPTS。。。

2.4.2 hbase-site.xml

<configuration>
	<property>     
		<name>hbase.rootdir</name>     
		<value>hdfs://node11:9000/hbase</value>   
	</property>

	<property>   
		<name>hbase.cluster.distributed</name>
		<value>true</value>
	</property>

   <!-- 0.98后的新变动,之前版本没有.port,默认端口为60000 -->
	<property>
		<name>hbase.master.port</name>
		<value>16000</value>
	</property>

	<property>   
		<name>hbase.zookeeper.quorum</name>
	     <value>node11:2181,node12:2181,node13:2181</value>
	</property>

	<property>   
		<name>hbase.zookeeper.property.dataDir</name>
	     <value>/opt/module/zookeeper-3.4.10/zkData</value>
	</property>
</configuration>

2.4.3 regionservers

node11
node12
node13

2.4.4 软连接hadoop配置文件到hbase

[yanlzh@node11 module]$ ln -s /opt/module/hadoop-2.7.2/etc/hadoop/core-site.xml /opt/module/hbase/conf/core-site.xml
[yanlzh@node11 module]$ ln -s /opt/module/hadoop-2.7.2/etc/hadoop/hdfs-site.xml /opt/module/hbase/conf/hdfs-site.xml

2.5 群发

2.6 启动HBase服务

单独启动

[yanlzh@node11 hbase]$ bin/hbase-daemon.sh start master
[yanlzh@node11 hbase]$ bin/hbase-daemon.sh start regionserver

群起

[atguigu@hadoop102 hbase]$ bin/start-hbase.sh
[atguigu@hadoop102 hbase]$ bin/stop-hbase.sh

2.7 查看HBase页面

http://node11:16010

第三章 HBase Shell操作

3.1 创建表

create 'student','info'

3.2 插入数据到表

put 'student','1001','info:sex','male'
put 'student','1001','info:age','18'
put 'student','1002','info:name','Janna'
put 'student','1002','info:sex','female'
put 'student','1002','info:age','20'

3.3 扫描查看表数据

scan 'student'
scan 'student',{STARTROW => '1001', STOPROW => '1003'}
scan 'student',{STARTROW => '1001'}

3.4 查看表结构

describe 'student'

3.5 更新指定字段的数据

put 'student','1001','info:name', 'Nick'
put 'student','1001','info:age','100'

3.6 查看"指定行"或"指定列族:列"的数据

get 'student','1001'
get 'student','1001','info:name'

3.7 统计表数据行数

count 'student'

3.8 删除数据

deleteall 'student','1001'
delete 'student', '1002','info:sex'

3.9 清空表数据

truncate 'student'

3.10 删除表

disable 'student'
drop 'student'

3.11 变更表信息

alter 'student',{NAME=>'info',VERSIONS=>3}

第四章 HBase数据结构

4.1 RowKey

与nosql数据库们一样,rowKey是用来检索记录的主键.
访问HBASE table中的,只有三种方式:

  1. 通过单个rowkey访问(get)
  2. 通过rowkey的range(正则)like
  3. 全表扫描(scan)

RowKey行键 (RowKey)可以是任意字符串(最大长度是64KB,实际应用中长度一般为 10-100bytes),在HBASE内部,RowKey保存为字节数组。存储时,数据按照RowKey的字典序(byte order)排序存储。设计RowKey时,要充分排序存储这个特性,将经常一起读取的行存储放到一起。(位置相关性)

4.2 Column Family

列族:HBASE表中的每个列,都归属于某个列族。列族是表的schema的一部分(而列不是),必须在使用表之前定义。列名以列族作为前缀。例如 courses:history,courses:math都属于courses 这个列族。

4.3 Ceil

由{rowkey, column Family:columu, version} 唯一确定的单元。cell中的数据是没有类型的,全部是字节码形式存贮。
关键字:无类型、字节码

4.4 TimeStamp

HBASE 中通过rowkey和columns确定的为一个存贮单元称为cell。每个 cell都保存 着同一份数据的多个版本版本通过时间戳来索引。时间戳的类型是 64位整型。时间戳可以由HBASE(在数据写入时自动 )赋值,此时时间戳是精确到毫秒 的当前系统时间。时间戳也可以由客户显式赋值。如果应用程序要避免数据版 本冲突,就必须自己生成具有唯一性的时间戳。每个 cell中,不同版本的数据按照时间倒序排序,即最新的数据排在最前面。

4.5 Namespace

命名空间的结构:
在这里插入图片描述

扫描二维码关注公众号,回复: 9068104 查看本文章
  1. Table:表,所有的表都是命名空间的成员,即表必属于某个命名空间,如果没有指定,则在default默认的命名空间中
  2. RegionServer group:一个命名空间包含了默认的RegionServer Group。
  3. Permission:权限,命名空间能够让我们来定义访问控制列表ACL(Access Control List)。例如,创建表,读取表,删除,更新等等操作。
  4. Quota:限额,可以强制一个命名空间可包含的region的数量。

第五章 HBase原理

5.1 读流程

在这里插入图片描述

1)Client先访问zookeeper,从meta表读取region的位置,然后读取meta表中的数据。meta中又存储了用户表的region信息;
2)根据namespace、表名和rowkey在meta表中找到对应的region信息;
3)找到这个region对应的regionserver
4)查找对应的region;
5)先从MemStore(写缓存)找数据,如果没有,再到BlockCache(读缓存)里面读;
6)BlockCache还没有,再到StoreFile上读(为了读取的效率);
7)如果是从StoreFile里面读取的数据,不是直接返回给客户端,而是先写入BlockCache,再返回给客户端

  • 客户端去zookeeper那里获取 hbase的元数据信息在哪台机器上(regionServer1)
  • 客户端去regionServer1 上读取元数据信息, 获取所要查询的数据在哪台机器上(regionServer4)
  • 客户端去regionServer4 上读取要查询的数据信息(读取细节如下:)
    • 先找region(高表水平切分),可以理解为分区partition
    • 从Memstore(写缓存)中找数据,如果没有
    • 再从BlockCache(读缓存)中找数据,如果没有
    • 最后从StoreFile中找数据
    • 从StoreFile里面读取的数据,不是直接返回给客户端,而是先写入BlockCache,再返回给客户端

在这里插入图片描述

5.2 写流程

在这里插入图片描述

1)Client向HregionServer发送写请求;
2)HregionServer将数据写到HLog(write ahead log)。为了数据的持久化和恢复;
3)HregionServer将数据写到内存(MemStore);
4)反馈Client写成功。

在这里插入图片描述

5.3 数据Flush过程

5.4 数据合并过程

第六章 HBase API操作

6.1 环境准备

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>1.3.1</version>
</dependency>

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.3.1</version>
</dependency>

6.2 HBase API

6.2.0 连接HBase步骤

		// 0. 获取配置对象, 获取hbase的连接
        Configuration conf = HBaseConfiguration.create();
        // 1. 创建hbase连接对象
        Connection connection = ConnectionFactory.createConnection(conf);
        // 2. 获取操作对象
        // (1)DDL-----> Admin
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        HBaseAdmin admin2 = new HBaseAdmin(conf);
        // (2)DCL-----> Table
        HTable table = (HTable) connection.getTable();
        HTable table2 = new HTable(conf, tableName);
        // 3. 操作数据库
        admin.tableExists("student");
        // 4. 获取操作结果
        // 5. 关闭数据库连接

6.2.1 获取configuration对象

	public static Configuration conf;
    static{
        conf = HBaseConfiguration.create();
    }

6.2.2 判断表是否存在

    public static boolean isTableExist(String tableName) throws Exception {
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        return admin.tableExists(tableName);
    }

6.2.3 创建表

    // 创建表
    public static void createTable(String tableName, String... columnFamily) throws Exception {
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        // 判断表是否存在
        if (isTableExist(tableName)) {
            System.out.println("table:" + tableName + "is exist");
            System.exit(0);
        } else {
            // 创建表属性对象, 表名需要转字节
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            // 创建多个列族
            for (String cf : columnFamily) {
                descriptor.addFamily(new HColumnDescriptor(cf));
            }
            admin.createTable(descriptor);
        }
    }

6.3.4 删除表

	public static void dropTable(String tableName) throws Exception {
        HBaseAdmin admin = new HBaseAdmin(conf);
        if (isTableExist(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }
    }

6.3.5 向表中插入数据

    public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
        // 创建HTable对象
        HTable hTable = new HTable(conf, tableName);
        // 向表中插入数据
        Put put = new Put(Bytes.toBytes(rowKey));
        // 向put对象中组装数据
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        hTable.put(put);
        hTable.close();
        System.out.println("插入数据成功");
    }

6.3.6 删除多行数据

    // 删除多行数据
    public static void deleteMultiRow(String tableName, String... rows) throws IOException {
        HTable hTable = new HTable(conf, tableName);
        ArrayList<Delete> deleteArrayList = new ArrayList<Delete>();
        for (String row : rows) {
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteArrayList.add(delete);
        }
        hTable.delete(deleteArrayList);
        hTable.close();
    }

6.3.7 获取所有数据

    // 获取所有数据
    public static void getAllRows(String tableName) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        HTable table = (HTable) connection.getTable(TableName.valueOf(tableName));

        // 得到用于扫描region的对象
        Scan scan = new Scan();
        // 使用HTable得到resultscaner实现类的对象
        ResultScanner resultSca = table.getScanner(scan);

        for (Result result : resultSca) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("row:" + Bytes.toString(CellUtil.cloneRow(cell)));
                System.out.println("cf:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("col:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

6.3.8 获取某一行数据

    // 获取某一行数据
    public static void getRow(String tableName, String rowKey) throws IOException {
        HTable hTable = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = hTable.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println("row:" + Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("cf:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("col:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("timeStamp:" + cell.getTimestamp());
        }
    }

6.3.9 获取某一行指定"列族"的数据

    // 获取某一行指定“列族:列”的数据
    public static void getRowByQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException {
        HTable hTable = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        Result result = hTable.get(get);

        for (Cell cell : result.rawCells()) {
            System.out.println("row:" + Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("cf:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("col:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }
发布了43 篇原创文章 · 获赞 0 · 访问量 542

猜你喜欢

转载自blog.csdn.net/qq_35199832/article/details/103481907