HBASE-25115 HFilePrettyPrinter can't seek to the row which is the first row of a hfile

Closes #2473

Signed-off-by: Wellington Chevreuil <wchevreuil@apache.org>
Signed-off-by: Viraj Jasani <vjasani@apache.org>
This commit is contained in:
niuyulin 2020-10-04 16:02:12 +05:30 committed by Viraj Jasani
parent b0170d0e24
commit 3226c1795a
No known key found for this signature in database
GPG Key ID: B3D6C0B41C8ADFD5
2 changed files with 24 additions and 5 deletions

View File

@ -322,17 +322,17 @@ public class HFilePrettyPrinter extends Configured implements Tool {
// scan over file and read key/value's and check if requested // scan over file and read key/value's and check if requested
HFileScanner scanner = reader.getScanner(false, false, false); HFileScanner scanner = reader.getScanner(false, false, false);
fileStats = new KeyValueStatsCollector(); fileStats = new KeyValueStatsCollector();
boolean shouldScanKeysValues = false; boolean shouldScanKeysValues;
if (this.isSeekToRow) { if (this.isSeekToRow && !Bytes.equals(row, reader.getFirstRowKey().orElse(null))) {
// seek to the first kv on this row // seek to the first kv on this row
shouldScanKeysValues = shouldScanKeysValues = (scanner.seekTo(PrivateCellUtil.createFirstOnRow(this.row)) != -1);
(scanner.seekTo(PrivateCellUtil.createFirstOnRow(this.row)) != -1);
} else { } else {
shouldScanKeysValues = scanner.seekTo(); shouldScanKeysValues = scanner.seekTo();
} }
if (shouldScanKeysValues) if (shouldScanKeysValues) {
scanKeysValues(file, fileStats, scanner, row); scanKeysValues(file, fileStats, scanner, row);
} }
}
// print meta data // print meta data
if (shouldPrintMeta) { if (shouldPrintMeta) {

View File

@ -108,4 +108,23 @@ public class TestHFilePrettyPrinter {
String expectedResult = "Scanning -> " + fileInRootDir + "\n" + "Scanned kv count -> 1000\n"; String expectedResult = "Scanning -> " + fileInRootDir + "\n" + "Scanned kv count -> 1000\n";
assertEquals(expectedResult, result); assertEquals(expectedResult, result);
} }
@Test
public void testHFilePrettyPrinterSeekFirstRow() throws Exception {
Path fileNotInRootDir = UTIL.getDataTestDir("hfile");
TestHRegionServerBulkLoad.createHFile(fs, fileNotInRootDir, cf, fam, value, 1000);
assertNotEquals("directory used is not an HBase root dir", UTIL.getDefaultRootDirPath(),
fileNotInRootDir);
HFile.Reader reader =
HFile.createReader(fs, fileNotInRootDir, CacheConfig.DISABLED, true, conf);
String firstRowKey = new String(reader.getFirstRowKey().get());
System.setOut(ps);
new HFilePrettyPrinter(conf)
.run(new String[] { "-v", "-w" + firstRowKey, String.valueOf(fileNotInRootDir) });
String result = new String(stream.toByteArray());
String expectedResult = "Scanning -> " + fileNotInRootDir + "\n" + "Scanned kv count -> 1\n";
assertEquals(expectedResult, result);
}
} }