欢迎投稿

今日深度:

Hadoop 序列化,

Hadoop 序列化,


运用hadoop的序列化 
    在hadoop的框架中要使一个类可序列化,要实现Writable接口的两个方法: 
Java代码  
  1. public interface Writable {  
  2.   void write(DataOutput out) throws IOException;  
  3.   void readFields(DataInput in) throws IOException;  
  4. }  

比java的实现Serializable复杂很多。但是通过比较可以发现,hadoop的序列化机制产生的数据量远小于java的序列化所产生的数据量。 

在这两个方法中自己控制对fileds的输入和输出。如果类中包含有其他对象的引用,那么那个对象也是要实现Writable接口的(当然也可以不实现Writable借口,只要自己处理好对对象的fileds的存贮就可以了)。 
下面是一个简单的例子: 
类Attribute 
Java代码  
  1. package siat.miner.etl.instance  
  2. import java.io.DataInput;  
  3. import java.io.DataOutput;  
  4. import java.io.IOException;  
  5. import org.apache.hadoop.io.IntWritable;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.io.Writable;  
  8.   
  9. public class Attribute implements Writable{  
  10.   
  11.     public static int ATTRIBUTE_TYPE_STRING = 1;//string type  
  12.     public static int ATTRIBUTE_TYPE_NOMINAL = 2;//nominal type  
  13.     public static int ATTRIBUTE_TYPE_REAL = 3;//real type  
  14.       
  15.     private IntWritable type;  
  16.     private Text name;  
  17.     public IntWritable getType() {  
  18.         return type;  
  19.     }  
  20.     public void setType(int type) {  
  21.         this.type = new IntWritable(type);  
  22.     }  
  23.     public Text getName() {  
  24.         return name;  
  25.     }  
  26.     public void setName(String name) {  
  27.         this.name = new Text(name);  
  28.     }  
  29.     public Attribute() {  
  30.         super();  
  31.         this.type = new IntWritable(0);  
  32.         this.name = new Text("");  
  33.     }  
  34.     public Attribute(int type, String name) {  
  35.         super();  
  36.         this.type = new IntWritable(type);  
  37.         this.name = new Text(name);  
  38.     }  
  39.     @Override  
  40.     public void readFields(DataInput in) throws IOException {  
  41.         // TODO Auto-generated method stub  
  42.         type.readFields(in);  
  43.         name.readFields(in);  
  44.           
  45.     }  
  46.     @Override  
  47.     public void write(DataOutput out) throws IOException {  
  48.         // TODO Auto-generated method stub  
  49.         type.write(out);  
  50.         name.write(out);  
  51.           
  52.     }  
  53. }  

类TestA: 
Java代码  
  1. package siat.miner.etl.test;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.DataInput;  
  6. import java.io.DataInputStream;  
  7. import java.io.DataOutput;  
  8. import java.io.DataOutputStream;  
  9. import java.io.IOException;  
  10.   
  11. import org.apache.hadoop.io.IntWritable;  
  12. import org.apache.hadoop.io.Writable;  
  13.   
  14. import siat.miner.etl.instance.Attribute;  
  15.   
  16. public class TestA implements Writable{  
  17.   
  18.     private Attribute a;  
  19.     private IntWritable b;  
  20.     /** 
  21.      * @param args 
  22.      * @throws IOException  
  23.      */  
  24.     public static void main(String[] args) throws IOException {  
  25.         // TODO Auto-generated method stub  
  26.   
  27.         Attribute a = new Attribute(Attribute.ATTRIBUTE_TYPE_NOMINAL, "name");  
  28.         TestA ta = new TestA(a, new IntWritable(1));  
  29.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  30.         DataOutputStream oos = new DataOutputStream(bos);  
  31.         ta.write(oos);  
  32.           
  33.         TestA tb = new TestA();  
  34.         tb.readFields(new DataInputStream(new ByteArrayInputStream(bos.toByteArray())));  
  35.     }  
  36.     public TestA(Attribute a, IntWritable b) {  
  37.         super();  
  38.         this.a = a;  
  39.         this.b = b;  
  40.     }  
  41.     public TestA() {  
  42.         // TODO Auto-generated constructor stub  
  43.     }  
  44.     @Override  
  45.     public void readFields(DataInput in) throws IOException {  
  46.         // TODO Auto-generated method stub  
  47.         a = new Attribute();  
  48.         a.readFields(in);  
  49.         b = new IntWritable();  
  50.         b.readFields(in);  
  51.     }  
  52.     @Override  
  53.     public void write(DataOutput out) throws IOException {  
  54.         // TODO Auto-generated method stub  
  55.         a.write(out);  
  56.         b.write(out);  
  57.     }  
  58.   
  59. }  


可以看到,hadoop的序列化机制就是利用java的DataInput和DataOutput来完成对基本类型的序列化,然后让用户自己来处理对自己编写的类的序列化。 

www.htsjk.Com true http://www.htsjk.com/Hadoop/41296.html NewsArticle Hadoop 序列化, 运用hadoop的序列化       在hadoop的框架中要使一个类可序列化,要实现Writable接口的两个方法:  Java代码   public   interface  Writable {      void  write(DataOutput out) ...
相关文章
    暂无相关文章
评论暂时关闭