public class FileSystemCat {
public static void main(String[] args) throws Exception {
String uri = "hdfs://localhost:9000/home/txy";
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
InputStream in = null;
try {
in = fs.open(new Path(uri));
IOUtils.copyBytes(in, System.out, 4096, false);
} finally {
IOUtils.closeStream(in);
}
}
}
因为第一种方法有那么大的限制,所以基本是不可能使用的。所以可以采用第二种方案。
其中
Configuration conf = new Configuration();
会读取系统默认的配置conf/core-site.xml配置
而uri指定了schema是hdfs,如果不指定,将会读取默认的本地文件系统。
=========================== 善良的分割线 ======================
常见问题:
Exception in thread "main" java.io.IOException: Call to localhost/127.0.0.1:9000 failed on local exception: java.io.EOFException
报此错说明客户端hadoop版本和服务端hadoop版本不一致。