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包坐标
<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 代码开发

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();
    }
}
清空HBase数据有多种方法: 1. **使用`delete_all`命令**:在HBase Shell中,执行`delete_all 'your_table_name'`可以清空表中的所有数据,此方法能在保留表结构的情况下清空数据。示例代码如下: ```bash # 执行 HBase Shell hbase shell # 清空表中的所有数据 delete_all 'your_table_name' ``` 2. **使用`truncate`或`truncate_preserve`命令**:`truncate 'namespace:tableName'`会把表分区也清除掉;`truncate_preserve 'namespace:tableName'`只清除数据,保留预分区。示例如下: ```bash # 执行 HBase Shell hbase shell # 清空表数据并清除分区 truncate 'namespace:tableName' # 只清空表数据,保留预分区 truncate_preserve 'namespace:tableName' ``` 3. **设置表的TTL进行数据清理**:对于因建表时未设置数据保存日期,导致表数据量大,需要清理很久之前数据的情况,可以通过设置表的TTL,然后通过系统触发major compact进行数据清理。步骤如下: - 查看ttl是forever的表:`desc '表名'` - 备份表:`snapshot '表名',‘快照名’` - 停用表:`disable '表名'` - 给表添加ttl:`alter '表名' , {NAME=>'列簇',TTL=>'604800'}` - 开启表:`enable '表名'` 清理脚本示例: ```bash #!/bin/bash # clean logs rm -rf /root/hbase-operator/logs/*.log while IFS=, read -r line; do echo "desc '$line'"|/root/hbase-1.3.1-bin/hbase/bin/hbase shell &>/root/hbase-operator/logs/$line.log 2>&1 done < "/root/hbase-operator/tables/2023_tables.dat" ``` 4. **删除具体数据**: - 删除特定的单元格数据:`delete 'emp','row','column name','time stramp'`,例如删除emp表中第二行personal data:name列、时间节点为1502182102866的记录:`delete 'emp','2','personal data:name',1502182102866` - 删除表中的所有单元格:`deleteall 'tablename','row'`,例如删除emp表中第二行数据:`deleteall 'emp','2'` [^1][^3][^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小技工丨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值