write & read a MapFile
import java.io.IOException; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.MapFile; import org.apache.hadoop.io.MapFile.Writer; import org.apache.hadoop.io.MapFile.Reader; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class MyMapFile { static private final String[] DATA = { "this is the first", "this is the second", "this is the third", "this is the forth" }; public static void main(String[] args) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); String uri = args[0]; IntWritable key = new IntWritable(); Text val = new Text(); MapFile.Writer writer = new MapFile.Writer(conf, new Path(uri), Writer.keyClass(key.getClass()), Writer.valueClass(val.getClass())); for( int i = 0; i < 10; i++ ) { key.set( i + 1 ); val.set(DATA[ i % DATA.length ]); writer.append(key, val); } writer.close(); MapFile.Reader reader = new MapFile.Reader(new Path(uri), conf); while( reader.next(key, val) ){ System.out.println( key + "\t" + val ); } reader.close(); } }