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;
}
}
badss
最新推荐文章于 2024-09-26 20:57:08 发布