這篇文章主要介紹了Hadoop文件的存儲格式,本文給大家介紹的非常詳細,具有一定的參考借鑑價值,需要的朋友可以參考下
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 (此命令查看會出現亂碼)
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字節的位置,告訴指針下一次要定位的位置。 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.包含兩個文件
- index 文件:索引和偏移量的映射,可以設置間隔,默認128(解釋:第128key位置--->第256字節 指存入128個key 對應的 第128key的末尾位置是第128字節的位置。)
- data 文件:存放真實的數據。格式爲key -value 。和seqfile文件類似
總結
以上所述是小編給大家介紹的Hadoop文件的存儲格式實例詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回覆大家的!