下面列出了怎么用org.apache.hadoop.fs.FsStatus的API类实例代码及写法,或者点击链接到github查看源代码。
public boolean isValid() {
FsStatus fsStatus = null;
try {
fsStatus = ivaratorCacheDir.getFs().getStatus();
} catch (IOException e) {
log.warn("Unable to determine status of the filesystem: " + ivaratorCacheDir.getFs());
}
// determine whether this fs is a good candidate
if (fsStatus != null) {
long availableStorageMiB = fsStatus.getRemaining() / 0x100000L;
double availableStoragePercent = (double) fsStatus.getRemaining() / fsStatus.getCapacity();
// if we are using less than our storage limit, the cache dir is valid
return availableStorageMiB >= ivaratorCacheDir.getConfig().getMinAvailableStorageMiB()
&& availableStoragePercent >= ivaratorCacheDir.getConfig().getMinAvailableStoragePercent();
}
return false;
}
public void test() throws IOException {
Configuration conf = new Configuration();
conf.addResource("conf/hdfs/test-hdfs-client-conf.xml");
System.setProperty("HADOOP_USER_NAME", "hdfs");
DistributedFileSystem fs = (DistributedFileSystem) FileSystem.get(conf);
DatanodeInfo[] dataNodeStatus = fs.getDataNodeStats();
for (DatanodeInfo dninfo : dataNodeStatus) {
System.out.println(dninfo.getHostName() + ", Is Decommission:"
+ dninfo.isDecommissioned());
System.out.println(dninfo.getHostName() + ", Dump Data node:"
+ dninfo.dumpDatanode());
}
System.out.println("Default block size:" + fs.getDefaultBlockSize());
ContentSummary contentSummary = fs.getContentSummary(new Path("/"));
System.out.println("Content Summary:" + contentSummary);
FsStatus fsstatus = fs.getStatus();
System.out.println(fsstatus.getCapacity());
}
@Override
protected void processPath(PathData item) throws IOException {
FsStatus fsStats = item.fs.getStatus(item.path);
long size = fsStats.getCapacity();
long used = fsStats.getUsed();
long free = fsStats.getRemaining();
usagesTable.addRow(
item.fs.getUri(),
formatSize(size),
formatSize(used),
formatSize(free),
StringUtils.formatPercent((double)used/(double)size, 0)
);
}
@Override
public FsStatus getStatus() throws IOException {
try (WaitRecorder recorder = OperatorStats.getWaitRecorder(operatorStats)) {
return underlyingFs.getStatus();
} catch(FSError e) {
throw propagateFSError(e);
}
}
@Override
public FsStatus getStatus(Path p) throws IOException {
try (WaitRecorder recorder = OperatorStats.getWaitRecorder(operatorStats)) {
return underlyingFs.getStatus(p);
} catch(FSError e) {
throw propagateFSError(e);
}
}
@Override
protected void processPath(PathData item) throws IOException {
FsStatus fsStats = item.fs.getStatus(item.path);
long size = fsStats.getCapacity();
long used = fsStats.getUsed();
long free = fsStats.getRemaining();
usagesTable.addRow(
item.fs.getUri(),
formatSize(size),
formatSize(used),
formatSize(free),
StringUtils.formatPercent((double)used/(double)size, 0)
);
}
/** @throws Exception If failed. */
@Test
public void testStatus() throws Exception {
Path file1 = new Path("/file1");
try (FSDataOutputStream file = fs.create(file1, EnumSet.noneOf(CreateFlag.class),
Options.CreateOpts.perms(FsPermission.getDefault()))) {
file.write(new byte[1024 * 1024]);
}
FsStatus status = fs.getFsStatus();
assertEquals(getClientFsUser(), fs.getFileStatus(file1).getOwner());
assertEquals(4, grid(0).cluster().nodes().size());
long used = 0, max = 0;
for (int i = 0; i < 4; i++) {
IgniteFileSystem igfs = grid(i).fileSystem("igfs");
IgfsMetrics metrics = igfs.metrics();
used += metrics.localSpaceSize();
max += metrics.maxSpaceSize();
}
assertEquals(used, status.getUsed());
assertEquals(max, status.getCapacity());
}
@Override
public FsStatus getStatus(Path p) throws IOException {
CephStatVFS stat = new CephStatVFS();
ceph.statfs(p, stat);
FsStatus status = new FsStatus(stat.bsize * stat.blocks,
stat.bsize * (stat.blocks - stat.bavail),
stat.bsize * stat.bavail);
return status;
}
@Override
public FsStatus getStatus() throws IOException {
return underlyingFs.getStatus();
}
@Override
public FsStatus getStatus(Path p) throws IOException {
return underlyingFs.getStatus(p);
}
@Test
public void persistReloadTest() throws Exception {
File tempDir = Files.createTempDir();
tempDir.deleteOnExit();
File smallDir = new File(tempDir, "small");
Assert.assertTrue(smallDir.mkdirs());
File largeDir = new File(tempDir, "large");
Assert.assertTrue(largeDir.mkdirs());
LocalFileSystem fs = new LocalFileSystem();
fs.initialize(tempDir.toURI(), new Configuration());
FsStatus fsStatus = fs.getStatus();
// set the min remaining MB to something which will cause the 'small' directiory to be skipped
long minRemainingMB = (fsStatus.getRemaining() / 0x100000L) + 4096l;
List<IvaratorCacheDir> ivaratorCacheDirs = new ArrayList<>();
ivaratorCacheDirs
.add(new IvaratorCacheDir(new IvaratorCacheDirConfig(smallDir.toURI().toString(), 0, minRemainingMB), fs, smallDir.toURI().toString()));
ivaratorCacheDirs.add(new IvaratorCacheDir(new IvaratorCacheDirConfig(largeDir.toURI().toString()), fs, largeDir.toURI().toString()));
String uniquePath = "blah";
HdfsBackedSortedSet<String> sortedSet = new HdfsBackedSortedSet<>(ivaratorCacheDirs, uniquePath, 9999, 2, new FileSortedSet.PersistOptions());
// Add an entry to the sorted set
String someTestString = "some test string";
sortedSet.add(someTestString);
// persist the sorted set
sortedSet.persist();
Path smallPath = new Path(smallDir.toURI().toString());
Path smallSubPath = new Path(smallPath, uniquePath);
Path largePath = new Path(largeDir.toURI().toString());
Path largeSubPath = new Path(largePath, uniquePath);
// ensure that data was written to the large folder, not the small folder
Assert.assertFalse(fs.exists(smallSubPath));
Assert.assertEquals(0, fs.listStatus(smallPath).length);
Assert.assertTrue(fs.exists(largeSubPath));
FileStatus[] fileStatuses = fs.listStatus(largeSubPath);
Assert.assertEquals(1, fileStatuses.length);
Assert.assertTrue(fileStatuses[0].getPath().getName().startsWith("SortedSet"));
// Now make sure reloading an ivarator cache dir works
HdfsBackedSortedSet<String> reloadedSortedSet = new HdfsBackedSortedSet<>(ivaratorCacheDirs, uniquePath, 9999, 2, new FileSortedSet.PersistOptions());
Assert.assertEquals(1, reloadedSortedSet.size());
Assert.assertEquals(someTestString, reloadedSortedSet.first());
}
@Override
public FsStatus getFsStatus() throws AccessControlException, FileNotFoundException, IOException {
return dfs.getStatus();
}
@Override
public FsStatus getStatus(Path p) throws IOException {
statistics.incrementReadOps(1);
return new FsStatus(Long.MAX_VALUE, 0, Long.MAX_VALUE);
}
/**
* @see ClientProtocol#getStats()
*/
public FsStatus getDiskStatus() throws IOException {
long rawNums[] = callGetStats();
return new FsStatus(rawNums[0], rawNums[1], rawNums[2]);
}
public DiskStatus(FsStatus stats) {
super(stats.getCapacity(), stats.getUsed(), stats.getRemaining());
}
@Override
public FsStatus getStatus(Path p) throws IOException {
statistics.incrementReadOps(1);
return dfs.getDiskStatus();
}
@Override
public FsStatus getStatus(Path p) throws IOException {
return super.getStatus(fullPath(p));
}
@Override
public FsStatus getFsStatus() throws IOException {
return myFs.getFsStatus();
}
@Override
public FsStatus getFsStatus() throws AccessControlException,
FileNotFoundException, IOException {
return new FsStatus(0, 0, 0);
}
@Override
public FsStatus getFsStatus() {
return new FsStatus(0, 0, 0);
}
/**
* @see ClientProtocol#getStats()
*/
public FsStatus getDiskStatus() throws IOException {
long rawNums[] = callGetStats();
return new FsStatus(rawNums[0], rawNums[1], rawNums[2]);
}
public DiskStatus(FsStatus stats) {
super(stats.getCapacity(), stats.getUsed(), stats.getRemaining());
}
@Override
public FsStatus getStatus(Path p) throws IOException {
statistics.incrementReadOps(1);
return dfs.getDiskStatus();
}
@Override
public FsStatus getStatus(Path p) throws IOException {
return super.getStatus(fullPath(p));
}
@Override
public FsStatus getFsStatus() throws IOException {
return myFs.getFsStatus();
}
@Override
public FsStatus getFsStatus() throws AccessControlException,
FileNotFoundException, IOException {
return new FsStatus(0, 0, 0);
}
@Override
public FsStatus getFsStatus() {
return new FsStatus(0, 0, 0);
}
@Override
public FsStatus getStatus() throws IOException {
return fileSystem.getStatus();
}
@Override
public FsStatus getStatus(Path p) throws IOException {
return fileSystem.getStatus(convertToDefaultPath(p));
}
@Override
public FsStatus getFsStatus() throws AccessControlException, FileNotFoundException, IOException {
return new FsStatus(1000000000, 1000, 1000000000 - 1000);
}