HBase 创建表预分区

来源:互联网 发布:ubuntu server配置 编辑:程序博客网 时间:2024/06/16 08:51

分区

HBase中,表会被划分为1…n个Region,被托管在RegionServer中。Region二个重要的属性:StartKey与 EndKey表示这个Region维护的rowKey范围,当我们要读/写数据时,如果rowKey落在某个start-end key范围内,那么就会定位到目标region并且读/写到相关的数据。
默认地,当我们只是通过HBaseAdmin指定TableDescriptor来创建一张表时,start-end key无边界,region的size越来越大时,大到 一定的阀值,就会找到一个midKey将region一分为二,成为2个region,这个过 程称为分裂(region-split).而midKey则为这二个region的临界

缺点

1.总是往最大start-key的region写记录,之前分裂出来的region不会再被写数据,它们都处于半满状态
2.split是比较耗时耗资源

优点

合理设计rowkey 能让各个region 的并发请求 平均分配(趋于均匀) 使IO 效率达到最高
(预分区需要将hbase.hregion.max.filesize设置一个较大的值,默认是10G(0.94.3 ) 也就是说单个region 默认大小是10G)

预分区

shell

指明分割点

create 't1','f1' ,SPLITS=>['10','20','30','40']

HexStringSplit指明分割策略,-c 10指明要分割的区域数量,-f指明表中的列族,用“:”分割。

hbase org.apache.hadoop.hbase.util.RegionSplitter test_table HexStringSplit -c 10 -f f1

根据文件创建分区并压缩

create 'split_table_test',{NAME =>'cf', COMPRESSION => 'SNAPPY'}, {SPLITS_FILE => 'region_split_info.txt'}  

COMPRESSION 默认值NONE,即不使用压缩
建议采用SNAPPY压缩

在Web(master:16010)监控查看
这里写图片描述

官方文档给出的建表提示

Here is some help for this command:Creates a table. Pass a table name, and a set of column familyspecifications (at least one), and, optionally(可选地), table configuration.Column specification(规格) can be a simple string (name), or a dictionary(dictionaries are described below in main help output), necessarily including NAME attribute. Examples:Create a table with namespace=ns1 and table qualifier=t1  hbase> create 'ns1:t1', {NAME => 'f1', VERSIONS => 5}Create a table with namespace=default and table qualifier=t1  hbase> create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}  hbase> # The above in shorthand would be the following:  hbase> create 't1', 'f1', 'f2', 'f3'  hbase> create 't1', {NAME => 'f1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}  hbase> create 't1', {NAME => 'f1', CONFIGURATION => {'hbase.hstore.blockingStoreFiles' => '10'}}

TTL 默认是 2147483647 即:Integer.MAX_VALUE 值 大概是68年,这个参数是说明该列族数据的 存活时间,单位是s,超过存过时间的数据将在表中不在显示,待下次major compact的时候再彻底删除数据,
若设置MIN_VERSIONS=>’0’ TTL时间戳过期后,将全部彻底删除该family 下所有的数据,如果MIN_VERSIONS 不等于0 那将保留最新的MIN_VERSIONS个版本的数据,其它的全部删除,比如MIN_VERSIONS=>’1’ 届时将保留一个最新版本的数据,其它版本的数据将不再保存。

Table configuration options can be put at the end.Examples:  hbase> create 'ns1:t1', 'f1', SPLITS => ['10', '20', '30', '40']  hbase> create 't1', 'f1', SPLITS => ['10', '20', '30', '40']  hbase> create 't1', 'f1', SPLITS_FILE => 'splits.txt', OWNER => 'johndoe'  hbase> create 't1', {NAME => 'f1', VERSIONS => 5}, METADATA => { 'mykey' => 'myvalue' }  hbase> # Optionally pre-split the table into NUMREGIONS, using  hbase> # SPLITALGO ("HexStringSplit", "UniformSplit" or classname)  hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}  hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit', REGION_REPLICATION => 2, CONFIGURATION => {'hbase.hregion.scan.loadColumnFamiliesOnDemand' => 'true'}}

VERSIONS 默认是3,这个参数的意思是数据保留三个 版本,如果数据随时都在更新,或老版本的数据无价值,那将此参数设为1 能节约2/3的空间
RegionSplitter提供三个用于预分割的工具:HexStringSplit、SplitAlgorithm、UniformSplit。
HexStringSplit和UniformSplit是两个预定义的静态类,可以直接使用;而SplitAlgorithm是一个接口,需要开发人员自己实现相应的分隔策略。如果是以十六进制字符串作为行键rowkey或者行键rowkey的前缀是十六进制字符串,用HexStringSplit就比较合适;UniformSplit会把行键均匀地分割多个部分,如果行将rowkey是随机的字节数组,用UniformSplit就比较合适;或者开发者根据需要实现分割策略。

You can also keep around a reference to the created table:  hbase> t1 = create 't1', 'f1'Which gives you a reference to the table named 't1', on which you can thencall methods.