公司用Kryo,先接触下,简单记录下。
引入包
<dependencies>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
简单的使用
实体对象:
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
使用kryo序列化和反序列化:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.junit.Test;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
public class HowToUse {
@Test
public void output() throws FileNotFoundException {
User user = new User();
user.setId(100);
user.setName("Nick Huang");
try (Output output = new Output(new FileOutputStream("d:/binary/user.bin"))) {
Kryo kryo = new Kryo();
kryo.writeObject(output, user);
}
}
@Test
public void input() throws FileNotFoundException {
try (Input input = new Input(new FileInputStream("d:/binary/user.bin"))) {
Kryo kryo = new Kryo();
User user = kryo.readObject(input, User.class);
System.out.println(user);
}
}
}
简单地和JDK自带序列化和反序列化对比
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
public class Compare {
public static void main(String[] args) throws Exception {
outputByKryo();
inputByKryo();
outputByJdk();
inputByJdk();
}
public static void outputByKryo() throws FileNotFoundException {
long startTime = System.currentTimeMillis();
for (int i = 10000; i < 20000; i++) {
User user = new User();
user.setId(i);
user.setName("Nick Huang");
try (Output output = new Output(new FileOutputStream("d:/binary/user" + i + ".bin"))) {
Kryo kryo = new Kryo();
kryo.writeObject(output, user);
}
}
System.out.println("outputByKryo cost : " + (System.currentTimeMillis() - startTime));
}
public static void inputByKryo() throws FileNotFoundException {
long startTime = System.currentTimeMillis();
for (int i = 10000; i < 20000; i++) {
try (Input input = new Input(new FileInputStream("d:/binary/user" + i + ".bin"))) {
Kryo kryo = new Kryo();
User user = kryo.readObject(input, User.class);
}
}
System.out.println("inputByKryo cost : " + (System.currentTimeMillis() - startTime));
}
public static void outputByJdk() throws IOException {
long startTime = System.currentTimeMillis();
for (int i = 20000; i < 30000; i++) {
User user = new User();
user.setId(i);
user.setName("Nick Huang");
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("d:/binary/user" + i + ".bin"))) {
output.writeObject(user);
}
}
System.out.println("outputByJdk cost : " + (System.currentTimeMillis() - startTime));
}
public static void inputByJdk() throws IOException, ClassNotFoundException {
long startTime = System.currentTimeMillis();
for (int i = 20000; i < 30000; i++) {
try (ObjectInputStream input = new ObjectInputStream(new FileInputStream("d:/binary/user" + i + ".bin"))) {
User user = (User)input.readObject();
}
}
System.out.println("inputByJdk cost : " + (System.currentTimeMillis() - startTime));
}
}
日志:
outputByKryo cost : 5856
inputByKryo cost : 516
outputByJdk cost : 7494
inputByJdk cost : 1142