1.15总结

java代码
package HDFSApi;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.;
import java.io.;
public class HDFSApi {
/**

  • 判断路径是否存在
    /
    public static boolean test(Configuration conf, String path) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    return fs.exists(new Path(path));
    }
    /*
  • 判断目录是否为空
  • true: 空,false: 非空
    /
    public static boolean isDirEmpty(Configuration conf, String remoteDir) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path dirPath = new Path(remoteDir);
    RemoteIterator remoteIterator = fs.listFiles(dirPath, true);
    return !remoteIterator.hasNext();
    }
    /*
  • 创建目录
    */
    public static boolean mkdir(Configuration conf, String remoteDir) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path dirPath = new Path(remoteDir);
    boolean result = fs.mkdirs(dirPath);
    fs.close();
    return result;
    }

/**

  • 删除目录
    /
    public static boolean rmDir(Configuration conf, String remoteDir) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path dirPath = new Path(remoteDir);
    /
    第二个参数表示是否递归删除所有文件 /
    boolean result = fs.delete(dirPath, true);
    fs.close();
    return result;
    }
    /
    *
  • 主函数
    /
    public static void main(String[] args) {
    Configuration conf = new Configuration();
    conf.set("fs.default.name","hdfs://node1:8020");
    String remoteDir = "/user/root/input"; // HDFS 目录
    Boolean forceDelete = false; // 是否强制删除
    try {
    /
    判断目录是否存在,不存在则创建,存在则删除 */
    if ( !HDFSApi.test(conf, remoteDir) ) {
    HDFSApi.mkdir(conf, remoteDir); // 创建目录
    System.out.println("创建目录: " + remoteDir);
    } else {
    if ( HDFSApi.isDirEmpty(conf, remoteDir) || forceDelete ) { // 目录为空或强制删除
    HDFSApi.rmDir(conf, remoteDir);
    System.out.println("删除目录: " + remoteDir);
    } else { // 目录不为空
    System.out.println("目录不为空,不删除: " + remoteDir);
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

(8)向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾;
追加到文件末尾

追加到原文件的开头,在 HDFS 中不存在与这种操作对应的命令,因此,无法使用一条
命令来完成。可以先移动到本地进行操作,再进行上传覆盖,具体命令如下:
hadoop fs -get text.txt
cat text.txt >> local.txt
hadoop fs -copyFromLocal -f local.txt text.txt

java代码
package HDFSApi;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.;
import java.io.;
public class HDFSApi {
/**

  • 判断路径是否存在
    /
    public static boolean test(Configuration conf, String path) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    return fs.exists(new Path(path));
    }
    /*
  • 追加文本内容
    /
    public static void appendContentToFile(Configuration conf, String content, String
    remoteFilePath) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path remotePath = new Path(remoteFilePath);
    / 创建一个文件输出流,输出的内容将追加到文件末尾 /
    FSDataOutputStream out = fs.append(remotePath);
    out.write(content.getBytes());
    out.close();
    fs.close();
    }
    /*
  • 追加文件内容
    /
    public static void appendToFile(Configuration conf, String localFilePath, String
    remoteFilePath) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path remotePath = new Path(remoteFilePath);
    / 创建一个文件读入流 /
    FileInputStream in = new FileInputStream(localFilePath);
    / 创建一个文件输出流,输出的内容将追加到文件末尾 /
    FSDataOutputStream out = fs.append(remotePath);
    / 读写文件内容 /
    byte[] data = new byte[1024];
    int read = -1;
    while ( (read = in.read(data)) > 0 ) {
    out.write(data, 0, read);
    }
    out.close();
    in.close();
    fs.close();
    }
    /*
  • 移动文件到本地
  • 移动后,删除源文件
    */
    public static void moveToLocalFile(Configuration conf, String remoteFilePath, String
    localFilePath) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path remotePath = new Path(remoteFilePath);
    Path localPath = new Path(localFilePath);
    fs.moveToLocalFile(remotePath, localPath);
    }

/**

  • 创建文件
    */
    public static void touchz(Configuration conf, String remoteFilePath) throws IOException {

    FileSystem fs = FileSystem.get(conf);
    Path remotePath = new Path(remoteFilePath);
    FSDataOutputStream outputStream = fs.create(remotePath);
    outputStream.close();
    fs.close();
    }

/**

  • 主函数
    /
    public static void main(String[] args) {
    Configuration conf = new Configuration();
    conf.set("fs.default.name","hdfs://node1:8020");
    String remoteFilePath = "/user/root/text.txt"; // HDFS 文件
    String content = "新追加的内容\n";
    String choice = "after"; //追加到文件末尾
    // String choice = "before"; // 追加到文件开头
    try {
    /
    判断文件是否存在 /
    if ( !HDFSApi.test(conf, remoteFilePath) ) {
    System.out.println("文件不存在: " + remoteFilePath);
    } else {
    if ( choice.equals("after") ) { // 追加在文件末尾
    HDFSApi.appendContentToFile(conf, content, remoteFilePath);
    System.out.println("已追加内容到文件末尾" + remoteFilePath);
    } else if ( choice.equals("before") ) { // 追加到文件开头
    / 没有相应的 api 可以直接操作,因此先把文件移动到本地/
    /创建一个新的 HDFS,再按顺序追加内容 */
    String localTmpPath = "/user/hadoop/tmp.txt";
    // 移动到本地
    HDFSApi.moveToLocalFile(conf, remoteFilePath, localTmpPath);
    // 创建一个新文件
    HDFSApi.touchz(conf, remoteFilePath);
    // 先写入新内容
    HDFSApi.appendContentToFile(conf, content, remoteFilePath);
    // 再写入原来内容
    HDFSApi.appendToFile(conf, localTmpPath, remoteFilePath);
    System.out.println("已追加内容到文件开头: " + remoteFilePath);
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

运行截图

(9)删除HDFS中指定的文件;

java代码
package HDFSApi;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.;
import java.io.;
public class HDFSApi {
/**

  • 删除文件
    */
    public static boolean rm(Configuration conf, String remoteFilePath) throws IOException {

    FileSystem fs = FileSystem.get(conf);
    Path remotePath = new Path(remoteFilePath);
    boolean result = fs.delete(remotePath, false);
    fs.close();
    return result;
    }
    /**

  • 主函数
    */
    public static void main(String[] args) {
    Configuration conf = new Configuration();
    conf.set("fs.default.name","hdfs://node1:8020");
    String remoteFilePath = "/user/root/text.txt"; // HDFS 文件
    try {
    if ( HDFSApi.rm(conf, remoteFilePath) ) {
    System.out.println("文件删除: " + remoteFilePath);
    } else {
    System.out.println("操作失败(文件不存在或删除失败)");
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

运行截图

(10)在HDFS中,将文件从源路径移动到目的路径。

Java代码
package HDFSApi;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.;
import java.io.;
public class HDFSApi {
/**

  • 移动文件
    */
    public static boolean mv(Configuration conf, String remoteFilePath, String
    remoteToFilePath) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path srcPath = new Path(remoteFilePath);
    Path dstPath = new Path(remoteToFilePath);
    boolean result = fs.rename(srcPath, dstPath);
    fs.close();
    return result;
    }

/**

  • 主函数
    */
    public static void main(String[] args) {
    Configuration conf = new Configuration();
    conf.set("fs.default.name","hdfs://node1:8020");
    String remoteFilePath = "hdfs:///user/root/text.txt"; // 源文件 HDFS 路径
    String remoteToFilePath = "hdfs:///user/root/new.txt"; // 目的 HDFS 路径
    try {
    if ( HDFSApi.mv(conf, remoteFilePath, remoteToFilePath) ) {
    System.out.println(" 将文件 " + remoteFilePath + " 移动到 " +
    remoteToFilePath);
    } else {
    System.out.println("操作失败(源文件不存在或移动失败)");
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
posted @   奉禾  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示
主题色彩