hadoop 读取文件的两种方式

时间:2022-02-23 08:21:17

1.操作javaAPI方式

 static{
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
public static void main(String[] args) throws IOException {
InputStream in=null;
try {
in=new URL(args[0]).openStream();
IOUtils.copyBytes(in, System.out, 4096,false);
} catch (Exception e) {
// TODO: handle exception
}
finally {
IOUtils.closeStream(in);
}

2.操作org.apache.hadoop.fs.FileSystem方式

public static void main(String[] args) throws IOException {
Configuration conf=new Configuration();
String[]otherargs=new GenericOptionsParser(conf, args).getRemainingArgs();
FileSystem fs=FileSystem.get(URI.create(otherargs[0]),conf);
InputStream in=null;
try {
in=fs.open(new Path(otherargs[0]));
IOUtils.copyBytes(in, System.out, 4096,false);
} catch (Exception e) {
// TODO: handle exception
}
finally {
IOUtils.closeStream(in);
}