hbase - scan - filter 示例

public static void main(String[] args) throws IOException {
        //Scan類常用方法說明
        //指定需要的family或column ,如果沒有調用任何addFamily或Column,會返回所有的columns; 
        // scan.addFamily(); 
        // scan.addColumn();
        // scan.setMaxVersions(); //指定最大的版本個數。如果不帶任何參數調用setMaxVersions,表示取所有的版本。如果不掉用setMaxVersions,只會取到最新的版本.
        // scan.setTimeRange(); //指定最大的時間戳和最小的時間戳,只有在此範圍內的cell才能被獲取.
        // scan.setTimeStamp(); //指定時間戳
        // scan.setFilter(); //指定Filter來過濾掉不需要的信息
        // scan.setStartRow(); //指定開始的行。如果不調用,則從表頭開始;
        // scan.setStopRow(); //指定結束的行(不含此行);
        // scan.setBatch(); //指定最多返回的Cell數目。用於防止一行中有過多的數據,導致OutofMemory錯誤。
        
        //過濾器
        //1、FilterList代表一個過濾器列表
        //FilterList.Operator.MUST_PASS_ALL -->and
        //FilterList.Operator.MUST_PASS_ONE -->or
        //eg、FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        //2、SingleColumnValueFilter
        //3、ColumnPrefixFilter用於指定列名前綴值相等
        //4、MultipleColumnPrefixFilter和ColumnPrefixFilter行爲差不多,但可以指定多個前綴。
        //5、QualifierFilter是基於列名的過濾器。
        //6、RowFilter
        //7、RegexStringComparator是支持正則表達式的比較器。
        //8、SubstringComparator用於檢測一個子串是否存在於值中,大小寫不敏感。
        
        HTable table=(HTable) getHTablePool().getTable("tb_stu");
        Scan scan=new Scan();
        scan.setMaxVersions();
        //指定最多返回的Cell數目。用於防止一行中有過多的數據,導致OutofMemory錯誤。
        scan.setBatch(1000);

        //scan.setTimeStamp(NumberUtils.toLong("1370336286283"));
        //scan.setTimeRange(NumberUtils.toLong("1370336286283"), NumberUtils.toLong("1370336337163"));
        //scan.setStartRow(Bytes.toBytes("quanzhou"));
        //scan.setStopRow(Bytes.toBytes("xiamen"));
        //scan.addFamily(Bytes.toBytes("info")); 
        //scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("id"));
        
        //查詢列鏃爲info,列id值爲1的記錄
        //方法一(單個查詢)
        // Filter filter = new SingleColumnValueFilter(
        //         Bytes.toBytes("info"), Bytes.toBytes("id"), CompareOp.EQUAL, Bytes.toBytes("1")); 
        // scan.setFilter(filter);
        
        //方法二(組合查詢)
        //FilterList filterList=new FilterList();
        //Filter filter = new SingleColumnValueFilter(
        //    Bytes.toBytes("info"), Bytes.toBytes("id"), CompareOp.EQUAL, Bytes.toBytes("1"));
        //filterList.addFilter(filter);
        //scan.setFilter(filterList);
        
        ResultScanner rs = table.getScanner(scan);
        
        for (Result r : rs) {
            for (KeyValue kv : r.raw()) {
                System.out.println(String.format("row:%s, family:%s, qualifier:%s, qualifiervalue:%s, timestamp:%s.", 
                        Bytes.toString(kv.getRow()), 
                        Bytes.toString(kv.getFamily()), 
                        Bytes.toString(kv.getQualifier()), 
                        Bytes.toString(kv.getValue()),
                        kv.getTimestamp()));
            }
        }
        
        rs.close();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章