Hbase Java API: Retrieving all rows that match a Partial Row Key -


In the Python module, I can retrieve all those rows that are the row keys introduced with a string key (I.e., partial row key).

Assume I have a line key in the format of (id | TYPE | DATE), I will be able to find all the rows with an ID of 1 and a type of A:

  import happybase connection = happybase.Connection ('hmaster-host.com') table = connection.table key ('table_name'), data in table.scan (row_prefix = "1 | A | "): The print key, the data   

is that I'm using it as a fully client-side Java program, which is Ryas are, but I can only find it'm using the full line key line:

  Import org.apache.hadoop.conf.configuration; Import org.apache.hadoop.fs.Path; Import org.apache.hadoop.hbase.client.Get; Import org.apache.hadoop.hbase.client.HTable; Import org.apache.hadoop.hbase.client.Result; Import org.apache.hadoop.hbase.util.Bytes; // class foo {public static zero main (string [] args) {configuration conf = new configuration (); Conf.addResource (new path ("C: \\ core-site.xml")); Conf.addResource (new path ("C: \\ hbase-site.xml")); Eligible table = new qualified (conf, "table_name"); Result line = table.get (get new (bytes.times ("1 | one | 2014-01-01 00:00"))); PrintRow (line); } Public static zero printers (result results) {string return string = ""; ReturnString + = bytes.stosting (result .getValue (bytes.times ("cf"), bytes.times ("id")) "+", "; returnString + = bytes.toString (result.getValue (bytes.times ("Cf"), bytes.times ("type")) + ",";; ReturnString + = bytes.stosting (result .getValue (bytes.times ("cf"), bytes toto ("date")) ); Println (returnString);} //}  

where "cf" column is family name.

Answer:

  Import java.io .IOException; Import java.util.Iterator; Import org.apache.hadoop.conf.configuration; Import org.apache.hadoop.fs.Path; Import Import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase .client.Scan; Import org.apache.hadoop.hbase.filter.Filter; Import org.apache.hadoop.hbase.filter.PrefixFilter; Import org.apache.hadoop.hbase.util.Bytes; // class foo {Public static zero main (string [] args) {configuration conf = new configuration (); Conf.addResource (new path ("C: \\ core-site.xml")); Conf.addResource (new path ("C: \\ hbase-site.xml")); Eligible table = new qualified (conf, "table_name"); Byte [] Prefix = Bytes. Totes ("1 | a |"); Scan scan = new scan (prefix); Filter prefix filter = new prefix filter (prefix); Scan.setFilter (prefixFilter); Resultant Scanner Results Scanner = table.getScanner (Scan); PrintRows (resultScanner); // result line = table.get (get new (bytes.times ("1 | one | 2014-01-01 00:00"))); // printRow (line); } Public Static Zero Print RO (Resultant Scanner Result for Eskner) {for (Iterator> Lieutenant; Results> Iterator = Result. Statement (Iterator); hasNext ();) {printRow (iterator.next ();}} Public Static Zero Printer (Results Results) {String Return String = ""; Return String + = Bytes.Sostosting (Results.Get Value (Bytes. Titles ("CF"), Bytes Totowitz ("ID")) "+ "; Return String + = Bytes.trusting ( Returns .getValue (bytes.times ("CF"), bytes.times ("type")) + ","; return string + = bytes .stustring (result.get value (bytes.tit ("cf"), Bytes Totoibets ("date")); System.out.println (returnString);}}  

Note that I use the setFilter method, While the answer below uses the addFilter method, using different APIs in our account.

You get HTable find ops Using ration so that you can only return one line (note that you can specify a prefix here and you do not have to enter the full key)

If you get multiple rows back If you want, you should use scanned

  byte [] prefix = byte .toBytes ("1 | A; "); scan scan = new scan (prefix); prefix filterfilffilter = new prefix filter (prefix); scan.addFilter (prefixFilter); resulting scanner result Scanner = table.getScanner (scan);  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -