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

write & read a sequence file


 

write & read a sequence file

 1 import java.io.IOException;

 2 

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

 4 import org.apache.hadoop.io.SequenceFile.Writer;

 5 import org.apache.hadoop.io.SequenceFile.Reader;

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

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

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

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

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

11 

12 public class MySequenceFile {

13   static private final String[] DATA =  {

14       "this is the first",

15       "this is the second",

16       "this is the third",

17       "this is the forth"

18     };  

19 

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

21     Configuration conf = new Configuration();

22     FileSystem fs = FileSystem.get(conf);

23     Path path = new Path(args[0]);

24     IntWritable key = new IntWritable();

25     Text value = new Text();

26 

27     SequenceFile.Writer writer = null;

28 

29     writer = SequenceFile.createWriter(conf, Writer.file(path), Writer.keyClass(key.getClass()), Writer.valueClass(value.getClass()));

30     for( int i = 0; i < 1000; i++ ) { 

31       key.set(i + 1); 

32       value.set(DATA[i % DATA.length]);

33       writer.append(key,value);

34     }   

35     writer.close();

36     SequenceFile.Reader reader = new SequenceFile.Reader(conf, Reader.file(path));

37     while( reader.next(key, value) ) {

38       String syncSeen = reader.syncSeen() ? "*" : "#";

39       System.err.println(key + "\t" + value + "\t" + reader.getPosition()+ "\t" + syncSeen);

40     }

41     reader.close();

42   }

43 }

 

你可能感兴趣的:(sequence)