Hadoop文件的存儲格式實例詳解
sequence文件存儲格式
1.txt
純文本格式,若干行記錄。默認(rèn)用字符編碼存儲
2.SequenceFile格式(順序文件格式,可進(jìn)行切割)
key-value 格式進(jìn)行存儲,最終形成的是一個二進(jìn)制文件, 需用hadoop提供的api進(jìn)行寫入存儲。
編寫 寫入 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.查看文件內(nèi)容
$> 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é)的位置,并取出相應(yīng)的值。 System.out.println(key +" : " + value); sr.close(); } /** * 讀取seqfile 同步點(diǎn) */ 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);//如上圖在寫入文件的時候可一指定多少條記錄寫入一個同步點(diǎn) 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.包含兩個文件
- index 文件:索引和偏移量的映射,可以設(shè)置間隔,默認(rèn)128(解釋:第128key位置--->第256字節(jié) 指存入128個key 對應(yīng)的 第128key的末尾位置是第128字節(jié)的位置。)
- data 文件:存放真實的數(shù)據(jù)。格式為key -value 。和seqfile文件類似
總結(jié)
以上所述是小編給大家介紹的Hadoop文件的存儲格式實例詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
聯(lián)邦學(xué)習(xí)FedAvg中模型聚合過程的理解分析
這篇文章主要為大家介紹了FedAvg中模型聚合過程的理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Hadoop 分布式存儲系統(tǒng) HDFS的實例詳解
HDFS是Hadoop Distribute File System 的簡稱,也就是Hadoop的一個分布式文件系統(tǒng)。這篇文章主要介紹了Hadoop 分布式存儲系統(tǒng) HDFS,需要的朋友可以參考下2019-06-06分享下網(wǎng)站開發(fā)人員應(yīng)該知道的61件事
有人在Stack Overflow上發(fā)問,動手開發(fā)網(wǎng)站之前,需要知道哪些事情,這里簡單為大家整理下,方便需要的朋友2014-03-03