設置磁盤配額
命令行:
hdfs dfsadmin -setQuota 5 /tmp
超額的錯誤提示爲:
put: The NameSpacequota (directories and files) of directory /tmp is exceeded: quota=5 filecount=8
java代碼:
public static void main(String[] args) {
FileSystem hdfs = null;
Configuration conf = new Configuration();
try {
hdfs = FileSystem.get(new URI("hdfs://192.xxx.xx.xx:9000"),conf,"username");
} catch (Exception e) {
e.printStackTrace();
}
Path filenamePath = new Path("/test/input");
try {
//會根據集羣的配置輸出,例如我這裏輸出3G
System.out.println("SIZE OF THE HDFS DIRECTORY : " + hdfs.getContentSummary(filenamePath).getSpaceConsumed());
// 顯示實際的輸出,例如這裏顯示 1G
System.out.println("SIZE OF THE HDFS DIRECTORY : " + hdfs.getContentSummary(filenamePath).getLength());
} catch (IOException e) {
e.printStackTrace();
}
}
jni代碼:
int64_t hdfsa::getSpaceConsumed(hdfsFS dfs, const char* file_path) {
JNIEnv* envJNI = getJNIEnv();
if (envJNI == NULL) {
Throw(Exception::ServerException, "Can't get JNIEnv from current thread", LIBHDFS_ERROR);
}
RAIIJObject_Path hdfsPath(envJNI, file_path);
if (hdfsPath.jerror()) {
Throw(
Exception::ServerException,
hdfsPath.jerrorStackTrace("Failed to construct a HDFS Path object"),
LIBHDFS_ERROR);
}
// get class ContentSummary
jvalue jContentSummary;
jthrowable jthr = invokeMethod(
envJNI, &jContentSummary, INSTANCE,
(jobject)dfs,
"org/apache/hadoop/hdfs/DistributedFileSystem",
"getContentSummary",
"(Lorg/apache/hadoop/fs/Path;)Lorg/apache/hadoop/fs/ContentSummary;", hdfsPath.get());
checkAndThrow(jthr, "getContentSummary");
// get the disk space usage
jvalue jSpaceConsumed;
jthr = invokeMethod(
envJNI, &jSpaceConsumed, INSTANCE,
jContentSummary.l,
"org/apache/hadoop/fs/ContentSummary",
"getSpaceConsumed", "()J");
checkAndThrow(jthr, "getSpaceConsumed");
return jSpaceConsumed.j;
}