package cn.test.serializable;
/**
* 序列化对象实现Serializable接口
* @author Young
* created on 2017-5-25
*/
import java.io.Serializable;
public class Student implements Serializable {
private String id;
private String name;
public Student (String id,String name){
this.id=id;
this.name=name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package cn.test.serializable;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
/**
* 序列化
* @author Young
* created on 2017-5-25
*/
public class TestSerializable {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu=new Student("201441413110","yang");
try {
FileOutputStream out=new FileOutputStream("d:\student");//创建OutputStream对象
ObjectOutputStream ob=new ObjectOutputStream(out);//封装在一个ObjectOutputStream对象内
ob.writeObject(stu);//调用writeObject()方法将对象序列化并发送给OutputStream,保存在d:\student中
ob.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package cn.test.serializable;
/**
* 反序列化
* @author Young
* created on 2017-5-25
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class TestDeserializable {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream in;
Student stu;
try {
in = new FileInputStream("d:\student");
ObjectInputStream ob=new ObjectInputStream(in);//InputStream封装在ObjectInputStream内
stu=(Student) ob.readObject();//调用readObject(),得到的结果是一个Object对象,需要进行转型得到最后所需的对象.
ob.close();
in.close();
System.out.println(stu.getId());
System.out.println(stu.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package Hadoop.writable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Writable;
/**
* hadoop 序列化与反序列化
* @author Young
* created by 2017-6-9
*/
public class Serialize {
public static byte[] serialize(Writable writable) throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataout = new DataOutputStream(out);
try {
writable.write(dataout);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
dataout.close();
}
return out.toByteArray();
}
public static byte[] deserialize(Writable writable ,byte[] bytes) throws IOException{
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
DataInputStream datain = new DataInputStream(in);
try {
writable.readFields(datain);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
datain.close();
}
return bytes;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
IntWritable intwritable = new IntWritable(20);
IntWritable newwriatble =new IntWritable();
byte[] bytes=Serialize.serialize(intwritable);
deserialize(newwriatble,bytes);
System.out.println(newwriatble.get());
}
}
package org.apache.hadoop.io;
import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Stable;
import org.apache.hadoop.io.Writable;
@Public
@Stable
public interface WritableComparable<T> extends Writable, Comparable<T> {
}
package org.apache.hadoop.io;
import java.util.Comparator;
import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Stable;
@Public
@Stable
public interface RawComparator<T> extends Comparator<T> {
int compare(byte[] arg0, int arg1, int arg2, byte[] arg3, int arg4, int arg5);
}
RawComparator<IntWritable>comparator=WritableComparator.get(IntWritable.class);
package cn.serialization;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.RawComparator;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparator;
public class TestWritable {
//序列化
public static byte[] serialize(Writable writable) throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataOut= new DataOutputStream(out);
writable.write(dataOut);
dataOut.close();
return out.toByteArray();
}
//反序列化
public static byte[] deserialize(Writable writable, byte[] bytes) throws IOException{
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
DataInputStream dataIn = new DataInputStream(in);
writable.readFields(dataIn);
dataIn.close();
return bytes;
}
public static void main(String[] args) throws IOException {
@SuppressWarnings("unchecked")
RawComparator <IntWritable> comparator = WritableComparator.get(IntWritable.class);
IntWritable w1 = new IntWritable(163);
IntWritable w2 = new IntWritable(67);
byte[] b1 = serialize(w1);
byte[] b2 = serialize(w2);
System.out.println(b1);
System.out.println(b2);
System.out.println(comparator.compare(w1, w2));//WritableComparator的 compare(),w1>w2 -> 1;w1<w2 -> -1 ; w1=w2 -> 0
System.out.println(comparator.compare(b1,0,b1.length,b2,0,b2.length));
}
}
public class WritableSerialization extends Configured implements Serialization<Writable> {...}
public interface Serializer<T> {
void open(OutputStream arg0) throws IOException;
void serialize(T arg0) throws IOException;
void close() throws IOException;
}
public interface Deserializer<T> {
void open(InputStream arg0) throws IOException;
T deserialize(T arg0) throws IOException;
void close() throws IOException;
}
public interface Serialization<T> {
boolean accept(Class<?> arg0);
Serializer<T> getSerializer(Class<T> arg0);
Deserializer<T> getDeserializer(Class<T> arg0);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有