场景
Windows下配置Hadoop的Java开发环境以及用Java API操作HDFS:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119379055
在上面将Hadoop的开发环境搭建起来之后,使用Java API 简单输出了文件目录。
那么对应HDFS的常用文件的操作还有哪些。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
1、获取HDFS文件系统
/**
* 获取HDFS文件系统
* @return
* @throws IOException
*/
public static FileSystem getFileSystem() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://192.168.148.128:9000");
configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
System.setProperty("HADOOP_USER_NAME","root");
FileSystem fileSystem = FileSystem.get(configuration);
return fileSystem;
}
2、获取HDFS中所有的dataNodes
/**
* 获取HDFS中所有的dataNode
* @return
* @throws IOException
*/
public static void listDataNodeInfo() throws IOException {
FileSystem fs = getFileSystem();
DistributedFileSystem hdfs = (DistributedFileSystem) fs;
DatanodeInfo[] dataNodeStats =hdfs.getDataNodeStats();
String[] names = new String[dataNodeStats.length];
System.out.println("HDFS中所有的dataNode:");
for(int i=0;i<names.length;i++)
{
names[i]=dataNodeStats[i].getHostName();
System.out.println(names[i]);
}
}
3、创建文件夹
/**
* 创建文件夹
* @return
* @throws IOException
*/
public static void mkdir() throws IOException {
FileSystem fs = getFileSystem();
Path srcPath = new Path("/newDir");
boolean isok = fs.mkdirs(srcPath);
if(isok)
{
System.out.printf("创建文件夹成功");
}else
{
System.out.printf("创建文件夹失败");
}
fs.close();
}
4、删除文件夹
/**
* 删除文件夹
* @return
* @throws IOException
*/
public static void deletedir() throws IOException {
FileSystem fs = getFileSystem();
Path srcPath = new Path("/newDir");
boolean isok = fs.deleteOnExit(srcPath);
if(isok)
{
System.out.printf("删除文件夹成功");
}else
{
System.out.printf("删除文件夹失败");
}
fs.close();
}
5、判断文件是否存在
/**
* 判断文件是否存在
* @return
* @throws IOException
*/
public static void checkFileExists() throws IOException {
FileSystem fs = getFileSystem();
Path srcPath = new Path("/badao.txt");
boolean isexist = fs.exists(srcPath);
if(isexist)
{
System.out.printf("文件存在");
}else
{
System.out.printf("文件不存在");
}
}
6、上传文件
/**
* 上传文件
* @return
* @throws IOException
*/
public static void uploadFile() throws IOException {
FileSystem fs = getFileSystem();
//源路径
Path srcPath = new Path("D:\\windows.txt");
//目标路径
Path targetPath = new Path("/");
fs.copyFromLocalFile(false,srcPath,targetPath);
fs.close();
}
7、下载文件
/**
* 下载文件
* @return
* @throws IOException
*/
public static void downloadFile() throws IOException {
FileSystem fs = getFileSystem();
//源路径
Path targetPath = new Path("D:\\");
//目标路径
Path srcPath = new Path("/user/1.txt");
fs.copyToLocalFile(srcPath,targetPath);
fs.close();
}
5、重命名文件
/**
* 重命名文件
* @return
* @throws IOException
*/
public static void renameFile() throws IOException {
FileSystem fs = getFileSystem();
//源路径
Path oldPath = new Path("/windows.txt");
//目标路径
Path newPath = new Path("/centos.txt");
boolean isok = fs.rename(oldPath,newPath);
if(isok)
{
System.out.printf("重命名成功");
}else
{
System.out.printf("重命名失败");
}
fs.close();
}
6、遍历目录和文件
/**
* 遍历目录和文件
* @return
* @throws IOException
*/
public static void showDir(Path path) throws IOException {
FileSystem fs = getFileSystem();
DistributedFileSystem hdfs = (DistributedFileSystem) fs;
FileStatus[] fileStatuses = hdfs.listStatus(path);
if(fileStatuses.length>0)
{
for (FileStatus status:fileStatuses) {
Path f = status.getPath();
System.out.println(f.toString());
if(status.isDirectory())
{
FileStatus[] files = hdfs.listStatus(f);
if(files.length>0)
{
for (FileStatus file:files) {
showDir(file.getPath());
}
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/136233.html