hbase shell and filter operation java api


A filter operation of hbase shell
1. no filter, a full table scan
    scan 'table' // check out all the data records in a table
    for example: scan 'upos_city_qh_yushu: tb_detail_userloc_outdoor_22180822'
    Query results are as follows (one of which is taken contents of the line keys):

 because hbase shell script operation is very inconvenient, and does not support viewing context, for us it is very user friendly, so for query operations, such as we use the following operation "

For example: echo "scan 'upos_city_qh_yushu: tb_detail_userloc_outdoor_20180822 '" | hbase shell
results // output to the console table, the effect is equivalent to the query in hbase shell script
// Note that the script which contains "" if required on the front plus \ escape
// the following example uses two ways of example, each of the commands are verified and

2. filter in accordance with the value of value ValueFilter
    scan 'table', fILTER => "ValueFilter ( =, 'substring : value value ') "// query the records in a table column value specified character string
    Examples: upos_city_qh_yushu: tb_detail_userloc_outdoor_22180822, the FILTER =>" ValueFilter (=,' the substring: 6327 ') "
    // look-up table indicates the order name upos_city_qh_yushu: tb_detail_userloc_outdoor_22180822
    which contains the record value of 6327
 


3. Filter according to List FamilyFilter cluster
    scan 'table', FILTER => "FamilyFilter ( =, 'substring: value string')" // query ranked cluster comprises a table illustrating the recording of a string
    Examples : Scan 'upos_city_qh_yushu: tb_detail_userloc_outdoor_20180822', the FILTER => "FamilyFilter (=, 'the substring: L')"
    
    echo "Scan 'upos_city_qh_yushu: tb_detail_userloc_outdoor_20180822', the FILTER => \" FamilyFilter (=, 'the substring: L') \ "" | HBase the shell

4. follow row key filter the RowFilter
    
    a filter key travel records containing a string of data (fuzzy query).
    command: scan 'table', fILTER => "RowFilter ( =, 'substring: string value ') "
    example: Scan' upos_city_qh_yushu: tb_detail_userloc_outdoor_20180822 ', the FILTER =>" the RowFilter (=,' the substring: 3040. ') "
    
    echo" Scan "upos_city_qh_yushu:tb_detail_userloc_outdoor_20180822',FILTER=>\"RowFilter(=,'substring:3040')\"" | hbase shell
    
    . b determined in accordance with a row of keys filter (<, <=, =,>,> =)
    command: scan 'table', FILTER => "RowFilter ( =, 'binary: OK key value')"
    Example : Scan 'upos_city_qh_yushu: tb_detail_userloc_outdoor_20180822', the FILTER => "the RowFilter (=, 'binary: 00_460075097670490_1534925332480')"
    
    echo "Scan 'upos_city_qh_yushu: tb_detail_userloc_outdoor_20180822', the FILTER => \" the RowFilter (=, 'binary: 00_460075097670490_1534925332480') \ "" | hbase shell

query results are as follows: line key to all records of 00_460075097670490_1534925332480

<, <=,>,> = Empathy

    . c filtration PrefixFilter prefix key in row
    scan 'table', FILTER => "PrefixFilter ( ' OK key prefix')" key // travel query string to record a start
    scan 'upos_city_qh_yushu: tb_detail_userloc_outdoor_20180822', FILTER = > "PrefixFilter ( '00_46007509767')"
    echo "Scan 'upos_city_qh_yushu: tb_detail_userloc_outdoor_20180822', the fILTER => \" PrefixFilter ( '00_46007509767') \ "" | hbase shell

. two java api operation of filter
1. first, some filtering operation hbase Some parameters

(1) comparison CompareFilter.CompareOp
comparison operator is used to define the relationship between comparison, you can choose the value of the following categories:

EQUAL equal to
GREATER greater than
GREATER_OR_EQUAL than or equal to
LESS less than
LESS_OR_EQUAL less
NOT_EQUAL not equal


(2) comparator ByteArrayComparable
can diversify the target matching results by the comparator, the comparator can be used have the following subclasses:

BinaryComparator matching the complete byte array 
BinaryPrefixComparator matching prefix byte array 
BitComparator
NullComparator
RegexStringComparator regular expression matching
SubstringComparator substring matching


2. Set hbase connection configuration, connected acquire hbase

   /**
    * 获取hbase连接的配置
    * @param quorum  举例 : 127.0.0.1:2181
    * @return
    */
   private Configuration getConfiguration(String quorum)
   {
       Configuration conf = HBaseConfiguration.create();
       conf.set("hbase.zookeeper.quorum", quorum);
       conf.set("hbase.rootdir", "/data/hadoop/data");
       conf.set("zookeeper.znode.parent", "/hbase");
       return conf;
   }


   / **
    * Get connected hbase
    * @param the conf
    * @return
    * /
   Private Connection the getConnection (the Configuration the conf)
   {
       Connection Conn = null;
           the try 
           {
               Conn = ConnectionFactory.createConnection (the conf);
               System.out.println ( "Get connected hbase success "+ conf.get (" hbase.zookeeper.quorum "));!
           } the catch (IOException E) {
               System.out.println (" Get hbase connection failed "+ e.getMessage ());
           }
       
       return Conn;
   }

3. obtain scan the object, set the filter conditions

    = Connection.GetTable table the Table (TableName.valueOf (tableName));
    // acquired by the connecting step of the operation, and the table name of the query to obtain the desired table object
    Scan scan = new Scan (); // Get the object scan to be queried by the object
    
    may then set up a filter to filter the query:
    
    If only one filter condition, to direct the use of various filter object
    filter conditions various, using:
    FilterList new new FilterList filters = (); // filter set
    is then used scan.setFilter (filters); // add to filter into
    the final use ResultScanner rs = table.getScanner (scan); // to obtain the results set

java api filter operation:

a column-based cluster filter FamilyFilter.
Constructor:
    FamilyFilter (CompareFilter.CompareOp familyCompareOp, ByteArrayComparable familyComparator)

    = New new FamilyFilter familyFilter FamilyFilter (CompareOp.EQUAL, new new BinaryComparator (Bytes.toBytes ( "info")));   
    // return all columns cluster info data

b based on the filter QualifierFilter column.
Filter b1 based on the column name.
    Constructor:
    QualifierFilter (CompareFilter.CompareOp op, ByteArrayComparable qualifierComparator)
    example: 
    QualifierFilter qualifierFilter = new new QualifierFilter (
        CompareOp.EQUAL, new new BinaryComparator (Bytes.toBytes ( "eci")));
        // returns the column data comprising eci

. b2 based on the column name prefix filter
    constructor:
    ColumnPrefixFilter (byte [] prefix) 
     ColumnPrefixFilter columnPrefixFilter = new new ColumnPrefixFilter (Bytes.toBytes ( "i"));
     // return all of the data column name beginning with i

. b3 filter based on the plurality of column names prefixes MultipleColumnPrefixFilter
    byte [] [] bytes = new new byte [] [] {Bytes.toBytes ( "i"), Bytes.toBytes ( "ECI")};
          
    MultipleColumnPrefixFilter multipleColumnPrefixFilter = new new MultipleColumnPrefixFilter (bytes);

  // return all rows to i or eci starts data columns
. 1
C filter based key for the row RowkeyFilter (major).
C1 OK key comparison filter.
    the filter filter = null;
    String filterStr = "00_460075097670490_1534925332480";
    
    row of keys equal filter:
    filter = new new PrefixFilter ( Bytes.toBytes (filterStr.trim ())); // returns the row of keys for the content of the entire contents of the specified row of keys
    
    row key range filter: 
    filter the RowFilter new new = (CompareOp.NOT_EQUAL, new new BinaryComparator (filterStr.trim () .getBytes ()));
    // returns OK key is not equal to the entire contents of the designated key row
    
    line keys are less than the filter:
    = the RowFilter new new filter (CompareOp.LESS, new new BinaryPrefixComparator (filterStr.trim () the getBytes ()).);
    // Returns OK key prefix specified row of keys less than or equal to the entire contents of
    
    row keys less filter: 
    filter the RowFilter new new = ( CompareOp.LESS_OR_EQUAL, new new BinaryPrefixComparator (filterStr.trim () the getBytes ().));
    // returns OK key prefixes the entire content of less than or equal to the specified row of keys
    
    row of keys is larger than the filter: 
    filter the RowFilter new new = (CompareOp.GREATER, new new BinaryPrefixComparator (filterStr.trim () the getBytes ().));
    // returns OK key prefix equal to the specified row is greater than the entire content key
    
    row of keys greater than or equal filter: 
    filter the RowFilter new new = (CompareOp.GREATER_OR_EQUAL, new new BinaryPrefixComparator (filterStr.trim () .getBytes ()));
    // returns the row of keys greater than or equal to the specified row of keys entirety 

c2 filter comprises a row of keys.
    OK key filter comprising: 
    = the RowFilter new new filter (CompareFilter.CompareOp.EQUAL, new new SubstringComparator (filterStr.trim ())); 
    // key travel filter contains all the data specified string

c3 and filtered by startkey endkey.
    
    compared with the above filter, when the number of pieces of data in the result set is substantially the same, the efficiency of this filter embodiment is significantly higher
    in our business scene most widely used
    
    in this way is not used filter object: the 
    
    code is as follows:
    String = StartKey "00_460075097670490_1534925332480 ";
    String EndKey =" 00_460075097670490_1534925432480 "
    Scan Scan = new new Scan ();
    scan.setStartRow (startkey.trim () getBytes ().);
    scan.setStopRow (endkey.trim () getBytes ().);
    // query from home row keys to the total number of rows recording end key
    // NOTE: includes a start line and end line keys !! key

according to previously acquired ResultScanner objects traverse the result set and output:
    ResultScanner RS = table.getScanner (scan);
        for (Result result : rs) {
             List<Cell> cells= result.listCells();    
             for (Cell cell : cells) {
                 String row = Bytes.toString(result.getRow());
                 String family1 = Bytes.toString(CellUtil.cloneFamily(cell));
                 String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                 String value = Bytes.toString(CellUtil.cloneValue(cell));
                 System.out.println("[row:"+row+"],[family:"+family1+"],[qualifier:"+qualifier+"]"
                         + ",[value:"+value+"],[time:"+cell.getTimestamp()+"]");
            }
        }

发布了17 篇原创文章 · 获赞 2 · 访问量 5万+

Guess you like

Origin blog.csdn.net/u011250186/article/details/103823807