在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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM