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.包含两个文件
总结
以上所述是小编给大家介绍的Hadoop文件的存储格式实例详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
1.现在复习的感觉就是:马上要有一大波僵尸涌过来,但老子连向日葵都还没种! 2...
这里尊托云数小编参考了几篇文章特为大家整理下,用到的朋友多支持一下了。 进行...
20210323第一家量产国产化蓝牙AOA高精度定位基站生态合能培训会上海站现场直播下...
堆 Heap Heap:可以迅速找到一堆数中的 最大 或者 最小 值的数据结构。 将根节点...
Epoll 是个很老的知识点,是后端工程师的经典必修课。这种知识具备的特点就是研...
戳蓝字“ CSDN云计算 ”关注我们哦 作者 | 刘丹 出品 | CSDN云计算IDCSDNcloud ...
IViewLocationExpander API ExpandViewLocations Razor视图路径,视图引擎会搜索...
详解Spring mvc ant path的使用方法 概要: 任何一个WEB都需要解决URL与请求处理...
WebService端代码 复制代码 代码如下: /// summary /// 上传文件到远程服务器 //...
项目背景 最近项目里有个webpack版本较老的项目,由于升级和换框架暂时不被leade...