write & read a MapFile(基于全新2.2.0API)

write & read a  MapFile


 

 1 import java.io.IOException;

 2 

 3 import org.apache.hadoop.io.IntWritable;

 4 import org.apache.hadoop.conf.Configuration;

 5 import org.apache.hadoop.io.Text;

 6 import org.apache.hadoop.io.MapFile;

 7 import org.apache.hadoop.io.MapFile.Writer;

 8 import org.apache.hadoop.io.MapFile.Reader;

 9 import org.apache.hadoop.fs.FileSystem;

10 import org.apache.hadoop.fs.Path;

11 

12 

13 

14 public class MyMapFile {

15 

16   static private final String[] DATA =  {

17       "this is the first",

18       "this is the second",

19       "this is the third",

20       "this is the forth"

21     };  

22 

23   public static void main(String[] args) throws IOException {

24     Configuration conf = new Configuration();

25     FileSystem fs = FileSystem.get(conf);

26     String uri = args[0];

27     

28     IntWritable key = new IntWritable();

29     Text val = new Text();

30 

31     MapFile.Writer writer = new MapFile.Writer(conf, new Path(uri), Writer.keyClass(key.getClass()), Writer.valueClass(val.getClass())); 

32 

33     for( int i = 0; i < 10; i++ ) { 

34       key.set( i + 1 );

35       val.set(DATA[ i % DATA.length ]); 

36       writer.append(key, val);

37     }

38     writer.close();

39 

40     MapFile.Reader reader = new MapFile.Reader(new Path(uri), conf);

41 

42     while( reader.next(key, val) ){

43       System.out.println( key + "\t" + val );

44     }

45     reader.close();

46   }

47 }

48

 

你可能感兴趣的:(pfile)