HBase 中有没有一种方法可以计算匹配 rowkey-search 的行

Is there a way in HBase to COUNT rows matching rowkey-search

假设我的 Rowkey 有两部分 (NUM1~NUM2)。

我想通过 Rowkey 的第一部分做一个计数组。有没有办法在 HBase 中做到这一点?

我总是可以将它作为一项 M/R 作业读取所有行、组、计数...但我想知道是否有办法在 HBase 中做到这一点?

您可以在 hbase 中使用 RegexStringComparator shell

hbase(main):003:0> import org.apache.hadoop.hbase.filter.RegexStringComparator
hbase(main):006:0> scan 'test', {FILTER => org.apache.hadoop.hbase.filter.RowFilter.new(CompareFilter::CompareOp.valueOf('EQUAL'),RegexStringComparator.new("NUM1*"))}

选项 1:

你可以使用 prefix filter....类似下面的东西。

前缀过滤器:

This filter takes one argument a prefix of a row key. It returns only those key-values present in a row that starts with the specified row prefix

Syntax

PrefixFilter (<row_prefix>)

同样可以与 java 客户端一起使用

使用 Hbase 的示例 shell:

scan 'yourtable', {FILTER => "PrefixFilter('12345|abc|50|2016-05-05')"}

scan 'yourtable', {STARTROW=>'12345' FILTER => "PrefixFilter('2016-05-05 08:10:10')"}

根据您的要求...

注意:java hbase 扫描 api 如果您想从 java

进行扫描,也有相同的方法

选项 2:

FuzzyRowFilter(see hbase-the-definitive) This is really useful in our case We have used bulk clients like map-reduce as well as standalone hbase clients

此过滤器以模糊方式作用于行键。它需要一个应返回的行键列表,以及一个附带的 byte[] 数组,该数组表示行键中每个字节的重要性。构造函数是这样的:

FuzzyRowFilter(List<Pair<byte[], byte[]>> fuzzyKeysData)

fuzzyKeysData 通过取两个值之一来指定行键字节的重要性:

0 Indicates that the byte at the same position in the row key must match as-is. 1 Means that the corresponding row key byte does not matter and is always accepted.

* 示例:部分行键匹配 * 一个可能的例子是匹配部分键,但不是从左到右,而是在复合键内的某个地方。假设行键格式为_,具有固定长度的部分,其中是4,是2,是4,是2个字节长。该应用程序现在请求在任何一年的 1 月执行特定操作(编码为 99)的所有用户。那么行键和模糊数据对如下:

行键 “????99????_01”,其中“?”是一个任意字符,因为它被忽略了。 模糊数据 =“\x01\x01\x01\x01\x00\x00\x00\x00\x01\x01\x01\x01\x00\x00\x00” 换句话说,模糊数据数组指示过滤器找到所有匹配“????99????_01”的行键,其中“?”将接受任何字符。

此过滤器的一个优点是它可能会在匹配行键结束时计算下一个匹配行键。它实现了 getNextCellHint() 方法来帮助 fast-forwarding 中的服务器找到可能匹配的下一个行范围。这加快了扫描速度,尤其是当跳过的范围非常大时。示例 4-12 使用过滤器从测试数据集中抓取特定行。

按列前缀过滤的示例

List<Pair<byte[], byte[]>> keys = new ArrayList<Pair<byte[], byte[]>>();
keys.add(new Pair<byte[], byte[]>(
  Bytes.toBytes("row-?5"), new byte[] { 0, 0, 0, 0, 1, 0 }));
Filter filter = new FuzzyRowFilter(keys);

Scan scan = new Scan()
  .addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-5"))
  .setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
  System.out.println(result);
}
scanner.close();

示例代码还在扫描中添加了过滤列,只是为了保持输出简短:

正在向 table 添加行... 扫描结果:

keyvalues={row-05/colfam1:col-01/1/Put/vlen=9/seqid=0,
           row-05/colfam1:col-02/2/Put/vlen=9/seqid=0,
           ...
           row-05/colfam1:col-09/9/Put/vlen=9/seqid=0,
           row-05/colfam1:col-10/10/Put/vlen=9/seqid=0}
keyvalues={row-15/colfam1:col-01/1/Put/vlen=9/seqid=0,
           row-15/colfam1:col-02/2/Put/vlen=9/seqid=0,
           ...
           row-15/colfam1:col-09/9/Put/vlen=9/seqid=0,
           row-15/colfam1:col-10/10/Put/vlen=9/seqid=0}

测试代码接线向table添加20行,命名为row-01到row-20。我们想要检索与模式 row-?5 匹配的所有行,换句话说,所有以数字 5 结尾的行。上面的输出确认了正确的结果。