badss

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import java.util.Scanner;
import java.net.URI;
import java.io.File;
public class Hshell {
    public static void main(String[] args) {
        while(true){
            try {
                Scanner input=new Scanner(System.in);
                String command[]=input.nextLine().split("\\s+");
                if(command[0].equals("Hshell")){
                    switch (command[1]){
                        case "-cp":
                            if(command[2].equals("-r")){
                                copyLocalDir(command[3],command[4]);
                            }
                            else{
                                copyLocalFile(command[2], command[3]);
                            }
                            break;
                        case "-rm":
                            if(command[2].equals("-r")){
                                deletedir(command[3]);
                            }
                            else{
                                deleteFile(command[2]);
                            }
                            break;
                        case "-list":
                            disinfo(command[2]);
                            break;
                        case "-find":
                            findfile(command[2],command[3]);
                            break;
                        default:
                            System.out.println("Command Syntax Error!");
                    }
                }
                else if(command[0].equals("q")) {System.exit(0);}
                else{System.out.println("Command Syntax Error!");}
            }catch(Exception e) {
                e.printStackTrace();
            }
        }

    }
    public static boolean copyLocalFile(String lPath,String hPath){
        try{
            FileSystem hdfs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            Path localPath = new Path(lPath);
            File localFile=new File(lPath);
            Path hdfsPath = new Path(hPath);
            if(localFile.exists())
            {
//                hdfs.mkdirs(hdfsPath);可以不用
                hdfs.copyFromLocalFile(true,localPath,hdfsPath);
                System.out.println("Copy successfully!");
            }
            else{
                System.out.println("Local file is not exist!");
                hdfs.close();
                return false;
            }
            hdfs.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }

    public static boolean deleteFile(String deletepath){
        try{
            FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            if(fs.exists(new Path(deletepath))) {
                if(fs.delete(new Path(deletepath),false)){
                    System.out.println("File "+ deletepath +" has been deleted successfully!");
                }
            } else {
                System.out.println("File not Exists!");
                fs.close();
                return false;
            }
            fs.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    public static  boolean deletedir(String deleteDir){
        try {
            FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            if(fs.delete(new Path(deleteDir),true)){
                System.out.println("Directory "+ deleteDir +" has been deleted successfully!");
            }
            fs.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }


    public static boolean copyLocalDir(String srcDir,String destDir){
        try{
            File f=new File(srcDir);
            FileSystem hdfs=FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            if(f.exists()){
                if(f.isDirectory())
                {
                    String newdir=destDir.endsWith("/")?destDir+f.getName():destDir+"/"+f.getName();//新的路径
                    hdfs.mkdirs(new Path(newdir));
                    File[] files=f.listFiles();
                    for(File file:files)
                    {
                        if(file.isDirectory()){
                            copyLocalDir(file.getAbsolutePath(),newdir);
                        }else{
                            hdfs.copyFromLocalFile(new Path(file.getAbsolutePath()),new Path(newdir));
                        }
                    }
                }
                else{
                    System.out.println(srcDir+"is not a directory!");
                    hdfs.close();
                    return false;
                }
            }
            else{
                System.out.println("Directory is not exsists!");
                hdfs.close();
                return false;
            }
            hdfs.close();
        }catch(Exception e){e.printStackTrace();}
        return true;
    }

    public static boolean disinfo(String srcpath)
    {
        try {
            FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            Path dirpath=new Path(srcpath);
            if(fs.getFileStatus(dirpath).isFile()){
                System.out.println(fs.getFileStatus(dirpath));
            }
            else if(fs.getFileStatus(dirpath).isDirectory())
            {
                FileStatus[] stats = fs.listStatus(dirpath);
                Path[] paths = FileUtil.stat2Paths(stats);
                for(Path p : paths)
                    if(fs.getFileStatus(p).isFile()) {
                        System.out.println(fs.getFileStatus(p));
                    }
                else System.out.println(p.getName());
            }
            else{
                System.out.println("Error path!");
                fs.close();
                return false;
            }
            fs.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public  static boolean mvfile(String srcpath,String destpath)
    {
        try {
            FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            if(fs.exists(new Path(srcpath))) {
                fs.rename(new Path(srcpath),new Path(destpath));
            } else {
                System.out.println("File not Exists!");
                fs.close();
                return false;
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public static boolean findfile(String filename,String dirpath){
        try{
            FileSystem hdfs = FileSystem.get(new URI("hdfs://master:9000"), new Configuration());
            Path dpath=new Path(dirpath);
            if(hdfs.exists(dpath))
            {
                if(hdfs.getFileStatus(dpath).isDirectory()){
                    FileStatus[] stats = hdfs.listStatus(dpath);
                    for(FileStatus st:stats){
                        if(st.isFile()){
                            if(st.getPath().getName().equals(filename)){
                                System.out.println(st.getPath().toString());
                                hdfs.close();
                                return true;
                            }
                        }
                        else{
                            findfile(filename,st.getPath().toString());
                        }
                    }
                    hdfs.close();
                    return false;
                }
                else{
                    System.out.println(dirpath+" is not a directory!");
                }
            }
            else{
                System.out.println("Directory is not exist!");
                hdfs.close();
                return false;
            }
            hdfs.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值