1,编写代码
import java.io.*;
import java.net.URL;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;
public class URLCat {
static {
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
public static void main(String[] args) throws Exception {
InputStream in = null;
try {
in = new URL(args[0]).openStream();
IOUtils.copyBytes(in, System.out, 4096, false);
} finally {
IOUtils.closeStream(in);
}
}
}
2,编译Java代码
$ Hadoopcom.sun.tools.javac.Mian -d classes URLCat.java
// then the class files will be generated in the directory of classes
3,打包成jar包
jar cf URLCat.jar classes/URLCat.class
// then we will get the URLCat.jar, the source is from classes/URLCat.class
4,运行jar包
hadoop jar URLCat.jar URLCat hdfs://10.10.12.171/input/WordCount/file1
Result:
hadoop ok
hadoop fail
hadoop 2.3
可见hdfs://10.10.12.171/input/WordCount/file1的内容就输出到了标准输出了。