Hadoop自定义可序列化的类

来源:互联网 发布:php统计字符串长度 编辑:程序博客网 时间:2024/05/18 13:45

在hadoop框架中实现自定义类可以被序列化。

[java] view plaincopy
  1. package com.rpc.nefu;  
  2.   
  3. import java.io.DataInput;  
  4. import java.io.DataOutput;  
  5. import java.io.IOException;  
  6.   
  7. import org.apache.hadoop.io.IntWritable;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.io.WritableComparable;  
  10.   
  11. //自定义的序列化方法,只需要实现WritableComparable接口,重写一些方法即可 主要就是 readFields() write() compareTo()  
  12. public class personWritable implements WritableComparable<personWritable>{  
  13.     private Text name;  
  14.     private IntWritable age;  
  15.     private Text sex;  
  16.     public personWritable(){  
  17.         set("name",-1,"female");  
  18.     }  
  19.     public personWritable(String _name,int _age,String _sex){  
  20.         set(_name,_age,_sex);  
  21.     }  
  22.     public void set(String name,int age,String sex){  
  23.         this.name = new Text(name);  
  24.         this.age = new IntWritable(age);  
  25.         this.sex = new Text(sex);  
  26.     }  
  27.     //反序列化过程,将datainput的内容还原为hadoop对象  
  28.     @Override  
  29.     public void readFields(DataInput in) throws IOException {  
  30.         // TODO Auto-generated method stub  
  31.         name.readFields(in);  
  32.         age.readFields(in);  
  33.         sex.readFields(in);  
  34.     }  
  35.     //序列化过程  
  36.     @Override  
  37.     public void write(DataOutput out) throws IOException {  
  38.         // TODO Auto-generated method stub  
  39.         name.write(out);  
  40.         age.write(out);  
  41.         sex.write(out);  
  42.     }  
  43.   
  44.     @Override  
  45.     public int compareTo(personWritable other) {  
  46.         // TODO Auto-generated method stub  
  47.         int cmp1 = name.compareTo(other.name);  
  48.         if(cmp1!=0){  
  49.             return cmp1;  
  50.         }  
  51.         int cmp2 = age.compareTo(other.age);  
  52.         if(cmp2!=0){  
  53.             return cmp2;  
  54.         }  
  55.         int cmp3 = sex.compareTo(other.sex);  
  56.         return cmp3;  
  57.     }  
  58.     //判断是否相等  
  59.     public boolean equals(Object o){  
  60.         if(o instanceof personWritable){  
  61.             personWritable pw = (personWritable) o;  
  62.             return name.equals(pw.name)&&age.equals(pw.age)&&sex.equals(pw.sex);  
  63.         }  
  64.         return false;  
  65.     }  
  66.     //哈希值  
  67.     public int hashCode(){  
  68.         return name.hashCode()*3+age.hashCode()*5+sex.hashCode()*7;  
  69.     }  
  70.     public String toString(){  
  71.         StringBuffer sb = new StringBuffer();  
  72.         sb.append("--");  
  73.         sb.append("姓名:"+name+"_");  
  74.         sb.append("年龄:"+age+"_");  
  75.         sb.append("性别:"+sex+"_");  
  76.         sb.append("--");  
  77.         return sb.toString();     
  78.     }  
  79. }  

[java] view plaincopy
  1. package com.rpc.nefu;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.DataInputStream;  
  6. import java.io.DataOutputStream;  
  7. import java.io.IOException;  
  8.   
  9. //import org.apache.hadoop.io.Writable;  
  10.   
  11. //将序列化的对象的内容返回到一个字节数组中去 记录序列的过程  
  12. public class hadoopSerializable {  
  13.     public static byte[] serialize(personWritable writable) throws IOException{  
  14.         //创建一个字节数组  
  15.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  16.         //创建一个DataOutputStream,将字节数组传递进去,保存输出的序列化后的内容  
  17.         DataOutputStream dataout =  new DataOutputStream(out);   
  18.         //让参数的Hadoop对象序列化到字节流中   
  19.         writable.write(dataout);   
  20.         dataout.close();   
  21.         //返回序列化后的字节流   
  22.         return out.toByteArray();   
  23.     }  
  24.       
  25.            /** 
  26.             *这个方法用于反序列化一个字节数组成Hadoop Writable对象  
  27.             *@param writable 反序列化后的Writable对象存放在这个参数中  
  28.             *@param bytes 被反序列化的字节数组 对应于上面序列化的bytes 
  29.             **/  
  30. public static void deserialize(personWritable writable,byte[] bytes) throws Exception{   
  31.           
  32.         ByteArrayInputStream in = new ByteArrayInputStream(bytes);   
  33.         //创建一个DataInputStream   
  34.         DataInputStream datain = new DataInputStream(in);   
  35.         //让Hadoop框架反序列化这个字节数组,还原后的Writable对象存放到第一个参数中   
  36.         writable.readFields(datain);   
  37.           
  38.         datain.close();   
  39.     }   
  40. }  

[java] view plaincopy
  1. package com.rpc.nefu;  
  2.   
  3. import org.apache.hadoop.util.StringUtils;  
  4.   
  5. public class serializeTest {  
  6.     public static void main(String [] args) throws Exception{   
  7.           
  8.         /*把我们自定义的Hadoop可序列化对象进行序列化 */  
  9.         System.out.println("Hadoop--对象序列化");   
  10.         personWritable pw = new personWritable("XD",23,"Male");   
  11.         String imformation= "自定义可序列化Hadoop类型为: "+pw.getClass().getName()+"\n";   
  12.         String primaryPersonWritableInfo = "序列化前对象为:  "+pw.toString()+"\n";   
  13.         //开始序列化过程   
  14.         byte[] serializedValue =hadoopSerializable.serialize(pw);   
  15.         String lengthInfo= "序列化后的字节数组长度为: "+serializedValue.length+"\n";   
  16.         String serializeValueInfo= "序列化后的值为: " +StringUtils.byteToHexString(serializedValue)+"\n";   
  17.    
  18.         System.out.println(imformation+primaryPersonWritableInfo+lengthInfo+serializeValueInfo+"\n");   
  19.            
  20.         System.out.println();   
  21.         //把我们序列化之后的字节数组反序列化为原始Hadoop对象   
  22.         System.out.println("反序列化--Hadoop");   
  23.         personWritable reversePersonWritable = new personWritable();   
  24.         /*StringUtils.byteToHexString 类似将自己数组转化为字符串*/  
  25.         String originalByteArrayInfo="被反序列化的字节数组内容为: "+StringUtils.byteToHexString(serializedValue)+"\n";  
  26.         //开始反序列化过程   
  27.         hadoopSerializable.deserialize(reversePersonWritable, serializedValue);   
  28.         String restoredValueInfo = "反序列化之后的Writable对象为: "+reversePersonWritable.toString();   
  29.         System.out.println(originalByteArrayInfo+restoredValueInfo+"\n");   
  30.     }   
  31. }  
0 0