您的位置:首页 > 运维架构

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();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: