Database Reference
In-Depth Information
If the next() method returns a non- null object, a key-value pair was read from the
stream, and the value can be retrieved using the getCurrentValue() method. Other-
wise, if next() returns null , the end of the file has been reached.
The program in Example 5-11 demonstrates how to read a sequence file that has Writ-
able keys and values. Note how the types are discovered from the
SequenceFile.Reader via calls to getKeyClass() and getValueClass() ,
and then ReflectionUtils is used to create an instance for the key and an instance
for the value. This technique allows the program to be used with any sequence file that
has Writable keys and values.
Example 5-11. Reading a SequenceFile
public class SequenceFileReadDemo {
public static void main ( String [] args ) throws IOException {
String uri = args [ 0 ];
Configuration conf = new Configuration ();
FileSystem fs = FileSystem . get ( URI . create ( uri ), conf );
Path path = new Path ( uri );
SequenceFile . Reader reader = null ;
try {
reader = new SequenceFile . Reader ( fs , path , conf );
Writable key = ( Writable )
ReflectionUtils . newInstance ( reader . getKeyClass (), conf );
Writable value = ( Writable )
ReflectionUtils . newInstance ( reader . getValueClass (), conf );
long position = reader . getPosition ();
while ( reader . next ( key , value )) {
String syncSeen = reader . syncSeen () ? "*" : "" ;
System . out . printf ( "[%s%s]\t%s\t%s\n" , position , syncSeen , key ,
value );
position = reader . getPosition (); // beginning of next record
}
} finally {
IOUtils . closeStream ( reader );
}
}
}
Another feature of the program is that it displays the positions of the sync points in the se-
quence file. A sync point is a point in the stream that can be used to resynchronize with a
record boundary if the reader is “lost” — for example, after seeking to an arbitrary posi-
tion in the stream. Sync points are recorded by SequenceFile.Writer , which inserts
Search WWH ::




Custom Search