15

when scan the hbase table row by row, how can i get the row key? here is my code:

for (Result rr : scanner) {
   System.out.println(rr);
}

is there any method like getKey() that i can use? thanks.

Jean-Philippe Bond
  • 10,089
  • 3
  • 34
  • 60
user468587
  • 4,799
  • 24
  • 67
  • 124

1 Answers1

37

If you want the row key in a string format, use the getRow and the Bytes.toString methods :

for (Result rr : scanner) {
   String key = Bytes.toString(rr.getRow())
}

HBase API - Result object

getRow() Method for retrieving the row key that corresponds to the row from which this Result was created.

Jean-Philippe Bond
  • 10,089
  • 3
  • 34
  • 60
  • 7
    I saw getRow() in the method list and that was the only candidate to retrieve the RowKey. Just curious, why they decided to name it getRow() and not getRowKey()? – Aram Paronikyan Sep 07 '16 at 10:44
  • 1
    Is this answer correct? I wonder why it is not accepted if so. – Mahdi May 22 '17 at 11:22
  • @Mahdi this answer is correct. According to the docs "Method for retrieving the row key that corresponds to the row from which this Result was created."(https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Result.html#getRow--). – Paul Jan 28 '20 at 23:52
  • I am getting incorrect row, does it returns encoded string? – Gaurav Ghongde Jun 06 '23 at 13:27