0
点赞
收藏
分享

微信扫一扫

Windows下使用Java API操作HDFS的常用方法


场景

Windows下配置Hadoop的Java开发环境以及用Java API操作HDFS

在上面将Hadoop的开发环境搭建起来之后,使用Java API 简单输出了文件目录。

那么对应HDFS的常用文件的操作还有哪些。

注:

实现

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]);
}
}

Windows下使用Java API操作HDFS的常用方法_hdfs

 

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();
}

Windows下使用Java API操作HDFS的常用方法_hdfs_02

 

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();
}

Windows下使用Java API操作HDFS的常用方法_hdfs_03

 

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("文件不存在");
}
}

Windows下使用Java API操作HDFS的常用方法_HDFS_04

 

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();
}

Windows下使用Java API操作HDFS的常用方法_System_05

 

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();
}

Windows下使用Java API操作HDFS的常用方法_HDFS_06

 

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();
}

Windows下使用Java API操作HDFS的常用方法_hdfs_07

 

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());
}
}
}
}
}
}

Windows下使用Java API操作HDFS的常用方法_HDFS_08

 

举报

相关推荐

0 条评论