Apache Avro™是一个数据序列化系统。
Avro提供:
所谓shemas,就是一种描述文件,能够说清楚你的对象的描述文件。可能有你对象里面有什么,他们之间的逻辑关系,数据的统计信息。比如说对人的描述包括身高、体重、三维、性别等,只要他的格式确定,就可以叫做shemas。
Avro也是依赖于schemas的(为了夸语言,也为了持久化,总要有二进制数据解析的指导)。当读取Avro数据的时候需要使用到schemas, 当写数据的时候也需要有schema的指导。这样就允许了每个数据都没有前缀开销(因为可以用schema来理解数据),这就会使得序列化很快而且数据会比较小。
当把Avro数据存储到文件的时候,会把他的schema数据一起存储,这样数据就可以被接下来的任意程序使用了。如果程序希望用其他的schema来读取数据,是很简单的,因为你可以同个比较两个schema来看能不能读取数据
当Avro用于RPC的时候,客户端和服务端可以在连接握手的阶段进行schema交换。(这种方式是可以被优化的,因为对于很多schema来说,是没有schema的交换的)由于两边都有对方完整的schema存在了,所有可以比较出同名域、缺少的域、多余的域。
avro的schema是用json定义的。这样有json包的语言就会比较容易实现了。
Avro提供的功能类似于Thrift, Protocol Buffers等系统。Avro在以下基本方面与这些系统不同。
1.序列化和反序列化
<dependency> <groupId>org.apache.avro</groupId> <artifactId>avro</artifactId> <version>1.8.2</version> </dependency>
<dependency> <groupId>org.apache.avro</groupId> <artifactId>avro-tools</artifactId> <version>1.8.2</version> </dependency>
{"namespace": "example.avro", "type": "record", "name": "User", "fields": [ {"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["int", "null"]}, {"name": "favorite_color", "type": ["string", "null"]} ] }
java -jar /path/to/avro-tools-1.8.2.jar compile schema user.avsc .
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();
// Serialize user1, user2 and user3 to disk 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();
// Deserialize Users from disk 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); }
This snippet will output:
{"name": "Alyssa", "favorite_number": 256, "favorite_color": null} {"name": "Ben", "favorite_number": 7, "favorite_color": "red"} {"name": "Charlie", "favorite_number": null, "favorite_color": "blue"}
First, we use a Parser to read our schema definition and create a Schema object.
Schema schema = new Schema.Parser().parse(new File("user.avsc"));
Using this schema, let's create some users.
GenericRecord user1 = new GenericData.Record(schema); user1.put("name", "Alyssa"); user1.put("favorite_number", 256); // Leave favorite color null GenericRecord user2 = new GenericData.Record(schema); user2.put("name", "Ben"); user2.put("favorite_number", 7); user2.put("favorite_color", "red");
// Serialize user1 and user2 to disk File file = new File("users.avro"); DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(schema); DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(datumWriter); dataFileWriter.create(schema, file); dataFileWriter.append(user1); dataFileWriter.append(user2); dataFileWriter.close();
Finally, we'll deserialize the data file we just created.
// Deserialize users from disk DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema); DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(file, datumReader); GenericRecord 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);
This outputs:
{"name": "Alyssa", "favorite_number": 256, "favorite_color": null} {"name": "Ben", "favorite_number": 7, "favorite_color": "red"}
2.RPC