Hadoop权威指南:FSDataInputStream对象
package org.apache.hadoop.fs;
public class FSDataInputStream extends DataInputStream implements Seekable, PositionedReadable {
// implementation elided
}public interface Seekable {
void seek(long pos) throws IOExcption;
long getPos() throws IOException;
boolean seekToNewSource(long targetPos) throws IOException;
}使用seek()方法,将Hadoop文件系统中的一个文件写入标准输出两次
seek()方法,将Hadoop文件系统中的一个文件写入标准输出两次代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import java.io.IOException;
import java.net.URI;
public class FileSystemDoubleCat {
public static void main(String[] args) throws IOException {
String uri = args[0];
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
FSDataInputStream in = null;
try {
in = fs.open(new Path(uri));
IOUtils.copyBytes(in, System.out, 4096, false);
in.seek(0);
IOUtils.copyBytes(in, System.out, 4096, false);
} finally {
IOUtils.closeStream(in);
}
}
}编译
运行
PositionedReadable接口
read()
readFully()
最后更新于