小李飞刀资源网 Design By www.iooab.com
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文件的存储格式实例详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
小李飞刀资源网 Design By www.iooab.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
小李飞刀资源网 Design By www.iooab.com
暂无评论...
更新日志
2024年10月28日
2024年10月28日
- 群星2013-青春缤纷辑压箱宝大公开3CD2[新加坡限量版][WAV整轨]
- 林育群.2013-BalladShow(日本版)【环球】【WAV+CUE】
- 陈加洛.1992-痛到感觉不到【宝丽金】【WAV+CUE】
- 群星.2023-宿命之敌电视剧原声带【韶愔音乐】【FLAC分轨】
- 東京事変-大発見[FLAC+CUE]
- 椎名林檎-三文ゴシップ[FLAC+CUE]
- 2024年08月04日
- 裘德《裘德「最后的水族馆」演唱会LIVE》[320K/MP3][228.89MB]
- 裘德《裘德「最后的水族馆」演唱会LIVE》[24bit 48kHz][FLAC/分轨][2.08G]
- 基因三重奏《如果你什么都不说 音乐会现场录音》[320K/MP3][145.37MB]
- 孟庭苇.1996-月亮说话(2020环球24KGOLD限量版)【上华】【WAV+CUE】
- 群星.1997-新艺宝优质音响系列·国语精选监听版【新艺宝】【WAV+CUE】
- 阿桑.2005-寂寞在唱歌(星外星引进版)【华研国际】【WAV+CUE】
- 基因三重奏《如果你什么都不说 音乐会现场录音》[FLAC/分轨][287.43MB]
- 蔡题谦《我爱你,却依然要看你走》[320K/MP3][88.65MB]