HBase Filter使用方法(二)

二、Dedicated Filtersdom

        2.1 SingleColumnValueFilter √ide

      2.2 SingleColumnValueExcludeFilter √函数

      2.3 PrefixFilter √spa

      2.4 PageFilter √code

      2.5 KeyOnlyFilter √ci

      2.6 FirstKeyOnlyFilter √get

      2.7 TimestampsFilter ×io

      2.8 RandomRowFilter √table

2.1   SingleColumnValueFilterast

             例子:Filter filter=new SingleColumnValueExcludeFilter(Bytes.toBytes(Family), Bytes.toBytes(Qualifier), CompareOp.EQUAL, Bytes.toBytes(Value));

       2.2   SingleColumnValueExcludeFilter

               使用:跟singlecolumnvaluefilter正好相反,这个是显示表中除了过滤的这条之外的全部数据 

               例子:

1

2

Filter filter=new SingleColumnValueExcludeFilter(Bytes.toBytes(Family), Bytes.toBytes(Qualifier), CompareOp.EQUAL, Bytes.toBytes(Value));

((SingleColumnValueExcludeFilter) filter).setFilterIfMissing(true);

注意:!须要加((SingleColumnValueExcludeFilter) filter).setFilterIfMissing(true); 

2.3   PrefixFilter   和ColumnPrefixFilter    

              使用:根据Row或Column的前缀取数据

              例子:Filter filter=new PrefixFilter(Bytes.toBytes("r"));

                    取出RowKey以r开头的全部数据

2.4    PageFilter

               经过设置pageside返回每一页page的数量

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

final byte[] POSTFIX = new byte[] { 0x00 };  

        HTable table;

try {

table = new HTable(config, tablename);

Filter filter = new PageFilter(pageside);  

        byte[] lastRow = null;  

        int totalRows = 0;  

        while (true) {  

            Scan scan = new Scan();  

            scan.setFilter(filter);  

            if(lastRow != null){  

                //注意这里添加了POSTFIX操做,否则死循环了  

                byte[] startRow = Bytes.add(lastRow,POSTFIX);  

                scan.setStartRow(startRow);  

            }  

            ResultScanner scanner = table.getScanner(scan);  

            int localRows = 0;  

            Result result;  

            while((result = scanner.next()) != null){  

                System.out.println(localRows++ + ":" + result);  

                totalRows ++;  

                lastRow = result.getRow();  

            }  

            scanner.close();  

            if(localRows == 0break;  

        }  

        System.out.println("total rows:" + totalRows);  

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

2.5    KeyOnlyFilter

              

         * 通常与其余过滤器配合使用

 * Filter:KeyOnlyFilter(boolean lenAsVal)

 * lenAsVal默认为假,表示不把val的长度做为val。不然val的长度将做为val输出。

 * 键过滤器能够简单的设置过滤的结果集中只包含键而忽略值,这里有一个选项能够把结果集的值保存为值的长度

             例子:Filter filter = new KeyOnlyFilter(false);  

2.6    firstkeyonlyFilter

            用法:同上,但仅会返回相同key的第一条kv

       2.8    RandomRowFilter  

              随即的返回row的数据,构造函数为

              RandomRowFilter(float chance)  

              chance取值为0到1.0,若是<0则为空,若是>1则包含全部的行。

              例子:Filter filter=new RandomRowFilter(0.5f)

相关文章
相关标签/搜索