hdfs api 模拟部分fs功能

打包为jar文件后可直接在命令行调用
大体上实现了创建删除重命名文件、创建删除重命名文件夹、上传下载文件、显示目录下文件的功能

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
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.Path;

public class Mydfs {
	static FileSystem fs = null;
	static String uri = "hdfs://localhost:9000";
	public static void main(String[] args) throws IOException{
		Configuration conf = new Configuration();
		try {
			fs = FileSystem.get(new URI(uri),conf,"hadoop");
			//lsf("/");
			//createDir("/test1/test2/test3");
			//rename("/test","/test10");
			//deleteDir("/test10");
			//putFile("/usr/a.txt","/test1/");
			//if(getFile("/test1/a.txt","/usr")) System.out.println("success");
			//if(createFile("/test1/b.txt")) System.out.println("success");
			//if(deleteFile("/test1/b.txt")) System.out.println("success");
			if(args.length < 2){
				System.out.println("Error:something wrong with order");
				System.exit(0);
			}
			
			String order = args[0].replace("-","");
			switch(order){
				case "ls":
					if(lsf(args[1])) 
						System.out.println("success");
					else
						System.out.println("fail");
					break;
					
				case "mkdir":
					if(createDir(args[1]))
						System.out.println("success");
					else
						System.out.println("fail");
					break;
					
				case "rm":
					if(args.length>2 && args[2]=="-r")
						if(deleteDir(args[3]))
							System.out.println("success");
						else
							System.out.println("fail");	
					else
						if(deleteFile(args[2]))
							System.out.println("success");
						else
							System.out.println("fail");
					break;
					
				case "touch":
					if(createFile(args[1]))
						System.out.println("success");
					else
						System.out.println("fail");
					break;
					
				case "put":
					if(args.length<3) {
						System.out.println("wrong order");
						break;
					}
					if(putFile(args[1],args[2]))
						System.out.println("success");
					else
						System.out.println("fail");
					break;
					
				case "get":
					if(args.length<3) {
						System.out.println("wrong order");
						break;
					}
					if(getFile(args[1],args[2]))
						System.out.println("success");
					else
						System.out.println("fail");
					break;
					
				case "mv":
					if(args.length<3) {
						System.out.println("wrong order");
						break;
					}
					if(rename(args[1],args[2]))
						System.out.println("success");
					else
						System.out.println("fail");
					break;
					
				default:
					System.out.println("Do nothing");
					break;
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		fs.close();
		
	}
	public static boolean lsf(String path){ // hdfs dfs -ls -R
		FileStatus[] fss = null;
		try {
			fss = fs.listStatus(new Path(path));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		for(FileStatus f :fss){
			System.out.println(f.getPath().toString().replace(uri,""));
			if(f.isDirectory()) lsf(f.getPath().toString());
		}
		return true;
		
	}
	@SuppressWarnings("deprecation")
	public static boolean deleteFile(String path){ //hdfs dfs -rm 
		try {
			if(fs.delete(new Path(path)))
				return true;
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(fs.exists(new Path(path)))
				return false;
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}
	
	@SuppressWarnings("deprecation")
	public static boolean deleteDir(String path){ //hdfs dfs -rm -r
		try {
			if(fs.delete(new Path(path)))
				return true;
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public static boolean createDir(String path){ //hdfs dfs -mkdir -p
		try {
			if(fs.mkdirs(new Path(path)))
				return true;
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public static boolean createFile(String path){ //hdfs dfs -touch ...
		try {
			FSDataOutputStream out = fs.create(new Path(path));
			out.writeUTF("hello hdfs api user\n");
			out.flush();
			out.close();
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(fs.exists(new Path(path)))
				return true;
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public static boolean getFile(String src,String dest){ //hdfs dfs -get
		try {
			fs.copyToLocalFile(new Path(src), new Path(dest));
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(fs.exists(new Path(src.substring(src.lastIndexOf('/'),src.length()))))
				return true;
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public static boolean putFile(String src,String dest){ //hdfs dfs -put
		try {
			fs.copyFromLocalFile(new Path(src), new Path(dest));
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(fs.exists(new Path(src.substring(src.lastIndexOf('/'),src.length()))))
				return true;
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public static boolean rename(String olds,String news){  //hdfs dfs mv
		try {
			if(fs.rename(new Path(olds),new Path(news)))
				return true;
		} catch (IllegalArgumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}

posted @ 2020-07-24 11:19  -拂石-  阅读(85)  评论(0)    收藏  举报