sequence文件存储格式
1.txt
纯文本格式,若干行记录。默认用字符编码存储
2.SequenceFile格式(顺序文件格式,可进行切割)
key-value 格式进行存储,最终形成的是一个二进制文件, 需用hadoop提供的api进行写入存储。
编写 写入 seq文件案例。
1 2 3 4 5 6 7 8 9 10 | Configuration configuration = new Configuration(); configuration.set(fs.defaultFS,http://导航http://导航http://导航http://导航hdfs://s100:8020); FileSystem fileSystem = FileSystem.get(configuration); Path path = new Path(http://导航http://导航http://导航http://导航http://导航http://导航http://导航http://导航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 文件案例
1 2 3 4 5 6 7 8 9 10 | Configuration configuration = new Configuration(); configuration.set(fs.defaultFS,http://导航http://导航http://导航http://导航hdfs://s100:8020); FileSystem fileSystem = FileSystem.get(configuration); Path path = new Path(http://导航http://导航http://导航http://导航http://导航http://导航http://导航http://导航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.查看文件内容
1 2 | $> hdfs dfs -text /user/myfile.seq $> hdfs dfs -cat /user/myfile.seq (此命令查看会出现乱码) |
seq 文件格式解析
顺序文件由文件头和随后的一条或多条记录组成
1 2 3 4 5 6 7 8 9 | ---文件头------ --key-value----sync --key-value---- --key-value---- --key-value---- --key-value----sync --key-value---- --key-value---- --key-value----sync |
文件头格式
SEQ+版本号+key类型class+value类型class + 压缩格式类型
代码案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | /** * 读取文件位置 */ public void seekSeq() throws IOException { Configuration configuration = new Configuration(); configuration.set(fs.defaultFS,http://导航http://导航http://导航http://导航hdfs://s100:8020); FileSystem fileSystem = FileSystem.get(configuration); Path path = new Path(http://导航http://导航http://导航http://导航http://导航http://导航http://导航http://导航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,http://导航http://导航http://导航http://导航hdfs://s100:8020); FileSystem fileSystem = FileSystem.get(configuration); Path path = new Path(http://导航http://导航http://导航http://导航http://导航http://导航http://导航http://导航hdfs://s100:8020/user/seqmyfile.seq); SequenceFile.Reader sr = new SequenceFile.Reader(fileSystem,path,configuration); IntWritable key = new IntWritable(); Text value = new Text(); |