ITPub博客

首页 > 大数据 > Hadoop > mapreduce 编程SequenceFile类的使用

mapreduce 编程SequenceFile类的使用

Hadoop 作者:破棉袄 时间:2014-08-25 12:30:34 0 删除 编辑
原:http://blog.csdn.net/zyj8170/article/details/7178371 

1.对于某些应用而言,需要特殊的数据结构来存储自己的数据。对于基于MapReduce的数据处理,将每个二进制数据的大对象融入自己的文件中并不能实现很高的可扩展性,针对上述情况,Hadoop开发了一组更高层次的容器SequenceFile。


     2. 考虑日志文件,其中每一条日志记录是一行文本。如果想记录二进制类型,纯文本是不合适的。这种情况下,Hadoop的SequenceFile类非常合适,因为上述提供了二进制键/值对的永久存储的数据结构。当作为日志文件的存储格式时,可以自己选择键,比如由LongWritable类型表示的时间戳,以及值可以是Writable类型,用于表示日志记录的数量。SequenceFile同样为可以作为小文件的容器。而HDFS和 MapReduce是针对大文件进行优化的,所以通过SequenceFile类型将小文件包装起来,可以获得更高效率的存储和处理。
     3. SequenceFile类内部有两个比较主要的内部类分别是SequenceFile.Reader和SequenceFile.Writer
      SequenceFile.Reader
      通过createWriter()静态方法可以创建SequenceFile对象,并返SequenceFile.Writer实例。该静态方法有多个重载版本,但都需要指定待写入的数据流(FSDataOutputStream或FileSystem对象和Path对象),Configuration对象,以及键和值的类型。另外可选参数包括压缩类型以及相应的codec,Progressable回调函数用于通知写入的进度,以及在SequenceFile头文件中存储的Metadata实例。存储在SequenceFile中的键和值对并不一定是Writable类型。任意可以通过Serialization类实现序列化和反序列化的类型均可被使用。一旦拥有SequenceFile.Writer实例,就可以通过append()方法在文件末尾附件键/值对。
    SequenceFile.Writer
    创建SequenceFile.Writer可以通过调用本身的构造函数 SequenceFile.Reader(FileSystem fs, Path file, Configuration conf) 来构造实例对象,从头到尾读取顺序文件的过程是创建SequenceFile.Reader实例后反复调用next()方法迭代读取记录的过程。读取的是哪条记录与你使用的序列化框架相关。如果使用的是Writable类型,那么通过键和值作为参数的Next()方法可以将数据流中的下一条键值对读入变量中:
     public boolean next(Writable key,Writable val),如果键值对成功读取,则返回true,如果已读到文件末尾,则返回false。

下面是我练习时调试的代码:


[java] view plaincopy
  1. import java.io.IOException;  
  2. import java.net.URI;  
  3. import java.util.Random;  
  4.   
  5.   
  6.   
  7. import org.apache.hadoop.conf.Configuration;  
  8. import org.apache.hadoop.fs.FileSystem;  
  9. import org.apache.hadoop.fs.Path;  
  10. import org.apache.hadoop.io.IntWritable;  
  11. import org.apache.hadoop.io.SequenceFile;  
  12. import org.apache.hadoop.io.Text;  
  13.   
  14.   
  15. public class sequence {  
  16.   
  17.     /** 
  18.      * @param args 
  19.      */  
  20.     public static  FileSystem fs;  
  21.     public static final String Output_path="/home/hadoop/test/A.txt";  
  22.     public static Random random=new Random();  
  23.     private static final String[] DATA={  
  24.           "One,two,buckle my shoe",  
  25.           "Three,four,shut the door",  
  26.           "Five,six,pick up sticks",  
  27.           "Seven,eight,lay them straight",  
  28.           "Nine,ten,a big fat hen"  
  29.          };  
  30.     public static Configuration conf=new Configuration();  
  31.     public static void write(String pathStr) throws IOException{  
  32.         Path path=new Path(pathStr);  
  33.         FileSystem fs=FileSystem.get(URI.create(pathStr), conf);  
  34.         SequenceFile.Writer writer=SequenceFile.createWriter(fs, conf, path, Text.class, IntWritable.class);  
  35.         Text key=new Text();  
  36.         IntWritable value=new IntWritable();  
  37.         for(int i=0;i
  38.             key.set(DATA[i]);  
  39.             value.set(random.nextInt(10));  
  40.             System.out.println(key);  
  41.             System.out.println(value);  
  42.              
  43.             System.out.println(writer.getLength());  
  44.             writer.append(key, value);  
  45.              
  46.         }  
  47.         writer.close();  
  48.          
  49.     }  
  50.     public static void read(String pathStr) throws IOException{  
  51.         FileSystem fs=FileSystem.get(URI.create(pathStr), conf);  
  52.         SequenceFile.Reader reader=new SequenceFile.Reader(fs, new Path(pathStr), conf);  
  53.         Text key=new Text();  
  54.         IntWritable value=new IntWritable();  
  55.         while(reader.next(key, value)){  
  56.             System.out.println(key);  
  57.             System.out.println(value);  
  58.         }  
  59.          
  60.     }  
  61.      
  62.     public static void main(String[] args) throws IOException {  
  63.         // TODO Auto-generated method stub  
  64.         write(Output_path);  
  65.         read(Output_path);  
  66.     }     
  67.   
  68. }  

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29754888/viewspace-1257226/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: oozie java api
下一篇: hadoop Kerberos过程
请登录后发表评论 登录
全部评论

注册时间:2014-07-16

  • 博文量
    180
  • 访问量
    1158211