1. write方法
@Test
public void write() throws IOException {
Configuration cfg = new Configuration();
cfg.set("mapreduce.framework.name", "local");
cfg.set("fs.defaultFs", "file:///");
Path mapFile = new Path("E:/test/a.map");
MapFile.Writer.Option optionKey = MapFile.Writer.keyClass(Text.class);
SequenceFile.Writer.Option optionValue = MapFile.Writer.valueClass(IntWritable.class);
MapFile.Writer writer = new MapFile.Writer(cfg, mapFile, optionKey, optionValue);
writer.append(new Text("lisi"), new IntWritable(2222));
writer.append(new Text("mazi"), new IntWritable(4444));
writer.append(new Text("wanger"), new IntWritable(3333));
writer.append(new Text("zhangsan"), new IntWritable(1111));
IOUtils.closeStream(writer);
}
2.read方法
@Test
public void read() throws IOException {
Configuration cfg = new Configuration();
cfg.set("mapreduce.framework.name", "local");
cfg.set("fs.defaultFs", "file:///");
Path mapFile = new Path("E:/test/a.map");
MapFile.Reader reader = new MapFile.Reader(mapFile, cfg);
Text key = new Text();
IntWritable value = new IntWritable();
while (reader.next(key, value)) {
System.out.println(key + " : " + value);
}
IOUtils.closeStream(reader);
}
3.sequenceFile2MapFile方法
@Test
public void sequenceFile2MapFile() throws Exception {
Configuration cfg = new Configuration();
cfg.set("mapreduce.framework.name", "local");
cfg.set("fs.defaultFs", "file:///");
FileSystem fs = FileSystem.get(cfg);
Path map = new Path("E:/test/sequenceFile2MapFile");
Path mapData = new Path(map, MapFile.DATA_FILE_NAME);
SequenceFile.Reader.Option optionFile = SequenceFile.Reader.file(mapData);
SequenceFile.Reader reader = new SequenceFile.Reader(cfg, optionFile);
Class keyClass = reader.getKeyClass();
Class valueClass = reader.getValueClass();
reader.close();
long entries = MapFile.fix(fs, map, keyClass, valueClass, false, cfg);
System.out.printf("Created MapFile %s with %d entries\n", map, entries);
}
4.测试类
@Test
public void test() throws IOException {
Configuration cfg = new Configuration();
cfg.set("mapreduce.framework.name", "local");
cfg.set("fs.defaultFs", "file:///");
Path mapFile = new Path("E:/test/sequenceFile2MapFile");
MapFile.Reader reader = new MapFile.Reader(mapFile, cfg);
IntWritable key = new IntWritable();
Text value = new Text();
while (reader.next(key, value)) {
System.out.println(key + " : " + value);
}
IOUtils.closeStream(reader);
}