您的位置:首页 > 其它

Hbase 笔记(4) 客户端API高级性能

2014-11-14 10:49 309 查看
1、比较运算符

CompareFilter.CompareOp.LESS

CompareFilter.CompareOp.LESS_OR_EQUAL

CompareFilter.CompareOp.EQUAL

CompareFilter.CompareOp.NOT_EQUAL

CompareFilter.CompareOp.GREATER_OR_EQUAL

CompareFilter.CompareOp.GREATER

2、比较器

BinaryComparator : 使用Bytes.compareTo()

BinaryPrefixComparator : 使用Bytes.compareTo() 前缀匹配

NullComparator: 判断是不是Null

BitComparator: 使用And、Or、Xor 方法。只能用EQUAL、NOT_EQUAL

RegexStringComparator: 正则表达式。只能用EQUAL、NOT_EQUAL

SubstringComparator: 使用String的contains 方法。只能用EQUAL、NOT_EQUAL

3、比较过滤器

(1)、行过滤器 RowFilter extends CompareFilter

Scan scan = new Scan();
Filter filter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row-22")));
scan.setFilter(filter);
scan.addColumn(Bytes.toBytes("CF1"), Bytes.toBytes("A"));
scan.addColumn(Bytes.toBytes("CF1"), Bytes.toBytes("B"));
scan.addColumn(Bytes.toBytes("CF2"), Bytes.toBytes("C"));
scan.addColumn(Bytes.toBytes("CF2"), Bytes.toBytes("D"));
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(result);
}
scanner.close();


正则表达式:

Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator(".*-.5"));


子串匹配:

Filter filter3 = new RowFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("-5"));


(2)、列族过滤器 FamilyFilter extends CompareFilter
(3)、列名过滤器 QualifierFilter extends CompareFilter

(4)、值过滤器 ValueFilter extends CompareFilter

(5)、参考列过滤器 DependentColumnFilter extends CompareFilter

4、专用过滤器

(1)、单列值过滤器 SingleColumnValueFilter extends FilterBase

(2)、单列排除过滤器 SingleColumnValueExcludeFilter extends FilterBase

(3)、前缀过滤器 PrefixFilter extends FilterBase , 指的是行健

(4)、分页过滤器 PageFilter extends FilterBase

HConnection connection = HConnectionManager.createConnection(conf);
HTableInterface table = connection.getTable("T1");

Filter filter = new PageFilter(15);
int totalRows = 0;
byte[] lastRow = null;
while (true) {
Scan scan = new Scan();
scan.setFilter(filter);
if (lastRow != null) {
byte[] startRow = Bytes.add(lastRow, Bytes.toBytes("1"));
scan.setStartRow(startRow);
System.out.println("startRow = " + (startRow == null ? "null" : Bytes.toStringBinary(startRow)));
}
ResultScanner scanner = table.getScanner(scan);
int localRows = 0;
for (Result result : scanner) {
lastRow = result.getRow();
String a = Bytes.toStringBinary(result.getValue(Bytes.toBytes("CF1"), Bytes.toBytes("A")));
String b = Bytes.toStringBinary(result.getValue(Bytes.toBytes("CF1"), Bytes.toBytes("B")));
String c = Bytes.toStringBinary(result.getValue(Bytes.toBytes("CF2"), Bytes.toBytes("C")));
String d = Bytes.toStringBinary(result.getValue(Bytes.toBytes("CF2"), Bytes.toBytes("D")));
System.out.println(Bytes.toStringBinary(lastRow) + ":" + a + "\t" + b + "\t" + c + "\t" + d);
totalRows++;
localRows++;
}
scanner.close();
System.out.println("localRows = " + localRows);
if (localRows == 0) {
break;
}
}
System.out.println("totalRows = " + totalRows);


输出:

...............................................................

startRow = 14158629376111

1415862937612:414 640
825 158

1415862937613:493 546
309 995

1415862937614:454 809
721 12

1415862937615:584 212
59 102

1415862937616:547 41
79 822

1415862937617:542 328
667 462

1415862937618:136 584
924 733

1415862937619:273 168
955 625

1415862937620:62 70
878 826

1415862937621:797 623
335 373

1415862937622:665 1
407 711

1415862937623:839 440
240 934

1415862937624:81 690
707 793

1415862937625:771 545
470 73

1415862937626:272 104
116 875

localRows = 15

startRow = 14158629376261

1415862937627:890 720
832 180

1415862937628:149 552
435 772

1415862937629:30 797
998 922

1415862937630:955 886
243 264

1415862937631:729 952
261 271

1415862937632:218 986
103 193

1415862937633:981 331
668 843

1415862937634:782 721
794 512

localRows = 8

startRow = 14158629376341

localRows = 0

totalRows = 698

(5)、行健过滤器 KeyOnlyFilter extends FilterBase

(6)、首次行健过滤器 FirstKeyOnlyFilter extends FilterBase

(7)、包含结束过滤器 InclusiveStopFilter extends FilterBase

(8)、时间戳过滤器 TimestampFilter extends FilterBase

(9)、列计数过滤器 ColumnCountFilter extends FilterBase

(10)、列分页过滤器 ColumnPagingFilter extends FilterBase

看起来不是很有用

(11)、列前缀过滤器 ColumnPrefixFilter extends FilterBase

(12)、随机行过滤器 RandomFilter extends FilterBase

5、附加过滤器

(1)、跳转过滤器 SkipFilter extends FilterBase

当过滤器发现某一行中的一列需要过滤时,这一行行数据都将过滤掉。

(2)、全匹配过滤器 WhileMatchFilter extends FilterBase

逐渐遍历数据,当其中一条数据被过滤时,将直接放弃本次扫描结果,而在放弃之前的结果操作是有效的。



6、过滤器List


多个过滤器连用:

(1)、过滤某一个条件(MUST_PASS_ONE) 或者所有条件(MUST_PASS_ALL)

(2)、使用ArrayList可以保证Filter顺序

List<Filter>  filters = new ArrayList<Filter>();
filters.add(filter1);
filters.add(filter2);
filters.add(filter3);
filters.add(filter4);
filters.add(filter5);

FilterList filterList1 = new FilterList(Operator.MUST_PASS_ALL,filters);
FilterList filterList2 = new FilterList(Operator.MUST_PASS_ONE,filters);


7、自定义过滤器

实现Filter 或者继承 FilterBase

ReturnCode 的类型:

INCLUDE、SKIP、NEXT_COL、NEXT_ROW、SEEK_NEXT_USING_HINT

过滤器兼容性:

不能使用Batch 的 : DependentColumnFilter,FilterList视情况而定

不用被SkipFilter包装的: PrefixFilterPageFilterInclusiveStopFilter,(SkipFilter、WhileMatchFilter、FilterList)视情况而定

不用被WhileMatchFilter包装的:(SkipFilter、WhileMatchFilter、FilterList)视情况而定

不能在Get中使用的: RowFilter,SingleColumnValueFilter,SingleColumnValueExcludeFilter,PrefixFilter,PageFilter,InclusiveStopFilter,RandomFilter,SkipFilter,WhileMatchFilter

不能在Scan中使用的:ColumnCountFilter

为了优化性能可以提前结束扫描的:RowFilter,PrefixFilter,PageFilter,InclusiveStopFilter, WhileMatchFilter,FilterList视情况而定

所有Filter 都可以加入 FilterList

8、计数器

(1)、单计数器

(2)、多计数器

9、协处理器

(1)、基础

协处理器允许用户在Region服务器上运行自己的代码。

使用场景:维护辅助索引、维护数据完整性、聚合函数sum、avg()等、SQL。

类型:

observer,回调函数在一些特定事件发生时候被执行,类似触发器;

endpoint,添加一些RPC来动态扩展RPC协议,类似存储过程。

(2)、Coprocessor接口

需实现方法 public void start(CoprocessorEnvironment ce), public void stop (CoprocessorEnvironment ce) 方法。

协处理器执行顺序:先系统级协处理器,后用户级协处理器,按照加载顺序执行。

(3)、协处理器加载

a、hbase-site.xml 中配置属性 hbase.coprocessor.region.classes, hbase.coprocessor.master.classes, hbase.coprocessor.wal.classes

b、从表描述符加载:,格式:<path-to-jar>|<className>|<SYSTEM or USER>。代码中设置属性 COPROCESSOR$1

(4)、RegionObserver

a、处理 Region 生命周期事件

pending open 状态 : preOpen / postOpen, preWALRestore / postWALRestore,

open 状态 : preFlush / postFlush, preCompact / postCompact, preSplit / postSplit,

pending close 状态 : preClose / postClose

b、处理客户端 API 事件: Get, Put, Delete, CheckAndPut, CheckAndDelete, GetClosestRowBefore, Exists 等等

c、RegionCoprocessorEnvironment :getRegion、getRegionServerServices、getSharedData

d、ObserverContext:获得上下文

e、BaseRegionObserver:监听协处理器的基类

(5)、MasterObserver

处理DDL操作,如 preCreateTable / postCreateTable, preModifyTable / postModifyTable 等

(6)、endpoint

在各个region之间操作

10、HTablePool

用法:

Configuration conf = HBaseConfiguration.create();
int maxSize = 100;
HTablePool pool = new HTablePool(conf, maxSize);
//............
String strTableName = "T1";
HTableInterface table = pool.getTable(strTableName);
// CRUD
table.close();
//.............
pool.close();


Hbase 0.98 将HTablePool废除, 新版用法:

Configuration conf = HBaseConfiguration.create();
HConnection connection = HConnectionManager.createConnection(conf);
//..............
HTableInterface table = connection.getTable("T1");
//CRUD
table.close();
//.............
connection.close();


11、连接管理

HConnection 细节:共享ZooKeeper 连接,缓存通用资源

好处:缓存-ROOT-、和 .META. 表信息,resion地址定位,减少网络调用次数。HTable多个实例共享一个连接。

缺点: 如果用户不显示关系连接,其将一直存在,直到客户端退出。将耗尽连接句柄、内存,IO异常。故必须调用close显示关闭连接。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: