佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Hadoop文件的存儲格式實例詳解

Hadoop文件的存儲格式實例詳解

熱門標簽:智能手機 檢查注冊表項 鐵路電話系統(tǒng) 美圖手機 網(wǎng)站文章發(fā)布 呼叫中心市場需求 銀行業(yè)務 服務器配置

sequence文件存儲格式

1.txt

純文本格式,若干行記錄。默認用字符編碼存儲

2.SequenceFile格式(順序文件格式,可進行切割)

key-value 格式進行存儲,最終形成的是一個二進制文件, 需用hadoop提供的api進行寫入存儲。

編寫 寫入 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();  

 3.編寫讀取 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 );
  }  

4.查看文件內容

$> hdfs dfs -text /user/myfile.seq
$> hdfs dfs -cat /user/myfile.seq (此命令查看會出現(xiàn)亂碼)

seq 文件格式解析

順序文件由文件頭和隨后的一條或多條記錄組成 

---文件頭------
--key-value----sync
--key-value----
--key-value----
--key-value----
--key-value----sync
--key-value----
--key-value----
--key-value----sync

文件頭格式

SEQ+版本號+key類型class+value類型class + 壓縮格式類型

代碼案例

 /**
   * 讀取文件位置
   */
  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字節(jié)的位置,告訴指針下一次要定位的位置。
    sr.next(key,value); // 定位到第253字節(jié)的位置,并取出相應的值。
    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();//獲取下次要定位的字節(jié)位置。
    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字節(jié) 指存入128個key 對應的 第128key的末尾位置是第128字節(jié)的位置。)
  2. data 文件:存放真實的數(shù)據(jù)。格式為key -value 。和seqfile文件類似

總結

以上所述是小編給大家介紹的Hadoop文件的存儲格式實例詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

標簽:河南 上海 新疆 樂山 滄州 沈陽 長治 紅河

巨人網(wǎng)絡通訊聲明:本文標題《Hadoop文件的存儲格式實例詳解》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    屯留县| 那坡县| 承德县| 益阳市| 三穗县| 习水县| 龙门县| 辉县市| 阜平县| 焦作市| 砚山县| 汉寿县| 万山特区| 广德县| 曲阜市| 手游| 晋中市| 儋州市| 广西| 塔城市| 布尔津县| 博野县| 勃利县| 射阳县| 五指山市| 泸溪县| 都江堰市| 吉木萨尔县| 瑞安市| 博客| 洛扎县| 辰溪县| 衡阳市| 西丰县| 伊宁县| 济南市| 武宣县| 清水河县| 栾川县| 新丰县| 华池县|