SequenceFile 转换成 MapFile
关键是给 SequenceFile 重建索引:使用 MapFile 的静态方法 x()。
01package com.laos.hadoop;
02
03import java.net.URI;
04
05import org.apache.hadoop.conf.Conguration;
06import org.apache.hadoop.fs.FileSystem;
07import org.apache.hadoop.fs.Path;
08import org.apache.hadoop.io.MapFile;
09import org.apache.hadoop.io.SequenceFile;
10
11public class MapFileFixer {
12public static void main(String[] args) throws Exception {
13String mapUri = args[0];
14
15Conguration conf = new Conguration();
16
17FileSystem fs = FileSystem.get(URI.create(mapUri), conf);
18Path map = new Path(mapUri);
19Path mapData = new Path(map, MapFile.DATA_FILE_NAME);
20
21// Get key and value types from data sequence le
22SequenceFile.Reader reader = new SequenceFile.Reader(fs, mapData, conf);
23Class keyClass = reader.getKeyClass();
24Class valueClass = reader.getValueClass();
25reader.close();
26
27// Create the map le index le
28long entries = MapFile.x(fs, map, keyClass, valueClass, false, conf);
29System.out.printf("Created MapFile %s with %d entries/n", map, entries);
30}
31}