HDFS-8545. Refactor FS#getUsed() to use ContentSummary and add an API to fetch the total file length from a specific path (Contributed by J.Andreina)
This commit is contained in:
parent
b0c818b60d
commit
7d2d16f4ee
|
@ -2079,16 +2079,17 @@ public abstract class FileSystem extends Configured implements Closeable {
|
|||
CACHE.remove(this.key, this);
|
||||
}
|
||||
|
||||
/** Return the total size of all files in the filesystem.*/
|
||||
public long getUsed() throws IOException{
|
||||
long used = 0;
|
||||
RemoteIterator<LocatedFileStatus> files = listFiles(new Path("/"), true);
|
||||
while (files.hasNext()) {
|
||||
used += files.next().getLen();
|
||||
}
|
||||
return used;
|
||||
/** Return the total size of all files in the filesystem. */
|
||||
public long getUsed() throws IOException {
|
||||
Path path = new Path("/");
|
||||
return getUsed(path);
|
||||
}
|
||||
|
||||
|
||||
/** Return the total size of all files from a specified path. */
|
||||
public long getUsed(Path path) throws IOException {
|
||||
return getContentSummary(path).getLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the block size for a particular file.
|
||||
* @param f the filename
|
||||
|
|
|
@ -389,7 +389,13 @@ public class FilterFileSystem extends FileSystem {
|
|||
public long getUsed() throws IOException{
|
||||
return fs.getUsed();
|
||||
}
|
||||
|
||||
|
||||
/** Return the total size of all files from a specified path.*/
|
||||
@Override
|
||||
public long getUsed(Path path) throws IOException {
|
||||
return fs.getUsed(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getDefaultBlockSize() {
|
||||
return fs.getDefaultBlockSize();
|
||||
|
|
|
@ -1237,6 +1237,12 @@ public class HarFileSystem extends FileSystem {
|
|||
return fs.getUsed();
|
||||
}
|
||||
|
||||
/** Return the total size of all files from a specified path.*/
|
||||
@Override
|
||||
public long getUsed(Path path) throws IOException {
|
||||
return fs.getUsed(path);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public long getDefaultBlockSize() {
|
||||
|
|
|
@ -1602,6 +1602,9 @@ Release 2.8.0 - UNRELEASED
|
|||
HDFS-9295. Add a thorough test of the full KMS code path.
|
||||
(Daniel Templeton via zhz)
|
||||
|
||||
HDFS-8545. Refactor FS#getUsed() to use ContentSummary and add an API to fetch
|
||||
the total file length from a specific path (J.Andreina via vinayakumarb)
|
||||
|
||||
OPTIMIZATIONS
|
||||
|
||||
HDFS-8026. Trace FSOutputSummer#writeChecksumChunks rather than
|
||||
|
|
|
@ -1048,4 +1048,32 @@ public class TestDistributedFileSystem {
|
|||
cluster.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout = 30000)
|
||||
public void testTotalDfsUsed() throws Exception {
|
||||
Configuration conf = new HdfsConfiguration();
|
||||
MiniDFSCluster cluster = null;
|
||||
try {
|
||||
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
|
||||
FileSystem fs = cluster.getFileSystem();
|
||||
// create file under root
|
||||
FSDataOutputStream File1 = fs.create(new Path("/File1"));
|
||||
File1.write("hi".getBytes());
|
||||
File1.close();
|
||||
// create file under sub-folder
|
||||
FSDataOutputStream File2 = fs.create(new Path("/Folder1/File2"));
|
||||
File2.write("hi".getBytes());
|
||||
File2.close();
|
||||
// getUsed(Path) should return total len of all the files from a path
|
||||
assertEquals(2, fs.getUsed(new Path("/Folder1")));
|
||||
//getUsed() should return total length of all files in filesystem
|
||||
assertEquals(4, fs.getUsed());
|
||||
} finally {
|
||||
if (cluster != null) {
|
||||
cluster.shutdown();
|
||||
cluster = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue