Example usage for org.apache.hadoop.hdfs DFSInputStream getReadStatistics

List of usage examples for org.apache.hadoop.hdfs DFSInputStream getReadStatistics

Introduction

In this page you can find the example usage for org.apache.hadoop.hdfs DFSInputStream getReadStatistics.

Prototype

public ReadStatistics getReadStatistics() 

Source Link

Document

Get statistics about the reads which this DFSInputStream has done.

Usage

From source file:com.cloudera.ByteBufferRecordReader.java

License:Apache License

@Override
public synchronized boolean nextKeyValue() throws IOException {
    if (key == null) {
        key = new LongWritable();
    }/*from  w ww  .ja v  a 2 s . co  m*/
    if (value == null) {
        value = new ByteBufferWritable();
    }
    if (pos >= end) {
        return false;
    }

    int numBytesRead = 0;
    // Use zero-copy ByteBuffer reads if available
    if (inputStream instanceof FSDataInputStream) {
        FSDataInputStream fsIn = (FSDataInputStream) inputStream;
        ByteBuffer buf = fsIn.read(bufferPool, (int) (end - start), readOption);
        numBytesRead += buf.limit();
        pos += buf.limit();
        // Update stats
        InputStream wrappedStream = fsIn.getWrappedStream();
        if (wrappedStream instanceof DFSInputStream) {
            DFSInputStream dfsIn = (DFSInputStream) wrappedStream;
            updateStats(dfsIn.getReadStatistics());
        }
        // Switch out the buffers
        if (value.getBuffer() != null) {
            fsIn.releaseBuffer(value.getBuffer());
        }
        value.setByteBuffer(buf);
    }
    // Fallback to normal byte[] based reads with a copy to the ByteBuffer
    else {
        byte[] b = new byte[(int) (end - start)];
        IOUtils.readFully(inputStream, b);
        numBytesRead += b.length;
        pos += b.length;
        value.setByteBuffer(ByteBuffer.wrap(b));
    }

    return numBytesRead > 0;
}