hadoop2 (2017-7-21修改)对SequeneceFile 的(读写)操作

来源:互联网 发布:setup.exe mac 打不开 编辑:程序博客网 时间:2024/06/03 23:03

写操作


根据上一篇的介绍,在hadoop2.x之后,Hadoop中的SequenceFile.Writer将会逐渐摒弃大量的createWriter()重载方法,而整合为更为简洁的createWriter()方法,除了配置参数外,其他的参数统统使用SequenceFile.Writer.Option来替代,具体有:


新的API里提供的option参数:


FileOption
FileSystemOption
StreamOption
BufferSizeOption
BlockSizeOption
ReplicationOption
KeyClassOption
ValueClassOption
MetadataOption
ProgressableOption
CompressionOption

这些参数能够满足各种不同的需要,参数之间不存在顺序关系,这样减少了代码编写工作量,更为直观,便于理解,下面先来看看这个方法,后边将给出一个具体实例。


createWriter
public static org.apache.hadoop.io.SequenceFile.Writer createWriter(Configuration conf,org.apache.hadoop.io.SequenceFile.Writer.Option  opts)
  throws IOException


Create a new Writer with the given options.


Parameters:
conf - the configuration to use
opts - the options to create the file with


Returns:
a new Writer
Throws:
IOException


(以下实例已经亲测修改)

权威指南第四版中提供了一个SequenceFileWriteDemo实例:

[java] view plain copy
  1. // cc SequenceFileWriteDemo Writing a SequenceFile  
  2. import java.io.IOException;  
  3. import java.net.URI;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.FileSystem;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.IOUtils;  
  9. import org.apache.hadoop.io.IntWritable;  
  10. import org.apache.hadoop.io.SequenceFile;  
  11. import org.apache.hadoop.io.Text;  
  12.   
  13. // vv SequenceFileWriteDemo  
  14. public class SequenceFileWriteDemo {  
  15.     
  16.   private static final String[] DATA = {  
  17.     "One, two, buckle my shoe",  
  18.     "Three, four, shut the door",  
  19.     "Five, six, pick up sticks",  
  20.     "Seven, eight, lay them straight",  
  21.     "Nine, ten, a big fat hen"  
  22.   };  
  23.     
  24.   public static void main(String[] args) throws IOException {  
  25.     String uri = "file:///E://IDEA//aa.txt"; 

  26.     Configuration conf = new Configuration();  
  27.     conf.set("fs.default.name","hdfs://172.16.11.222:9000");

  28.     FileSystem fs = FileSystem.get(URI.create(uri), conf);  
  29.     Path path = new Path(uri);  
  30.   
  31.     IntWritable key = new IntWritable();  
  32.     Text value = new Text();  
  33.     SequenceFile.Writer writer = null;  
  34.     try {  
  35.       writer = SequenceFile.createWriter(fs, conf, path,  
  36.           key.getClass(), value.getClass());  
  37.         
  38.       for (int i = 0; i < 100; i++) {  
  39.         key.set(100 - i);  
  40.         value.set(DATA[i % DATA.length]);  
  41.         System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key, value);  
  42.         writer.append(key, value);  
  43.       }  
  44.     } finally {  
  45.       IOUtils.closeStream(writer);  
  46.     }  
  47.   }  
  48. }  
  49. // ^^ SequenceFileWriteDemo  


对于上面实例中的createWriter()方法用整合之后的最新的方法来改写一下,代码如下:

[java] view plain copy
  1. package org.apache.hadoop.io;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.Path;  
  7. import org.apache.hadoop.io.IOUtils;  
  8. import org.apache.hadoop.io.IntWritable;  
  9. import org.apache.hadoop.io.SequenceFile;  
  10. import org.apache.hadoop.io.SequenceFile.Writer;  
  11. import org.apache.hadoop.io.SequenceFile.Writer.FileOption;  
  12. import org.apache.hadoop.io.SequenceFile.Writer.KeyClassOption;  
  13. import org.apache.hadoop.io.SequenceFile.Writer.ValueClassOption;  
  14. import org.apache.hadoop.io.Text;  
  15.   
  16. public class THT_testSequenceFile2 {  
  17.   
  18.     private static final String[] DATA = { "One, two, buckle my shoe",  
  19.             "Three, four, shut the door""Five, six, pick up sticks",  
  20.             "Seven, eight, lay them straight""Nine, ten, a big fat hen" };  
  21.   
  22.     public static void main(String[] args) throws IOException {  
  23.         // String uri = args[0];  
  24.         
    String uri = "file:///E://IDEA//bb.txt"
     
  25.         Configuration conf = new Configuration();  
  26. conf.set("fs.default.name""hdfs://172.16.11.222:9000");
  27.         Path path = new Path(uri);  
  28.   
  29.         IntWritable key = new IntWritable();  
  30.         Text value = new Text();  
  31.         SequenceFile.Writer writer = null;  
  32.         SequenceFile.Writer.FileOption option1 = (FileOption) Writer.file(path);  
  33.         SequenceFile.Writer.KeyClassOption option2 = (KeyClassOption) Writer.keyClass(key.getClass());  
  34.         SequenceFile.Writer.ValueClassOption option3 = (ValueClassOption) Writer.valueClass(value.getClass());  
  35.           
  36.         try {  
  37.               
  38.             writer = SequenceFile.createWriter( conf, option1,option2,option3,Writer.compression(CompressionType.RECORD));  
  39.               
  40.             for (int i = 0; i < 10; i++) {  
  41.                 key.set(1 + i);  
  42.                 value.set(DATA[i % DATA.length]);  
  43.                 System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key,  
  44.                         value);  
  45.                 writer.append(key, value);  
  46.             }  
  47.         } finally {  
  48.             IOUtils.closeStream(writer);  
  49.         }  
  50.     }  
  51. }  

运行结果如下:


[html] view plain copy
  1. 2017-07-20 22:15:05,027 INFO  compress.CodecPool (CodecPool.java:getCompressor(153)) - Got brand-new compressor [.deflate]  
  2. [128]   1   One, two, buckle my shoe  
  3. [173]   2   Three, four, shut the door  
  4. [220]   3   Five, six, pick up sticks  
  5. [264]   4   Seven, eight, lay them straight  
  6. [314]   5   Nine, ten, a big fat hen  
  7. [359]   6   One, two, buckle my shoe  
  8. [404]   7   Three, four, shut the door  
  9. [451]   8   Five, six, pick up sticks  
  10. [495]   9   Seven, eight, lay them straight  
  11. [545]   10  Nine, ten, a big fat hen  




生成的文件:




读操作


新的API里提供的option参数:


FileOption -表示读哪个文件
InputStreamOption
StartOption
LengthOption -按照设置的长度变量来决定读取的字节
BufferSizeOption
OnlyHeaderOption


根据最新的API直接上源码:


[java] view plain copy
  1. package org.apache.hadoop.io;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.Path;  
  7. import org.apache.hadoop.io.IOUtils;  
  8. import org.apache.hadoop.io.SequenceFile;  
  9. import org.apache.hadoop.io.SequenceFile.Reader;  
  10. import org.apache.hadoop.io.Writable;  
  11. import org.apache.hadoop.util.ReflectionUtils;  
  12.   
  13. public class THT_testSequenceFile3 {  
  14.   
  15.     public static void main(String[] args) throws IOException {  
  16.         //String uri = args[0];  
  17.         String uri = "file:///E://IDEA//bb.txt";  
  18.         Configuration conf = new Configuration();  
  19.         Path path = new Path(uri);  
  20.         SequenceFile.Reader.Option option1 = Reader.file(path);  
  21.         SequenceFile.Reader.Option option2 = Reader.length(174);//这个参数表示读取的长度  
  22.         SequenceFile.Reader reader = null;  
  23.         try {  
  24.             reader = new SequenceFile.Reader(conf,option1,option2);  
  25.             Writable key = (Writable) ReflectionUtils.newInstance(  
  26.                     reader.getKeyClass(), conf);  
  27.             Writable value = (Writable) ReflectionUtils.newInstance(  
  28.                     reader.getValueClass(), conf);  
  29.             long position = reader.getPosition();  
  30.             while (reader.next(key, value)) {  
  31.                 String syncSeen = reader.syncSeen() ? "*" : "";  
  32.                 System.out.printf("[%s%s]\t%s\t%s\n", position, syncSeen, key,  
  33.                         value);  
  34.                 position = reader.getPosition(); // beginning of next record  
  35.             }  
  36.         } finally {  
  37.             IOUtils.closeStream(reader);  
  38.         }  
  39.     }  
  40. }  

我这儿设置了一个读取长度的参数,只读到第174个字节那,所以运行结果如下:


[java] view plain copy
  1. 2017-07-20 22:15:05,089 INFO  compress.CodecPool (CodecPool.java:getDecompressor(181)) - Got brand-new decompressor [.deflate]  
  2. [128]   1   One, two, buckle my shoe  
  3. [173]   2   Three, four, shut the door 
阅读全文
0 0
原创粉丝点击