感受最快的序列化和反序列化工具:Avro
参考阅读:
[url]http://tech.meituan.com/serialization_vs_deserialization.html[/url]
官网:
[url]http://avro.apache.org/docs/current/gettingstartedjava.html#Creating+users[/url]
代码结构图:
[img]http://dl2.iteye.com/upload/attachment/0108/9625/bb06ca9d-3639-3b5b-9c11-d7b405282407.jpg[/img]
pmo 引入:
user.avsc
特殊注意:通过以上avsc文件可以直接生成User类代码。
参考阅读:
[url]http://tech.meituan.com/serialization_vs_deserialization.html[/url]
官网:
[url]http://avro.apache.org/docs/current/gettingstartedjava.html#Creating+users[/url]
代码结构图:
[img]http://dl2.iteye.com/upload/attachment/0108/9625/bb06ca9d-3639-3b5b-9c11-d7b405282407.jpg[/img]
pmo 引入:
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.7.7</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.7.7</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
user.avsc
{"namespace": "com.gym.backadmin.controller",
"type": "record",
"name": "User",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number", "type": ["int", "null"]},
{"name": "favorite_color", "type": ["string", "null"]}
]
}
特殊注意:通过以上avsc文件可以直接生成User类代码。
public class Deserializing {
public static void main(String[] args) throws Exception {
File file = new File("users.avro");
DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class);
DataFileReader<User> dataFileReader = new DataFileReader<User>(file, userDatumReader);
User user = null;
while (dataFileReader.hasNext()) {
// Reuse user object by passing it to next(). This saves us from
// allocating and garbage collecting many objects for files with
// many items.
user = dataFileReader.next(user);
System.out.println(user);
}
}
}
public class Serializing {
public static void main(String[] args) throws IOException {
User user1 = new User();
user1.setName("Alyssa");
user1.setFavoriteNumber(256);
// Leave favorite color null
// Alternate constructor
User user2 = new User("Ben", 7, "red");
// Construct via builder
User user3 = User.newBuilder()
.setName("Charlie")
.setFavoriteColor("blue")
.setFavoriteNumber(null)
.build();
DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class);
DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter);
dataFileWriter.create(user1.getSchema(), new File("users.avro"));
dataFileWriter.append(user1);
dataFileWriter.append(user2);
dataFileWriter.append(user3);
dataFileWriter.close();
}
}