Liyin updated the revision "[jira] [HBASE-4698] Let the HFile Pretty Printer
print all the key values for a specific row.".
Reviewers: mbautin, Kannan, jgray, gqchen, nspiegelberg, JIRA
Address Mikhail's comments.
REVISION DETAIL
https://reviews.facebook.net/D111
AFFECTED FILES
src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java
Index: src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java
===================================================================
--- src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java
+++ src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java
@@ -41,7 +41,6 @@
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.io.hfile.HFile.FileInfo;
-import org.apache.hadoop.hbase.io.hfile.HFileBlockIndex.BlockIndexReader;
import org.apache.hadoop.hbase.regionserver.TimeRangeTracker;
import org.apache.hadoop.hbase.util.BloomFilter;
import org.apache.hadoop.hbase.util.BloomFilterFactory;
@@ -68,7 +67,9 @@
private boolean printBlocks;
private boolean checkRow;
private boolean checkFamily;
-
+ private boolean isSeekToRow = false;
+ // the row that the user want to specify and only print kv for.
+ private byte[] row = null;
private Configuration conf;
private List<Path> files = new ArrayList<Path>();
@@ -92,6 +93,8 @@
options.addOption("a", "checkfamily", false, "Enable family check");
options.addOption("f", "file", true,
"File to scan. Pass full-path; e.g. hdfs://a:9000/hbase/.META./12/34");
+ options.addOption("s", "seekToRow", true,
+ "Seek to this row and print all the kvs for this row only");
options.addOption("r", "region", true,
"Region to scan. Pass region name; e.g. '.META.,,1'");
}
@@ -125,6 +128,19 @@
files.add(new Path(cmd.getOptionValue("f")));
}
+ if (cmd.hasOption("s")) {
+ String key = cmd.getOptionValue("s");
+ if (key!= null && key.length() != 0) {
+ row = key.getBytes();
+ isSeekToRow = true;
+ System.out.println("Only print the kv for the row: " +
+ Bytes.toString(row));
+ } else {
+ System.err.println("Invalid row is specified.");
+ System.exit(-1);
+ }
+ }
+
if (cmd.hasOption("r")) {
String regionName = cmd.getOptionValue("r");
byte[] rn = Bytes.toBytes(regionName);
@@ -203,8 +219,13 @@
if (printKey || checkRow || checkFamily) {
// scan over file and read key/value's, performing any requested checks
HFileScanner scanner = reader.getScanner(false, false, false);
- scanner.seekTo();
- scanKeyValues(file, scanner);
+ if (this.isSeekToRow) {
+ // seek to the first kv on this row
+ scanner.seekTo(KeyValue.createFirstOnRow(this.row).getKey());
+ } else {
+ scanner.seekTo();
+ }
+ scanKeyValues(file, scanner, row);
}
// print meta data
@@ -220,7 +241,16 @@
reader.close();
}
- private void scanKeyValues(Path file, HFileScanner scanner)
+ /**
+ *
+ * @param file The path of the file
+ * @param scanner The HFileScanner for the file
+ * @param row Seek to the specific row and print all the kvs for this row.
+ * if Row is null, it means no row is specified and it will
+ * print all the rows in this file.
+ * @throws IOException
+ */
+ private void scanKeyValues(Path file, HFileScanner scanner, byte[] row)
throws IOException {
KeyValue pkv = null;
boolean first = true;
@@ -230,6 +260,18 @@
System.out.print("[");
do {
KeyValue kv = scanner.getKeyValue();
+ if (row != null && row.length != 0) {
+ int result = Bytes.compareTo(kv.getRow(), row);
+ if (result > 0) {
+ System.out.println("All the data for row " +
+ Bytes.toString(row) + " has been printed out");
+ break;
+ } else if (result < 0) {
+ System.out.println("Start to scan for the row: " +
+ Bytes.toString(row));
+ continue;
+ }
+ }
// check if rows are in order
if (checkRow && pkv != null) {
if (Bytes.compareTo(pkv.getRow(), kv.getRow()) > 0) {