MapFile

1 篇文章 / 0 new
author
MapFile
MapFile 資料是以 key, value 方式存入, 其指定名稱所產生的不是檔案名稱, 而是一個目錄名稱, 但目錄下一般會有 index, data 兩個檔案,
(MapFile 資料結構)
► write
public class MapFileWrite {
    public static void main(String[] args) throws IOException {
        String uri = "/sequence/mapFile.txt";
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(uri), conf);
        IntWritable key = new IntWritable();
        Text value = new Text();
        MapFile.Writer writer = null;
        try {
            writer = new MapFile.Writer(conf, fs, uri, key.getClass(), value.getClass());
            for (int i = 0; i < 20; i++) {
                key.set(i + 1);
                value.set(String.format("%02d record data", i));
                writer.append(key, value);
            }
        } finally {
            IOUtils.closeStream(writer);
        }
        fs.close();
    }
}
► read
public class MapFileRead {
    public static void main(String[] args) throws Exception {
        String uri = "/sequence/mapFile.txt";
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(uri), conf);
        MapFile.Reader reader = new MapFile.Reader(fs, uri, conf);
        WritableComparable<?> key = (WritableComparable<?>) ReflectionUtils.newInstance(reader.getKeyClass(), conf);
        Writable value = (Writable) ReflectionUtils.newInstance(reader.getValueClass(), conf);
        while (reader.next(key, value)) {
            System.out.printf("[%s]=%s\n",key,value);
        }
        //
        reader.get(new IntWritable(5), value);
        System.out.printf("get(5)=%s\n",value);
        //
        reader.seek(new IntWritable(15));
        reader.next(key, value);
        System.out.printf("seek(15), get(%s)=%s\n",key,value);
        fs.close();
    }
}
Free Web Hosting