Database Reference
In-Depth Information
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 ); // go back to the start of the file
IOUtils . copyBytes ( in , System . out , 4096 , false );
} finally {
IOUtils . closeStream ( in );
}
}
}
Here's the result of running it on a small file:
% hadoop FileSystemDoubleCat hdfs://localhost/user/tom/quangle.txt
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
FSDataInputStream also implements the PositionedReadable interface for
reading parts of a file at a given offset:
public interface PositionedReadable {
public int read ( long position , byte [] buffer , int offset , int
length )
throws IOException ;
public void readFully ( long position , byte [] buffer , int offset ,
int length )
throws IOException ;
public void readFully ( long position , byte [] buffer ) throws
IOException ;
}
The read() method reads up to length bytes from the given position in the file in-
to the buffer at the given offset in the buffer. The return value is the number of bytes
actually read; callers should check this value, as it may be less than length . The
Search WWH ::




Custom Search