hadoop命令源码之cat实现

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值