hadoop命令源码之ls实现

package cn.yws;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;


public class MyLS {
	
	private Configuration conf;
	public static final SimpleDateFormat dateForm = 
		    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public MyLS(){}
	
	public MyLS(Configuration conf) {
		super();
		this.conf = conf;
	}
	public Configuration getConf() {
		return conf;
	}

	public void setConf(Configuration conf) {
		this.conf = conf;
	}
	
	/** helper returns listStatus() */
	  private static FileStatus[] shellListStatus(String cmd, FileSystem srcFs,FileStatus src) {
	    if (!src.isDir()) {
	      FileStatus[] files = { src };
	      return files;
	    }
	    Path path = src.getPath();
	    try {
	      FileStatus[] files = srcFs.listStatus(path);
	      if ( files == null ) {
	        System.err.println(cmd +  ": could not get listing for '" + path + "'");
	      }
	      return files;
	    } catch (IOException e) {
	      System.err.println(cmd +  ": could not get get listing for '" + path + "' : " + e.getMessage().split("\n")[0]);
	    }
	    return null;
	  }

	private int ls(String srcf, boolean recursive) throws IOException {
	    Path srcPath = new Path(srcf);
	    
	    FileSystem srcFs = srcPath.getFileSystem(this.getConf());
	    FileStatus[] srcs = srcFs.globStatus(srcPath);
	    
	    if (srcs==null || srcs.length==0) {
	      throw new FileNotFoundException("Cannot access " + srcf + 
	          ": No such file or directory.");
	    }
	 
	    boolean printHeader = (srcs.length == 1) ? true: false;
	    int numOfErrors = 0;
	    for(int i=0; i<srcs.length; i++) {
	      numOfErrors += ls(srcs[i], srcFs, recursive, printHeader);
	    }
	    return numOfErrors == 0 ? 0 : -1;
	  }

	  /* list all files under the directory <i>src</i>
	   * ideally we should provide "-l" option, that lists like "ls -l".
	   */
	  private int ls(FileStatus src, FileSystem srcFs, boolean recursive,
	      boolean printHeader) throws IOException {
	    final String cmd = recursive? "lsr": "ls";
	    final FileStatus[] items = shellListStatus(cmd, srcFs, src);
	    if (items == null) {
	      return 1;
	    } else {
	      int numOfErrors = 0;
	      if (!recursive && printHeader) {
	        if (items.length != 0) {
	          System.out.println("Found " + items.length + " items");
	        }
	      }
	      
	      int maxReplication = 3, maxLen = 10, maxOwner = 0,maxGroup = 0;

	      for(int i = 0; i < items.length; i++) {
	        FileStatus stat = items[i];
	        int replication = String.valueOf(stat.getReplication()).length();
	        int len = String.valueOf(stat.getLen()).length();
	        int owner = String.valueOf(stat.getOwner()).length();
	        int group = String.valueOf(stat.getGroup()).length();
	        
	        if (replication > maxReplication) maxReplication = replication;
	        if (len > maxLen) maxLen = len;
	        if (owner > maxOwner)  maxOwner = owner;
	        if (group > maxGroup)  maxGroup = group;
	      }
	      
	      for (int i = 0; i < items.length; i++) {
	        FileStatus stat = items[i];
	        Path cur = stat.getPath();
	        String mdate = dateForm.format(new Date(stat.getModificationTime()));
	        
	        System.out.print((stat.isDir() ? "d" : "-") + 
	          stat.getPermission() + " ");
	        System.out.printf("%"+ maxReplication + 
	          "s ", (!stat.isDir() ? stat.getReplication() : "-"));
	        if (maxOwner > 0)
	          System.out.printf("%-"+ maxOwner + "s ", stat.getOwner());
	        if (maxGroup > 0)
	          System.out.printf("%-"+ maxGroup + "s ", stat.getGroup());
	        System.out.printf("%"+ maxLen + "d ", stat.getLen());
	        System.out.print(mdate + " ");
	        System.out.println(cur.toUri().getPath());
	        if (recursive && stat.isDir()) {
	          numOfErrors += ls(stat,srcFs, recursive, printHeader);
	        }
	      }
	      return numOfErrors;
	    }
	  }
	  
	  public static void main(String[] args) {
		
		  if(args.length!=2)
			{
				System.out.println("args!==2,len="+args.length);
				return ;
			}
		  boolean recursive=false;
		  if(Integer.parseInt(args[1])>=1)
		  {
			  recursive=true;
		  }
		  Configuration conf=new Configuration();
		 conf.set("mapred.job.tracker", "192.168.0.58:9001");
		  MyLS myLS=new MyLS(conf);
		try {
			myLS.ls(args[0], recursive);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值