MRS HBase MOB特性介绍
配置场景
在实际应用中,需要存储大大小小的数据,比如图像数据、文档。小于10MB的数据一般都可以存储在HBase上,对于小于100KB的数据,HBase的读写性能是最优的。如果存放在HBase的数据大于100KB甚至到10MB大小时,插入同样个数的数据文件,但是总的数据量会很大,会导致频繁的compaction和split,占用很多CPU,磁盘IO频率很高,性能严重下降。
通过将MOB(Medium-sized Objects)数据(即100KB到10MB大小的数据)直接以HFile的格式存储在文件系统上(例如HDFS文件系统),通过expiredMobFileCleaner和Sweeper工具集中管理这些文件,然后把这些文件的地址信息及大小信息作为value存储在普通HBase的store上。这样就可以大大降低HBase的compation和split频率,提升性能。
HBase当前默认开启MOB功能,相关配置项如表1所示。如果需要使用MOB功能,用户需要在创建表或者修改表属性时在指定的列族上指定使用mob方式存储数据。
配置描述
为了开启HBase MOB功能,用户需要在创建表或者修改表属性时在指定的列族上指定使用mob方式存储数据。
使用代码声明使用mob存储的方式:
HColumnDescriptor hcd = new HColumnDescriptor("f");
hcd.setMobEnabled(true);
使用shell声明使用mob的方式,MOB_THRESHOLD单位是字节:
hbase(main):009:0> create 't3',{NAME => 'd', MOB_THRESHOLD => '102400', IS_MOB => 'true'}
0 row(s) in 0.3450 seconds
=> Hbase::Table - t3
hbase(main):010:0> describe 't3'
Table t3 is ENABLED
t3
COLUMN FAMILIES DESCRIPTION
{NAME => 'd', MOB_THRESHOLD => '102400', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE',
TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW',
IN_MEMORY => 'false', IS_MOB => 'true', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}
1 row(s) in 0.0170 seconds
参数入口:
在MRS Manager系统中,选择“集群 > 待操作集群的名称 > 服务 > HBase > 配置”,单击“全部配置”。在搜索框中输入参数名称。
表一参数描述
参数 |
描述 | 默认值 |
---|---|---|
hbase.mob.file.cache.size | 缓存打开文件句柄的个数。如果该值设置的比较大,cache可以缓存更多的文件句柄,从而降低打开关闭文件的频率。但是如果该值设置过大会导致打开的文件句柄数过多。默认值是:“1000”。此参数在服务端ResionServer上配置。 |
1000 |
hbase.mob.cache.evict.period | mob cache回收缓存的mob文件的周期,默认是3600s。 |
3600 |
hbase.mob.cache.evict.remain.ratio | mob cache回收之后保留的文件个数占cache容量个数的比例,当缓存的文件个数超过hbase.mob.file.cache.size设置的值之后会触发mob cache回收。 | 0.5 |
hbase.master.mob.ttl.cleaner.period | ExpiredMobFileCleanerChore的执行周期,以秒为单位。默认值是一天(86400秒)。
说明:
如果生存时间值过期了,即文件从创建起已经超过了24小时,则MOB文件将会被过期mob文件清理工具删除。 |
86400 |
HBase MOB数据的写入与普通HBase数据的写入没有什么区别,对客户来说是透明的。为了使用HBase MOB功能需要在“hbase-site.xml”中添加HBase MOB相关的配置项,具体请参见《组件操作指南》中“配置MOB”章节,除此之外还需要在指定column family上开启MOB功能,样例代码如下:
代码样例
以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testCreateMOBTable方法中。
public void testCreateMOBTable() {
LOG.info("Entering testCreateMOBTable.");
Admin admin = null;
try {
// Create Admin instance
admin = conn.getAdmin();
HTableDescriptor tabDescriptor = new HTableDescriptor(tableName);
HColumnDescriptor mob = new HColumnDescriptor("mobcf");
// Open mob function
mob.setMobEnabled(true);
// Set mob threshold
mob.setMobThreshold(10L);
tabDescriptor.addFamily(mob);
admin.createTable(tabDescriptor);
LOG.info("MOB Table is created successfully.");
} catch (Exception e) {
LOG.error("MOB Table is created failed " ,e);
} finally {
if (admin != null) {
try {
// Close the Admin object.
admin.close();
} catch (IOException e) {
LOG.error("Close admin failed " ,e);
}
}
}
LOG.info("Exiting testCreateMOBTable.");
}
样例:用Put接口写入MOB数据
public void testMOBDataInsertion() {
LOG.info("Entering testMOBDataInsertion.");
Table table = null;
try {
// set row name to "row"
Put p = new Put(Bytes.toBytes("row"));
byte[] value = new byte[1000];
// set the column value of column family mobcf with the value of "cf1"
p.addColumn(Bytes.toBytes("mobcf"), Bytes.toBytes("cf1"), value);
// get the table object represent table tableName
table = conn.getTable(tableName);
// put data
table.put(p);
LOG.info("MOB data inserted successfully.");
} catch (Exception e) {
LOG.error("MOB data inserted failed " ,e);
} finally {
if(table != null){
try {
table.close();
} catch (Exception e2) {
LOG.error("Close table failed " ,e);
}
}
}
LOG.info("Exiting testMOBDataInsertion.");
}
注意事项
不允许多个线程在同一时间共用同一个HTable实例。HTable是一个非线程安全类,因此,同一个HTable实例,不应该被多个线程同时使用,否则可能会带来并发问题。
HBase MOB数据读出与普通HBase数据的读出没有什么区别,对客户来说是透明的。为了使用HBase MOB功能需要在“hbase-site.xml”中添加HBase MOB相关的配置项,具体请参见《组件操作指南》中“配置MOB”章节,除此之外还需要在指定column family上开启MOB功能。
代码样例
以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testMOBDataRead方法中。
public void testMOBDataRead() {
LOG.info("Entering testMOBDataRead.");
ResultScanner scanner = null;
Table table = null;
Admin admin = null;
try {
// get table object representing table tableName
table = conn.getTable(tableName);
admin = conn.getAdmin();
admin.flush(table.getName());
Scan scan = new Scan();
// get table scanner
scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("mobcf"),
Bytes.toBytes("cf1"));
String string = Bytes.toString(value);
LOG.info("value:" + string);
}
LOG.info("MOB data read successfully.");
} catch (Exception e) {
LOG.error("MOB data read failed " ,e);
} finally {
if(scanner != null){
scanner.close();
}
if (table != null) {
try {
// Close table object
table.close();
} catch (IOException e) {
LOG.error("Close table failed " ,e);
}
}
if (admin != null) {
try {
// Close the Admin object.
admin.close();
} catch (IOException e) {
LOG.error("Close admin failed " ,e);
}
}
}
LOG.info("Exiting testMOBDataRead.");
}
- 点赞
- 收藏
- 关注作者
评论(0)