Hbase复习总结之一

1. Hbase 的简介

特点
强一致性读/写: HBASE不是“最终一致的” 数据存储 , 它非常适合于诸如高速计数器聚合等任务
自动分块: HBase表通过Region分布在集群上,随着数据的增长,区域被自动拆分和重新分布
自动RegionServer故障转移
Hadoop/HDFS集成: HBase支持HDFS开箱即用作为其分布式文件系统
MapReduce : HBase通过MapReduce支持大规模并行处理,将HBase用作源和接收器
块缓存和布隆过滤器 : HBase支持块Cache和Bloom过滤器进行大容量查询优化
运行管理: HBase为业务洞察和JMX度量提供内置网页。

重要特点:
大:一个表可以有上十亿行,上百万列
面向列:面向列(族)的存储和权限控制,列(族)独立检索。
稀疏:对于为空(null)的列,并不占用存储空间,因此,表可以设计的非常稀疏。

2.hbase 数据模型:

列(Column): HBase中的列有列族(column family) 和列限定符(列名)(Column Qualifier)组成。
列族(Column Family):
	出于性能原因, 列族将一组列及其值组织在一起
	每个列族都有一组存储属性: 例如 是否应该换成在内存中, 数据如何被压缩等
	表中的每一行都有相同的列族, 但在列族中不存储任何内容
	所有的列族的数据全部都存储在一块(文件系统HDFS)
	Hbase官方建议所有的列族保持一样的列, 并且将同一类的列放在一个列族中

列标识符(Column Qualifier)
列族中包含一个个的列限定符, 这样可以为存储的数据提供索引
列族在创建表的时候是固定的, 但列限定符是不做限制的
不同的列可能会存在不同的列标识符
单元格(Cell): 单元格是行、列族和列限定符的组合,包含一个值和一个时间戳, 数据以二进制存储
版本号(verson num): 每条数据都会有版本号的概念
每条数据都可以有多个版本号, 默认值为系统时间戳, 类型为Long
每条数据都可以有多个版本号, 默认值为系统时间戳, 类型为Long
在向Hbase插入更新数据的时候, HBase默认会将当前操作的时间记录下来, 当然也可以人为指定时间
不同版本的数据按照时间倒序排序, 即最新的数据排在最前面。

物理存储模型

在这里插入图片描述

3. Hbase 的常用命令:

1. 进入hbase : hbase shell
2. 查看数据库中有哪些表: list
3. 创建一个表 : 创建 一个user表 和连个列簇:  info data
	create 'user', 'info', 'data'  或者 : create 'user', 'info'
4. 添加两条数据
 	put 'user','10000','info:name','zhangsan'
	put 'user','10000','info:age','20'

 5.查询
 通过scan 全表扫描:
 	scan 'user'
 指定rowkey 进行查询
 	get 'user','10000'
5. 查看rowkey 下面的某个列簇信息 : get ' 表名','rowkey','列簇'
	 get 'user','10000','info'
6查询rowkey 指定列簇和指定字段的值 :get '表名','rowkey','列簇:列名','列簇:列名'
	get 'user','10000','info:age','info:name'
7.查看rowkey 指定多个列簇信息
 	get 'user','10000','info'
 	 	 或者
 	get 'user','10000',{
    
    COLUMN =>['info','data']}
8.指定rowkey 和列进行查询
	get 'user','10000',{
    
    FILTER=>"ValueFilter(=,'binary:zhangsan')"}
9.查询所有数据
	sacn 'user'
	scan 'user',{
    
    FORMATTER => 'toString'}
	scan 'user' ,{
    
    LIMIT =>3,FORMATTER=>'toString'}
10.列簇查询: 查询user 表中info 的信息
	scan 'user',{
    
    COLUMNS => 'info'}
		或
	scan 'user',{
    
    COLUMNS => 'info',RAW=>true,VERSIONS=>5}
11.多列簇的查询:
	scan 'user',{
    
    COLUMNS=>['info','data']}
		或
	scan 'user',{
    
    COLUMNS=>['info:name','data:pic']}
12. 查询指定列簇与某个列名的查询
	scan 'user',{
    
    COLUMNS=>'info:name'}
13.指定列簇和列名以及限定版本查询
	scan 'user',{
    
    COLUMNS=>'info:name',VERSIONS=>5}
14.指定多个列簇与按照数据值模糊查询
	scan 'user',{
    
    COLUMNS=>['info','data'],FILTER=>"(QualifierFilter(=,'substring:a'))"}
15. 指定rowkey 的范围值查询
	scan 'user',{
    
    COLUMNS=>'info',STARTROW=>'10000',ENDROW=>'10000'}
16.指定rowkey 模糊查询
	scan 'user',{
    
    FILTER=>"PrefixFilter('info')"}
17.指定数据范围值查询 :   查询user表中指定范围的数据
scan 'user', {
    
    TIMERANGE => [1392368783980, 1392380169184]}

4 更新操作

1. 更新版本号: 更新表user 表f1 列簇的版本号
	alter 'user',NAME=>'info',VERSIONS=>5
	
	

5. 删除操作

指定rowkey 和列名进行行删除
delete 'user', '10000', 'info:name'
指定rowkey 删除正行数据
deleteall 'user', '10000'

删除一个列簇
	alter 'user', NAME => 'info', METHOD => 'delete’
 或
	alter 'user', 'delete' => 'info'
清空表数据
truncate 'user'

删除表: 注意要先禁用表在进行删除
disable 'user'
drop 'user'


6. 统计有多少行的数据

count 'user'

7. java代码操作

需要引入的pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>wudl-bigdata</artifactId>
        <groupId>com-wudl-bigdata</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>wudl-hbase-operate</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <target>1.8</target>
                    <source>1.8</source>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

java 代码

package com.hbase.api;

import javafx.scene.control.Tab;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :wudl
 * @date :Created in 2021-12-04 22:55
 * @description:Hbase 的操作
 * @modified By:
 * @version: 1.0
 */

public class HbaseOperate {
    
    
    public static void main(String[] args) throws IOException {
    
    

        // 创建表
        HbaseOperate.Connection();
        // 添加数据
//        addData();

//        getList();
        //删除数据
        delData();
    }


    public static Connection getConnection() throws IOException {
    
    
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.161:2181");
        Connection conn = null;
        if (conn == null) {
    
    
            return ConnectionFactory.createConnection(conf);
        }

        return conn;
    }

    public static void Connection() throws IOException {
    
    

        Admin admin = getConnection().getAdmin();

        TableName wudlHbase = TableName.valueOf("wudlHbase");
        boolean isTable = admin.tableExists(wudlHbase);
        if (!isTable) {
    
    
            TableDescriptorBuilder newBuilder = TableDescriptorBuilder.newBuilder(wudlHbase);
            List<ColumnFamilyDescriptor> familList = new ArrayList<>();
            // 添加列簇
            familList.add(ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes()).build());
            // 在表的构造器中添加表的列簇信息
            newBuilder.setColumnFamilies(familList);
            // 通过表构造器,构建表对象
            TableDescriptor build = newBuilder.build();
            // 执行创建表操作
            admin.createTable(build);
        }
        //释放资源
        admin.close();
        // 关闭连接
        getConnection().close();

    }

    /**
     * 往 表中添加一行数据
     */

    public static void addData() throws IOException {
    
    
        Admin admin = getConnection().getAdmin();
        TableName wudlHbase = TableName.valueOf("wudlHbase");
        boolean isTable = admin.tableExists(wudlHbase);
        if (isTable) {
    
    
            Table table = getConnection().getTable(TableName.valueOf("wudlHbase"));
            for (int i = 0; i < 10; i++) {
    
    
                String number = 10000 + i + "";
                String name = "hdfs" + i;
                String address = "广东省" + i;
                String age = 10 + i + "";
                Put put = new Put(number.getBytes());
                put.addColumn("cf".getBytes(), "name".getBytes(), name.getBytes());
                put.addColumn("cf".getBytes(), "age".getBytes(), age.getBytes());
                put.addColumn("cf".getBytes(), "address".getBytes(), address.getBytes());
                table.put(put);
            }
            table.close();
            getConnection().close();
            System.out.println("数据添加成功-------------->");
        }
    }

    /**
     * 查询数据
     */
    public static void getList() throws IOException {
    
    
        Admin admin = getConnection().getAdmin();
        Table table = getConnection().getTable(TableName.valueOf("wudlHbase"));
        // 获取row 为4949191 的数据
        Get get = new Get("4949191".getBytes());
        Result result = table.get(get);
        List<Cell> cells = result.listCells();
        for (Cell cell : cells) {
    
    
            byte[] rowKeyBytes = CellUtil.cloneRow(cell);
            byte[] familyBytes = CellUtil.cloneFamily(cell);
            byte[] qualifierBytes = CellUtil.cloneQualifier(cell);
            byte[] valueBytes = CellUtil.cloneValue(cell);
            String rowKey = Bytes.toString(rowKeyBytes);
            String family = Bytes.toString(familyBytes);
            String qualifier = Bytes.toString(qualifierBytes);

            if (qualifier.equals("NUM_CURRENT") || qualifier.equals("NUM_PREVIOUS") || qualifier.equals("TOTAL_MONEY") || qualifier.equals("NUM_USAGE")) {
    
    
                double value = Bytes.toDouble(valueBytes);
                System.out.println("rowkey-->" + rowKey + ":family--->" + family + "--;qualue--->" + qualifier + "---;value----->" + value);
            } else {
    
    
                String value = Bytes.toString(valueBytes);
                System.out.println("rowkey-->" + rowKey + ":family--->" + family + "--;qualue--->" + qualifier + "---;value----->" + value);
            }
        }

        table.close();
        getConnection().close();
        System.out.println("查询数据完成-------------->");
    }

    /**
     * 删除一条数据
     * @throws IOException
     */
    public static void delData() throws IOException {
    
    
        Table table = getConnection().getTable(TableName.valueOf("wudlHbase"));
        Delete delete = new Delete("4949191".getBytes());
        delete.addFamily("cf".getBytes());
        delete.addColumn("cf".getBytes(), "name".getBytes());
        table.delete(delete);

    }

    public static  void delTable() throws IOException {
    
    
        Admin admin = getConnection().getAdmin();
        if (!admin.tableExists(TableName.valueOf("wudlHbase"))){
    
    
            admin.disableTable(TableName.valueOf("wudlHbase"));
        }
        admin.disableTable(TableName.valueOf("wudlHbase"));

    }


}

猜你喜欢

转载自blog.csdn.net/wudonglianga/article/details/121722396