HDFS实战之打开文件
- 源码如下
package shen.liu.hdfs.practice
import java.io.IOException
import java.io.InputStream
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.FileSystem
import org.apache.hadoop.fs.Path
import org.apache.zookeeper.common.IOUtils
public class HDFSFileCat {
public static void main(String args[]) throws IOException {
if(args.length!=1) {
System.out.println("args.length = "+args.length)
System.out.println("parameter error")
}else {
Configuration conf = new Configuration()
FileSystem hdfs = FileSystem.get(conf)
Path path = new Path(args[0])
InputStream in = null
try {
in = hdfs.open(path)
IOUtils.copyBytes(in,System.out,4096,false)
}finally {
IOUtils.closeStream(in)
//不管是否抛出异常,本语句都会执行
}
}
}
}