Hadoop文件的存儲格式

sequence文件存儲格式

  1. txt
    1. 純文本格式,若干行記錄。默認用字符編碼存儲
  2. SequenceFile格式(順序文件格式,可進行切割)
    1. key-value 格式進行存儲,最終形成的是一個二進制文件, 需用hadoop提供的api進行寫入存儲。
    2. 編寫 寫入 seq文件案例。
    
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS","hdfs://s100:8020");
        FileSystem fileSystem = FileSystem.get(configuration);
        Path path = new Path("hdfs://s100:8020/user/seqmyfile.seq");
        SequenceFile.Writer writer = SequenceFile.createWriter(fileSystem, configuration, path, IntWritable.class, Text.class);
    
    
        writer.append(new IntWritable(1),new Text("gg1"));
        writer.append(new IntWritable(1),new Text("gg2"));
        writer.append(new IntWritable(1),new Text("gg3"));
        writer.append(new IntWritable(1),new Text("gg4"));
        writer.close();
        
    
    
    1. 編寫讀取 seq 文件案例
     Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS","hdfs://s100:8020");
        FileSystem fileSystem = FileSystem.get(configuration);
        Path path = new Path("hdfs://s100:8020/user/seqmyfile.seq");
        SequenceFile.Reader sr = new SequenceFile.Reader(fileSystem,path,configuration);
    
        IntWritable key = new IntWritable();
        Text value = new Text();
        while (sr.next(key,value)){
            System.out.println(key +":"+value );
        }
        
    
    1. 查看文件內容
      $> hdfs dfs -text /user/myfile.seq
      $> hdfs dfs -cat /user/myfile.seq (此命令查看會出現亂碼)

seq 文件格式解析

順序文件由文件頭和隨後的一條或多條記錄組成 
---文件頭------
--key-value----sync
--key-value----
--key-value----
--key-value----
--key-value----sync
--key-value----
--key-value----
--key-value----sync

  1. 文件頭格式
    1. SEQ+版本號+key類型class+value類型class + 壓縮格式類型
  1. 代碼案例
  /**
     * 讀取文件位置
     */
    public void seekSeq() throws IOException {
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS","hdfs://s100:8020");
        FileSystem fileSystem = FileSystem.get(configuration);
        Path path = new Path("hdfs://s100:8020/user/seqmyfile.seq");
        SequenceFile.Reader sr = new SequenceFile.Reader(fileSystem,path,configuration);
        IntWritable key = new IntWritable();
        Text value = new Text();
        sr.seek(253); // 定位到第253字節的位置,告訴指針下一次要定位的位置。
        sr.next(key,value); // 定位到第253字節的位置,並取出相應的值。
        System.out.println(key +" : " + value);
        sr.close();
    }

    /**
     * 讀取seqfile 同步點
     */
    public void sync() throws IOException {
        /**
         * -----文件頭-------
  128byte* --key-value----sync
  153byte* --key-value----
        .* --key-value----
        .* --key-value----
        .* --key-value----sync
         * --key-value----
         * --key-value----
         * --key-value----sync
         */
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS","hdfs://s100:8020");
        FileSystem fileSystem = FileSystem.get(configuration);
        Path path = new Path("hdfs://s100:8020/user/seqmyfile.seq");
        SequenceFile.Reader sr = new SequenceFile.Reader(fileSystem,path,configuration);
        IntWritable key = new IntWritable();
        Text value = new Text();
        int syncPos = 12;
        sr.sync(syncPos);//如上圖在寫入文件的時候可一指定多少條記錄寫入一個同步點
        long pos = sr.getPosition();//獲取下次要定位的字節位置。
        sr.next(key,value);
        System.out.println("syncPos : " + syncPos + "pos : " + pos +"key : "+key+"value : " + value);
    }

MapFile文件格式

  1. 是排序的seqfie,具有索引。要求key按照大小順序添加
  2. 包含兩個文件
    1. index 文件:索引和偏移量的映射,可以設置間隔,默認128(解釋:第128key位置--->第256字節 指存入128個key 對應的 第128key的末尾位置是第128字節的位置。)
    2. data 文件:存放真實的數據。格式爲key -value 。和seqfile文件類似

具體的API可參看Hadoop權威指南第四章 4.5 基於文件的數據結構

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章