要点
-
掌握HBase的命名空间namespace概念
-
掌握HBase数据版本确界
-
掌握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);//针对某一个列族下面所有的列设置TTLf1.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();}
}