diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Display.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Display.java index d3b9a21bbcf..670fa152f72 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Display.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/Display.java @@ -175,7 +175,7 @@ class Display extends FsCommand { public static class Checksum extends Display { public static final String NAME = "checksum"; - public static final String USAGE = " ..."; + public static final String USAGE = "[-v] ..."; public static final String DESCRIPTION = "Dump checksum information for files that match the file " + "pattern to stdout. Note that this requires a round-trip " + @@ -184,6 +184,16 @@ class Display extends FsCommand { "file depends on its content, block size and the checksum " + "algorithm and parameters used for creating the file."; + private boolean displayBlockSize; + + @Override + protected void processOptions(LinkedList args) + throws IOException { + CommandFormat cf = new CommandFormat(1, Integer.MAX_VALUE, "v"); + cf.parse(args); + displayBlockSize = cf.getOpt("v"); + } + @Override protected void processPath(PathData item) throws IOException { if (item.stat.isDirectory()) { @@ -191,14 +201,15 @@ class Display extends FsCommand { } FileChecksum checksum = item.fs.getFileChecksum(item.path); - if (checksum == null) { - out.printf("%s\tNONE\t%n", item.toString()); + String outputChecksum = checksum == null ? "NONE" : + String.format("%s\t%s", checksum.getAlgorithmName(), StringUtils + .byteToHexString(checksum.getBytes(), 0, checksum.getLength())); + if (displayBlockSize) { + FileStatus fileStatus = item.fs.getFileStatus(item.path); + out.printf("%s\t%s\tBlockSize=%s%n", item.toString(), outputChecksum, + fileStatus != null ? fileStatus.getBlockSize() : "NONE"); } else { - String checksumString = StringUtils.byteToHexString( - checksum.getBytes(), 0, checksum.getLength()); - out.printf("%s\t%s\t%s%n", - item.toString(), checksum.getAlgorithmName(), - checksumString); + out.printf("%s\t%s%n", item.toString(), outputChecksum); } } } diff --git a/hadoop-common-project/hadoop-common/src/site/markdown/FileSystemShell.md b/hadoop-common-project/hadoop-common/src/site/markdown/FileSystemShell.md index f4a37ea0366..f050e30832c 100644 --- a/hadoop-common-project/hadoop-common/src/site/markdown/FileSystemShell.md +++ b/hadoop-common-project/hadoop-common/src/site/markdown/FileSystemShell.md @@ -73,10 +73,14 @@ Returns 0 on success and -1 on error. checksum -------- -Usage: `hadoop fs -checksum URI` +Usage: `hadoop fs -checksum [-v] URI` Returns the checksum information of a file. +Options + +* The `-v` option displays blocks size for the file. + Example: * `hadoop fs -checksum hdfs://nn1.example.com/file1` diff --git a/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml b/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml index 29a88fc8fba..e38c2592f85 100644 --- a/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml +++ b/hadoop-common-project/hadoop-common/src/test/resources/testConf.xml @@ -714,7 +714,7 @@ RegexpComparator - ^-checksum <src> \.\.\. :\s* + ^-checksum \[-v\] <src> \.\.\. :\s* RegexpComparator diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java index 5266fe409b3..76179bce566 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSShell.java @@ -71,6 +71,7 @@ import org.junit.rules.Timeout; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Rule; +import org.junit.Assert; import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.FS_TRASH_INTERVAL_KEY; import static org.apache.hadoop.fs.permission.AclEntryScope.ACCESS; @@ -1121,6 +1122,31 @@ public class TestDFSShell { } } + @Test (timeout = 30000) + public void testChecksum() throws Exception { + PrintStream printStream = System.out; + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + FsShell shell = new FsShell(dfs.getConf()); + final Path filePath = new Path("/testChecksum/file1"); + writeFile(dfs, filePath); + FileStatus fileStatus = dfs.getFileStatus(filePath); + FileChecksum checksum = dfs.getFileChecksum(filePath); + String[] args = {"-checksum", "-v", filePath.toString()}; + assertEquals(0, shell.run(args)); + // verify block size is printed in the output + assertTrue(out.toString() + .contains(String.format("BlockSize=%s", fileStatus.getBlockSize()))); + // verify checksum is printed in the output + assertTrue(out.toString().contains(StringUtils + .byteToHexString(checksum.getBytes(), 0, checksum.getLength()))); + } finally { + Assert.assertNotNull(printStream); + System.setOut(printStream); + } + } + @Test (timeout = 30000) public void testCopyToLocal() throws IOException { FsShell shell = new FsShell(dfs.getConf());