12.5每日总结6

发布时间 2023-12-05 23:19:39作者: 听着DJ读童话

提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import java.io.*;

public class RemoveOrMake {
/**
* 判断路径是否存在
*/
public static boolean test(Configuration conf, String path) {
try (FileSystem fs = FileSystem.get(conf)) {
return fs.exists(new Path(path));
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

/**
* 创建目录
*/
public static boolean mkdir(Configuration conf, String remoteDir) {
try (FileSystem fs = FileSystem.get(conf)) {
Path dirPath = new Path(remoteDir);
return fs.mkdirs(dirPath);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

/**
* 创建文件
*/
public static void touchz(Configuration conf, String remoteFilePath) {
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf)) {
FSDataOutputStream outputStream = fs.create(remotePath);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 删除文件
*/
public static boolean rm(Configuration conf, String remoteFilePath) {
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf)) {
return fs.delete(remotePath, false);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

/**
* 主函数
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
String remoteFilePath = "/user/tiny/input/text.txt"; // HDFS路径
String remoteDir = "/user/tiny/input"; // HDFS路径对应的目录

try {
/* 判断路径是否存在,存在则删除,否则进行创建 */
if (RemoveOrMake.test(conf, remoteFilePath)) {
RemoveOrMake.rm(conf, remoteFilePath); // 删除
System.out.println("删除文件: " + remoteFilePath);
} else {
if (!RemoveOrMake.test(conf, remoteDir)) { // 若目录不存在,则进行创建
RemoveOrMake.mkdir(conf, remoteDir);
System.out.println("创建文件夹: " + remoteDir);
}
RemoveOrMake.touchz(conf, remoteFilePath);
System.out.println("创建文件: " + remoteFilePath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}