hadoop命令源码之ls实现
2014-07-03 10:25
387 查看
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(); } } }
相关文章推荐
- hadoop命令源码之text实现
- C语言实现的ls命令源码分享
- hadoop命令源码之cat实现
- C语言实现的ls命令源码分享
- ls命令的简单实现源码
- Hadoop源码学习笔记(6)——从ls命令一路解剖
- Hadoop源码学习-脚本命令(hadoop fs -ls)执行细节
- 安装、进程-云计算学习笔记---hadoop的简介,以及安装,用命令实现对hdfs系统进行文件的上传下载-by小雨
- 命令ls的C语言实现
- Linux下ls命令的简单模拟实现
- C语言实现的linux下ls命令
- Photoshop色阶、曲线命令图解和编程实现(附源码)
- 使用C语言将ls命令重定向实现
- 实现一个简单的ls命令
- Unix ls命令的实现
- ls命令源码
- 用文件IO操作实现ls命令
- Linux命令简单实现 -- ls
- ls命令的实现
- 在linux下代码实现ls -l 命令(利用移位 和 与 操作 对指定位的分离 )(指定位段的分离)