HDFS中两种random read比较.pdf
文本预览下载声明
HDFS 中两种random read 比较
HDFS 中两种random read 比较
code version: hadoop-0.19.1
首先说pread。pread 会明确的把要读的size 传给datanode (在new BlockReader
的时候)
Java 代码
/**
* Read bytes starting from the specified position.
*
* @param position start read from this position
* @param buffer read buffer
* @param offset offset into buffer
* @param length number of bytes to read
*
* @return actual number of bytes read
*/
@Override
public int read(long position, byte[] buffer, int offset, int length)
throws IOException {
// sanity checks
checkOpen();
if (closed) {
throw new IOException(Stream closed);
}
long filelen = getFileLength();
if ((position 0) || (position = filelen)) {
return -1;
}
int realLen = length;
if ((position + length) filelen) {
realLen = (int)(filelen - position);
}
// determine the block and byte range within the block
// corresponding to position and realLen
ListLocatedBlock blockRange = getBlockRange(position,
realLen);
int remaining = realLen;
for (LocatedBlock blk : blockRange) {
long targetStart = position - blk.getStartOffset();
long bytesToRead = Math.min(remaining, blk.getBlockSize() -
显示全部