hbase-05 namespace、数据的确界&TTL

要点

  1. 掌握HBase的命名空间namespace概念

  2. 掌握HBase数据版本确界

  3. 掌握HBase数据TTL

1. HBase的namespace

1.1 namespace基本介绍

  • 在HBase中,namespace命名空间指对一组表的逻辑分组,类似RDBMS中的database,方便对表在业务上划分。
  • Apache HBase从0.98.0, 0.95.2两个版本号开始支持namespace级别的授权操作,HBase全局管理员能够创建、改动和回收namespace的授权。

1.2 namespace的作用

  • 配额管理:限制一个namespace可以使用的资源,包括region和table

  • 命名空间安全管理:提供了另一个层面的多租户安全管理

  • Region服务器组:一个命名或一张表,可以被固定到一组RegionServers上,从而保证了数据隔离性

1.3 namespace的基本操作

复制代码
创建namespace
hbase>create_namespace 'nametest'  

查看namespace
hbase>describe_namespace 'nametest'  

列出所有namespace
hbase>list_namespace  

在namespace下创建表
hbase>create 'nametest:testtable', 'fm1' 

查看namespace下的表
hbase>list_namespace_tables 'nametest'  

删除namespace
hbase>drop_namespace 'nametest'  

2. HBase的数据版本的确界以及TTL

2.1 数据的确界

  • 在HBase当中,我们可以为数据设置上界和下界,其实就是定义数据的历史版本保留多少个,通过自定义历史版本保存的数量,我们可以实现数据多个历史版本的数据查询

  • 版本的下界

    • 默认的版本下界是0,即禁用。row版本使用的最小数目是与生存时间(TTL Time To Live)相结合的,并且我们根据实际需求可以有0或更多的版本,使用0,即只有1个版本的值写入cell。
  • 版本的上界

    • 之前默认的版本上界是3,也就是一个row保留3个副本(基于时间戳的插入)。
    • 该值不要设计的过大,一般的业务不会超过100。如果cell中存储的数据版本号超过了3个,再次插入数据时,最新的值会将最老的值覆盖。(现版本已默认为1)

2.2 数据的TTL

  • 在实际工作当中经常会遇到有些数据过了一段时间我们可能就不需要了,那么这时候我们可以使用定时任务去定时的删除这些数据

  • 或者我们也可以使用Hbase的TTL(Time To Live)功能,让我们的数据定期的会进行清除

  • 使用代码来设置数据的确界以及设置数据的TTL如下

2.2.1 创建maven工程

  • 创建maven工程,导入jar包坐标
xml 复制代码
<repositories>
    <repository>
        <id>cloudera</id>
        <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.6.0-mr1-cdh5.14.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>1.2.0-cdh5.14.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-server</artifactId>
        <version>1.2.0-cdh5.14.2</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.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
                <!--    <verbal>true</verbal>-->
            </configuration>
        </plugin>
        <!--将我们其他用到的一些jar包全部都打包进来  -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <minimizeJar>false</minimizeJar>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2.2.2 代码开发

java 复制代码
public class HBaseVersionAndTTL {
    public static void main(String[] args) throws IOException, InterruptedException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","node01,node02,node03");
        Connection connection = ConnectionFactory.createConnection();
        Admin admin = connection.getAdmin();
        if(!admin.tableExists(TableName.valueOf("version_hbase"))){
            HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("version_hbase"));
            HColumnDescriptor f1 = new HColumnDescriptor("f1");
            f1.setMinVersions(3);
            f1.setMaxVersions(5);
            //针对某一个列族下面所有的列设置TTL
            f1.setTimeToLive(30);
            hTableDescriptor.addFamily(f1);
            admin.createTable(hTableDescriptor);
        }
        Table version_hbase = connection.getTable(TableName.valueOf("version_hbase"));
        Put put = new Put("1".getBytes());
        //针对某一条具体的数据设置TTL
        //put.setTTL(3000);
        put.addColumn("f1".getBytes(),"name".getBytes(),System.currentTimeMillis(),"zhangsan".getBytes());
        version_hbase.put(put);
        Thread.sleep(1000);
        Put put2 = new Put("1".getBytes());
        put2.addColumn("f1".getBytes(),"name".getBytes(),System.currentTimeMillis(),"zhangsan2".getBytes());
        version_hbase.put(put2);
        Get get = new Get("1".getBytes());
        get.setMaxVersions();
        Result result = version_hbase.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
        }
        version_hbase.close();
        connection.close();
    }
}
相关推荐
逆境不可逃几秒前
【与我学 ClaudeCode】规划与协调篇 之 Skills:按需加载的领域知识框架
大数据·人工智能·elasticsearch·搜索引擎·agent·claudecode
大信说财务2 分钟前
发票管理的技术底座:从架构支撑到可信归档
大数据·软件·财务管理·数电票·发票
一切皆是因缘际会4 分钟前
从概率拟合到内生心智:七层投影架构重构AGI数字生命新范式
大数据·数据结构·人工智能·重构·架构·agi
维双云6 分钟前
互联网新手建站哪家好?新手第一次做网站,先别急着选品牌
大数据
戴国进13 分钟前
详解Git的worktree实现多分支并行开发
大数据·git
技术不好的崎鸣同学15 分钟前
信息安全工程师之《网络安全体系与网络安全模型》
大数据·安全·web安全
Chockmans15 分钟前
春秋云境CVE-2022-32992(文件上传和sql注入)保姆级教学
数据库·sql·安全·网络安全·网络攻击模型·春秋云境·cve-2022-32992
muddjsv16 分钟前
SQL 语句:从产生、发展到内容全景
数据库·sql
ZC跨境爬虫18 分钟前
跟着 MDN 学CSS day_6:(伪类和伪元素详解)
前端·javascript·css·数据库·ui·html