zoukankan      html  css  js  c++  java
  • 在HDFS中将文件从源路径移动到目的路径。

    import java.text.SimpleDateFormat;
    import java.util.Scanner;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FSDataOutputStream;
    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.LocatedFileStatus;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.RemoteIterator;
    
    public class J_moveFile {
    
        public static void DelFile(FileSystem fs, Path p_remotepath) {
            try {
                if (fs.delete(p_remotepath, true)) {
                    System.out.println("del successfully");
                } else {
                    System.out.println("del fail");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void ReadFilesStatus(FileStatus fileinfo) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("file path :" + fileinfo.getPath());
        }
    
        public static void GetAllFilePath(FileStatus fileinfo, FileSystem fs) {
            try {
                String s_path = "/user";
                Path p_path = new Path(s_path);
                RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(
                        p_path, true);
                while (remoteIterator.hasNext()) {
                    fileinfo = remoteIterator.next();
                    ReadFilesStatus(fileinfo);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void MoveFile(FileSystem fs, String s_remotepath,
                Path p_remotepath_src, Path p_remotepath_dst) {
            try {
                String s_tmppath = "/home/hadoop/tmpfile/";
                String[] split_path = s_remotepath.split("/");
                String filename = split_path[split_path.length - 1];
                s_tmppath = s_tmppath + filename;
                Path p_tmppath = new Path(s_tmppath);
                fs.moveToLocalFile(p_remotepath_src, p_tmppath);
                fs.moveFromLocalFile(p_tmppath, p_remotepath_dst);
                System.out.println("MoveFile successfully");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String arg[]) {
            try {
                Var_init var = new Var_init();
                GetAllFilePath(var.fileinfo, var.fs);
                System.out
                        .println("plz input the file u want to move like /user/hadoop/test /user/hadoop/qwq/");
                Scanner sc = new Scanner(System.in);
                String src_path = sc.next();
                String dst_path = sc.next();
                Path src_remotepath = new Path(src_path);
                Path dst_remotepath = new Path(dst_path);
    
                MoveFile(var.fs, src_path, src_remotepath, dst_remotepath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    View Code

    Var_init类参考 https://www.cnblogs.com/MiraculousB/p/13848744.htm

  • 相关阅读:
    求最大子数组和
    第四周学习进度
    四则运算3
    实用工具箱app开发日记5
    实用工具箱app开发日记4
    实用工具箱app开发日记3
    实用工具箱app开发日记2
    实用工具箱app开发日记1
    《软件需求与分析》阅读笔记
    软件需求分析--阅读笔记3
  • 原文地址:https://www.cnblogs.com/MiraculousB/p/13848984.html
Copyright © 2011-2022 走看看