package cn.yws;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.aspectj.org.eclipse.jdt.internal.compiler.flow.FinallyFlowContext;
import com.sun.istack.internal.FinalArrayList;
public class MyCat {
/**
* Accumulate exceptions if there is any. Throw them at last.
*/
private abstract class DelayedExceptionThrowing {
abstract void process(Path p, FileSystem srcFs) throws IOException;
final void globAndProcess(Path srcPattern, FileSystem srcFs)
throws IOException {
List<IOException> exceptions = new ArrayList<IOException>();
for (Path p : FileUtil.stat2Paths(srcFs.globStatus(srcPattern),srcPattern))
try {
process(p, srcFs);
} catch (IOException ioe) {
exceptions.add(ioe);
}
if (!exceptions.isEmpty())
if (exceptions.size() == 1)
throw exceptions.get(0);
else
throw new IOException("Multiple IOExceptions: "
+ exceptions);
}
}
public void cat(final String src, boolean verifyChecksum,final Configuration conf) throws IOException {
// cat behavior in Linux
// [~/1207]$ ls ?.txt
// x.txt z.txt
// [~/1207]$ cat x.txt y.txt z.txt
// xxx
// cat: y.txt: No such file or directory
// zzz
Path srcPattern = new Path(src);
new DelayedExceptionThrowing() {
@Override
void process(Path p, FileSystem srcFs) throws IOException {
printToStdout(srcFs.open(p),conf);
}
}.globAndProcess(srcPattern,
getSrcFileSystem(srcPattern, verifyChecksum,conf));
}
/**
* Print from src to stdout.
*/
private void printToStdout(InputStream in,Configuration conf) throws IOException {
try {
//可以打印到控制端shell System.out.println()好像就不可以.
IOUtils.copyBytes(in, System.out, conf, false);
System.out.println("test test");
} finally {
in.close();
}
}
private FileSystem getSrcFileSystem(Path src, boolean verifyChecksum,Configuration conf
) throws IOException {
FileSystem srcFs = src.getFileSystem(conf);
srcFs.setVerifyChecksum(verifyChecksum);
return srcFs;
}
public static void main(String[] args) {
if(args.length!=1)
{
System.out.println("args!==1,len="+args.length);
return ;
}
Configuration conf=new Configuration();
conf.set("mapred.job.tracker", "192.168.0.58:9001");
MyCat myCat=new MyCat();
try {
myCat.cat(args[0], true, conf);
} catch (IOException e) {
e.printStackTrace();
}
}
}
hadoop命令源码之cat实现
最新推荐文章于 2024-04-28 08:37:56 发布