HDFS操作01

实验内容与完成情况:

(一)编程实现以下功能,并利用Hadoop提供的Shell命令完成相同任务:

 

(1) HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,则由用户来指定是追加到原有文件末尾还是覆盖原有的文件;

 

(2) HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;

 

(3) HDFS中指定文件的内容输出到终端中;

 

(4) 显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;

 

(5) 给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;

 

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

 

(7) 提供一个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录文件所在目录不存在,则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删除该目录;

 

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

 

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

 

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

 

1)先展示编程实现效果:

 

 

 

 

HDFSOperations.java:

package hdfs;

 

import org.apache.hadoop.fs.*;

 

import java.io.IOException;

 

public class HDFSOperations {

    // 上传文件到 HDFS

    public static void uploadFile(String localFilePath, String hdfsPath, boolean overwrite, boolean append) throws IOException {

        FileSystem fs = HDFSConfig.getFileSystem();

        Path srcPath = new Path(localFilePath);

        Path destPath = new Path(hdfsPath);

 

        System.out.println("正在上传文件,从: " + localFilePath + " 到 HDFS 路径: " + hdfsPath);

        if (fs.exists(destPath)) {

            if (overwrite) {

                System.out.println("文件已存在。正在覆盖...");

                fs.delete(destPath, true);

            } else if (append) {

                System.out.println("文件已存在。正在追加...");

                FSDataOutputStream out = fs.append(destPath);

                FSDataInputStream in = fs.open(srcPath);

                byte[] buffer = new byte[1024];

                int bytesRead;

                while ((bytesRead = in.read(buffer)) != -1) {

                    out.write(buffer, 0, bytesRead);

                }

                in.close();

                out.close();

                System.out.println("文件追加成功。");

                return;

            } else {

                System.out.println("文件已存在。没有做任何修改。");

                return;

            }

        }

 

        fs.copyFromLocalFile(srcPath, destPath);

        System.out.println("文件上传成功。");

        fs.close();

    }

 

    // 下载文件到本地

    public static void downloadFile(String hdfsFilePath, String localFilePath) throws IOException {

        FileSystem fs = HDFSConfig.getFileSystem();

        Path hdfsPath = new Path(hdfsFilePath);

        Path localPath = new Path(localFilePath);

 

        System.out.println("正在下载文件,从 HDFS 路径: " + hdfsFilePath + " 到本地路径: " + localFilePath);

        if (fs.exists(hdfsPath)) {

            if (fs.exists(localPath)) {

                localPath = new Path(localFilePath + "_new");

                System.out.println("本地文件已存在。正在将下载的文件重命名为: " + localPath);

            }

            fs.copyToLocalFile(hdfsPath, localPath);

            System.out.println("文件下载成功。");

        } else {

            System.out.println("HDFS 上未找到文件。");

        }

 

        fs.close();

    }

 

    // 输出文件内容到终端

    public static void viewFile(String hdfsFilePath) throws IOException {

        FileSystem fs = HDFSConfig.getFileSystem();

        Path filePath = new Path(hdfsFilePath);

 

        System.out.println("正在查看文件内容: " + hdfsFilePath);

        if (fs.exists(filePath)) {

            FSDataInputStream in = fs.open(filePath);

            byte[] buffer = new byte[1024];

            int bytesRead;

            while ((bytesRead = in.read(buffer)) != -1) {

                System.out.write(buffer, 0, bytesRead);

            }

            in.close();

            System.out.println("\n文件内容显示成功。");

        } else {

            System.out.println("HDFS 上未找到文件。");

        }

 

        fs.close();

    }

 

    // 显示文件信息

    public static void fileInfo(String hdfsFilePath) throws IOException {

        FileSystem fs = HDFSConfig.getFileSystem();

        Path filePath = new Path(hdfsFilePath);

 

        System.out.println("正在获取文件信息: " + hdfsFilePath);

        if (fs.exists(filePath)) {

            FileStatus status = fs.getFileStatus(filePath);

            System.out.println("路径: " + status.getPath());

            System.out.println("所有者: " + status.getOwner());

            System.out.println("权限: " + status.getPermission());

            System.out.println("大小: " + status.getLen());

            System.out.println("修改时间: " + status.getModificationTime());

        } else {

            System.out.println("HDFS 上未找到文件。");

        }

 

        fs.close();

    }

 

    // 递归显示目录下文件信息

    public static void dirInfo(String hdfsDirPath) throws IOException {

        FileSystem fs = HDFSConfig.getFileSystem();

        Path dirPath = new Path(hdfsDirPath);

 

        System.out.println("正在获取目录信息: " + hdfsDirPath);

        if (fs.exists(dirPath)) {

            RemoteIterator<LocatedFileStatus> files = fs.listFiles(dirPath, true);

            while (files.hasNext()) {

                LocatedFileStatus fileStatus = files.next();

                System.out.println("路径: " + fileStatus.getPath());

                System.out.println("所有者: " + fileStatus.getOwner());

                System.out.println("权限: " + fileStatus.getPermission());

                System.out.println("大小: " + fileStatus.getLen());

                System.out.println("修改时间: " + fileStatus.getModificationTime());

                System.out.println("-------------------------------------------");

            }

        } else {

            System.out.println("HDFS 上未找到目录。");

        }

 

        fs.close();

    }

 

    // 创建文件

    public static void createFile(String hdfsFilePath) throws IOException {

        FileSystem fs = HDFSConfig.getFileSystem();

        Path filePath = new Path(hdfsFilePath);

 

        System.out.println("正在创建文件: " + hdfsFilePath);

        if (!fs.exists(filePath)) {

            FSDataOutputStream out = fs.create(filePath);

            out.close();

            System.out.println("文件创建成功。");

        } else {

            System.out.println("文件已存在。");

        }

 

        fs.close();

    }

 

    // 删除文件或目录

    public static void deletePath(String hdfsPath, boolean recursive) throws IOException {

        FileSystem fs = HDFSConfig.getFileSystem();

        Path path = new Path(hdfsPath);

 

        System.out.println("正在删除路径: " + hdfsPath);

        if (fs.exists(path)) {

            fs.delete(path, recursive);

            System.out.println("路径删除成功。");

        } else {

            System.out.println("HDFS 上未找到路径。");

        }

 

        fs.close();

    }

 

    // 追加内容到文件

    public static void appendToFile(String hdfsFilePath, String content) throws IOException {

        FileSystem fs = HDFSConfig.getFileSystem();

        Path filePath = new Path(hdfsFilePath);

 

        System.out.println("正在向文件追加内容: " + hdfsFilePath);

        if (fs.exists(filePath)) {

            FSDataOutputStream out = fs.append(filePath);

            out.write(content.getBytes());

            out.close();

            System.out.println("内容追加成功。");

        } else {

            System.out.println("HDFS 上未找到文件。");

        }

 

        fs.close();

    }

 

    // 移动文件

    public static void moveFile(String srcHdfsPath, String destHdfsPath) throws IOException {

        FileSystem fs = HDFSConfig.getFileSystem();

        Path srcPath = new Path(srcHdfsPath);

        Path destPath = new Path(destHdfsPath);

 

        System.out.println("正在移动文件,从: " + srcHdfsPath + " 到: " + destHdfsPath);

        if (fs.exists(srcPath)) {

            fs.rename(srcPath, destPath);

            System.out.println("文件移动成功。");

        } else {

            System.out.println("HDFS 上未找到源文件。");

        }

 

        fs.close();

    }

 

    public static void main(String[] args) {

        try {

            // 1. 上传文件

            HDFSOperations.uploadFile("src/main/java/hdfs/test.txt", "/user/hadoop/testFile.txt", true, false);

 

            // 2. 下载文件

            HDFSOperations.downloadFile("/user/hadoop/testFile.txt", "localFile.txt");

 

            // 3. 查看文件内容

            HDFSOperations.viewFile("/user/hadoop/testFile.txt");

 

            // 4. 查看文件信息

            HDFSOperations.fileInfo("/user/hadoop/testFile.txt");

 

            // 5. 查看目录信息

            HDFSOperations.dirInfo("/user/hadoop");

 

            // 6. 创建文件

            HDFSOperations.createFile("/user/hadoop/newFile");

 

            // 7. 删除文件

            HDFSOperations.deletePath("/user/hadoop/newFile", false);

 

            // 8. 追加内容

            HDFSOperations.appendToFile("/user/hadoop/testFile.txt", "这是追加的内容。");

 

            // 9. 移动文件

            HDFSOperations.moveFile("/user/hadoop/testFile.txt", "/user/hadoop/movedTestFile.txt");

 

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

 

 

posted @   艾鑫4646  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示