java与hadoop的序列化、反序列化(一)

序列化:将对象转化成流的过程称为序列化

反序列化:将流转化成对象的过程称之为反序列化

一,先看下两者在代码使用上的区别:

1,java的序列化与反序列化

java中要想使一个java对象可以实现序列化与反序列化,必须让该类实现java.io.Serializable接口

java.io.Serializable接口定义如下:

publicinterface Serializable {

}

  序列化主要依赖java.io.ObjectOutputStream,该类对java.io.FileOutputStream进一步做了封装,这里主要使用ObjectOutputStream类的writeObject()方法实现序列化功能

下面以对象类Person为例,看看java序列化与反序列化的操作代码,要序列化的java类:

/**
 *
 * @author xuemin
 * @date 2014-7-8
 *
 */
public class Person implements Serializable {

public Person(){

}
public Person(String name,int age){
this.name = name;
this.age = age;
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

序列化与反序列化demo

/**
 *
 * @author xuemin
 * @date 2014-7-8
 *
 */
public class JavaSerializeTest {


public static void main(String[] args) throws IOException, ClassNotFoundException {

Person person = new Person("zhangsan",1);
Person person2 = new Person("张三",2);
File file = new File("d://javaSerialize.txt");

/*
* 序列化到本地磁盘
*/

FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);

objectOutputStream.writeObject(person);

objectOutputStream.writeObject(person2);
objectOutputStream.writeObject("\t");
objectOutputStream.close();


/*
* 反序列化
*/

FileInputStream fileInputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

Person p = (Person) objectInputStream.readObject();
System.out.println(p);
p= (Person) objectInputStream.readObject();
System.out.println(p);

//上面序列化的时候写入了两个对象,此处若第三次读取,所以抛出了java.io.EOFExceptionat java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)的异常
/*p= (Person) objectInputStream.readObject();
System.out.println(p);*/

//那么应该怎么处理,能让程序读取到文件尾停止呢?我所想到的解决方案,已测可用:1,序列化时把所有对象放入数组或者容器中 2,在写入的末尾追加特殊对象或者特殊值判定
Object object = objectInputStream.readObject();
if(object instanceof String)
return;
}
}


2,hadoop(hdfs)的序列化与反序列化(其他细节不表)

/**
 *
 * @author xuemin
 * @date 2014-7-8
 *
 */
public class HadoopSequenceTest {


public static void main(String[] args) throws IOException {

Configuration conf=new Configuration();  
FileSystem fs=FileSystem.get(conf);  
Path seqFile=new Path("/zxm/hadoopSequence.txt");  

/*
* 序列化对象到hdfs文件中
*/
//Writer内部类用于文件的写操作,假设Key和Value都为Text类型  
SequenceFile.Writer writer=new SequenceFile.Writer(fs,conf,seqFile,Text.class,Text.class);  
//通过writer向文档中写入记录  
writer.append(new Text("key1"),new Text("value1")); 
writer.append(new Text("key2"),new Text("value2")); 
IOUtils.closeStream(writer);//关闭write流  


/*
* 反序列化
*/
//Reader内部类用于文件的读取操作  
SequenceFile.Reader reader=new SequenceFile.Reader(fs,seqFile,conf);  
//通过reader从文档中读取记录  
Text key=new Text();  
Text value=new Text();  
while(reader.next(key,value)){  
   System.out.println(key);  
   System.out.println(value);  
}  
IOUtils.closeStream(reader);//关闭read流  
}
}

参考:

http://www.blogjava.net/sxyx2008/archive/2009/11/09/301663.html

http://blog.csdn.net/javaman_chen/article/details/7241087



你可能感兴趣的:(hadoop)