您的位置:首页 > 其它

HBase分享系列(3)--读取细节

2015-08-04 16:45 309 查看
一、布隆过滤器

1、检索机制

2、优点

(1)节省存储空间

(2)节省定位时间

3、缺点

(1)有误差

(2)有删除困难

布隆过滤可以每列族单独启用。

使用 HColumnDescriptor.setBloomFilterType(NONE | ROW | ROWCOL) 对列族单独启用布隆。

Default = NONE 没有布隆过滤。

对 ROW,行键的哈希在每次插入行时将被添加到布隆。

对 ROWCOL,行键 + 列族 + 列族修饰的哈希将在每次插入行时添加到布隆。

二、性能相关

1、建表时预先创建region

2、关闭客户端写入自动刷新

3、不需要返回结果

4、多线程写入

5、设置一个版本

6、添加行级BloomFilter

7、使用查询过滤器

8、设置读缓存

三、客户端API

1、建表

Configuration conf = HBaseConfiguration.create();

HBaseAdmin admin = new HBaseAdmin(conf);

HTableDescriptor tableDesc = new HTableDescriptor(“test”);

HColumnDescriptor cf = new HColumnDescriptor(“cf”);//列族

// 为该列族添加行级过滤器

cf.setBloomFilterType(BloomType.ROW);

cf.setMaxVersions(1); // 只存一个版本

tableDesc.addFamily(cf); // 添加列族

byte[][]splits = getSumObjSplits();//创建预分区—字节数组

admin.createTable( tableDesc );

2、分区

static byte[][] getSumObjSplits(){

String key1 = new String(“1”);

String key2 = new String(“2”);

String key3 = new String(“3”);

String key4 = new String(“4”);

String key5 = new String(“5”);

String key6 = new String(“6”);

String key7 = new String(“7”);

String key8 = new String(“8”);

String key9 = new String(“9”);

byte[][] splits = new byte[9][];

splits[0] = key1.getBytes();

splits[1] = key2.getBytes();

splits[2] = key3.getBytes();

splits[3] = key4.getBytes();

splits[4] = key5.getBytes();

splits[5] = key6.getBytes();

splits[6] = key7.getBytes();

splits[7] = key8.getBytes();

splits[8] = key9.getBytes();

return splits;

}

3、使用过滤器

Scan s = new Scan();

ResultScanner rs = null;

Result r = null;

FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);//过滤器列表

PrefixFilter preFilter = new PrefixFilter(Bytes.toBytes(RmpedId));//前缀过滤器

FamilyFilter familyFilter = new FamilyFilter(CompareOp.EQUAL,

new BinaryComparator(Bytes.toBytes(“cf”));

filterList.addFilter(preFilter);

filterList.addFilter(familyFilter);

s.setFilter(filterList);

s.setStartRow(Bytes.toBytes(RowKeyGenerator.getLeftPadedMpedId(mpedId) + HbaseServiceTool.getMinString(yestoday)));

s.setStopRow(Bytes.toBytes(RowKeyGenerator.getLeftPadedMpedId(mpedId) + HbaseServiceTool.getMinString(tomorrow)));

s.setCacheBlocks(true);//设置读缓存

s.setCaching(2000);

4、类型转换(这部分内容有些引用自网络,原始出处没有记录。)

使用Java中的String类方法

String ->byte[]

String s = “你好中国”;

byte[] b = s.getBytes();

byte[]->String

String s = new String( b );

使用HBase中的Bytes类方法

String ->byte[]

String s = “你好中国”;

byte[] b = Byte.toBytes( s );

byte[]->String

String s = Bytes.toString( b );

注意:

如果没有中文,这两种方式转换效果是一样的。对中文而言,最好采用第二种方式转换,否则可能出现由于编码方式不一样而导致乱码问题。

int型、long型、char型等数字形式,直接使用第二种方式会省空间,否则int型写入4个字节、long型8字节等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: