hadoop2.2编程: SequenceFileWritDemo

 1 import java.io.IOException;

 2 import java.net.URI;

 3 

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

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

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

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

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

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

10 import org.apache.hadoop.io.IOUtils;

11 

12 public class SequenceFileWritDemo {

13 private static final String[] DATA = { 

14 "one, two, buckle my shoe",

15 "Three, four, shut the door"

16 };

17 

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

19 String uri = args[0];

20 Configuration conf = new Configuration();

21 FileSystem fs = FileSystem.get(URI.create(uri), conf);

22 Path path = new Path(uri);

23 

24 IntWritable key = new IntWritable();

25 Text value = new Text();

26 SequenceFile.Writer writer = null;

27 try {

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

29 

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

31 key.set(100 - i);

32 

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

34 

35 System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key, value);

36 writer.append(key, value);

37 }

38 } finally {

39 IOUtils.closeStream(writer);

40 }

41 }

42 }

 

你可能感兴趣的:(sequence)