HDFS 指令
通常需要在LINUX控制台查询HDFS相关信息,此时就需要使用到HDFS相关命令。
dfs相关命令
/bin/hdfs dfs - help 可以显示有哪些命令可以使用,/bin/hdfs dfs -help comman-name可以显示某个命令的具体使用细节。

Paste_Image.png
再比如想看看put命令的使用

Paste_Image.png
dfsadmin相关命令
使用/bin/hdfs dfsadmin -help来查看有哪些指令可以使用

Paste_Image.png
dfsadmin命令用法可以参考
https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/HDFSCommands.html#dfsadmin
-report 汇报HDFS的基本数据,这个数据也可以在NameNode的前端页面查到

Paste_Image.png
HDFS JAVA API
经常我们需要操作HDFS文件,因此熟悉最基本的HDFS API函数是非常有必要的。
通过我们需要使用FileSystem来打开与HDFS文件或者本地文件的文件句柄,进而可以操作HDFS文件或者本地文件。下面我们举例几种场景来简单熟悉一下API:1,将本地文件拷贝到HDFS上,2,创建HDFS文件,3,删除HDFS文件,4,将本地目录下的文件发到HDFS上进行合并,而不是先合并再发送。
1,将本地文件拷贝到HDFS上
/**
*
* @param path :hdfs path
*/
public void writeToHDFS(String src,String path){ try {
hdfs.copyFromLocalFile(new Path(src),new Path(path));
} catch (IOException e) {
e.printStackTrace();
}
}2,创建HDFS文件
/**
* 创建HDFS
* @param hdfsName
* @return
*/
public boolean createHdfs(String hdfsName){ try {
hdfs.createNewFile(new Path(hdfsName));
} catch (IOException e) {
e.printStackTrace(); return false;
} return true;
}3,删除HDFS文件
public boolean deleteHdfs(String hdfsName){ try {
Path path = new Path(hdfsName); boolean fileExists = hdfs.exists(path); if(fileExists){
hdfs.delete(path,true);
}else {
System.out.println("File not exist"); return false;
}
} catch (IOException e) {
e.printStackTrace(); return false;
} return true;
}4,将本地目录下的文件发到HDFS上进行合并,而不是先合并再发送
/**
*
* @param inputDir: 输入目录
* @param hdfs:hdfs输出路径
* @return true成功,false失败
*/
public boolean writeDirToHdfs(String inputDir,String hdfs){
Path inputDirPath = new Path(inputDir);
Path hdfsPath = new Path(hdfs);
FSDataInputStream fsDataInputStream = null;
FSDataOutputStream fsDataOutputStream = null; try {
fsDataOutputStream = this.hdfs.create(hdfsPath);
FileStatus[] fileStatus = local.listStatus(inputDirPath); for(FileStatus f : fileStatus){
System.out.println("fileName: " + f.getPath().getName());
fsDataInputStream = local.open(f.getPath()); byte[] bytes = new byte[1024]; int len = 0; while((len = fsDataInputStream.read(bytes)) > 0){
fsDataOutputStream.write(bytes,0,len);
}
}
} catch (IOException e) {
e.printStackTrace(); return false;
}finally { if(fsDataOutputStream != null){ try {
fsDataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} if(fsDataInputStream != null){ try {
fsDataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return true;
}
作者:kason_zhang
链接:https://www.jianshu.com/p/454e926f919d
随时随地看视频