内容简介:路径:src/main/avro/user.avsc生成类:src/main/java/me/zhongmingmao/avro/User.java转载请注明出处:http://zhongmingmao.me/2018/10/16/kafka-avro-introduction/
<dependency> <groupId>org.apache.avro</groupId> <artifactId>avro</artifactId> <version>1.8.2</version> </dependency>
<plugin> <groupId>org.apache.avro</groupId> <artifactId>avro-maven-plugin</artifactId> <version>1.8.2</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>${java.version}</source> <target>${java.version}</target> </configuration> </plugin>
Schema
路径:src/main/avro/user.avsc
{ "namespace": "me.zhongmingmao.avro", "type": "record", "name": "User", "fields": [ {"name": "name", "type": "string"}, {"name": "favorite_number", "type": ["int", "null"]}, {"name": "favorite_color", "type": ["string", "null"]} ] }
使用Avro – 生成代码
编译Schema
mvn clean compile
生成类:src/main/java/me/zhongmingmao/avro/User.java
序列化
User user1 = new User(); user1.setName("A"); user1.setFavoriteNumber(1); User user2 = new User("B", 2, "c2"); User user3 = User.newBuilder().setName("C").setFavoriteNumber(3).setFavoriteColor("c3").build(); DatumWriter<User> userDatumWriter = new SpecificDatumWriter<>(User.class); DataFileWriter<User> dataFileWriter = new DataFileWriter<>(userDatumWriter); dataFileWriter.create(user1.getSchema(), new File("/tmp/users.avro")); dataFileWriter.append(user1); dataFileWriter.append(user2); dataFileWriter.append(user3); dataFileWriter.close();
反序列化
DatumReader<User> userDatumReader = new SpecificDatumReader<>(User.class); DataFileReader<User> dataFileReader = new DataFileReader<>(new File("/tmp/users.avro"), userDatumReader); User user = null; while (dataFileReader.hasNext()) { user = dataFileReader.next(user); log.info("{}", user); } dataFileReader.close(); // {"name": "A", "favorite_number": 1, "favorite_color": null} // {"name": "B", "favorite_number": 2, "favorite_color": "c2"} // {"name": "C", "favorite_number": 3, "favorite_color": "c3"}
使用Avro – 不生成代码
序列化
String avscFilePath = getClass().getClassLoader().getResource("user.avsc").getPath(); Schema schema = new Schema.Parser().parse(new File(avscFilePath)); GenericData.Record user1 = new GenericData.Record(schema); user1.put("name", "A"); user1.put("favorite_number", 1); GenericData.Record user2 = new GenericData.Record(schema); user2.put("name", "B"); user2.put("favorite_number", 2); user2.put("favorite_color", "c2"); DatumWriter<GenericRecord> userDatumWriter = new SpecificDatumWriter<>(schema); DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(userDatumWriter); dataFileWriter.create(schema, new File("/tmp/users2.avro")); dataFileWriter.append(user1); dataFileWriter.append(user2); dataFileWriter.close();
反序列化
DatumReader<GenericRecord> userDatumReader = new SpecificDatumReader<>(schema); DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(new File("/tmp/users2.avro"), userDatumReader); GenericRecord user = null; while (dataFileReader.hasNext()) { user = dataFileReader.next(user); log.info("{}", user); } dataFileReader.close(); // {"name": "A", "favorite_number": 1, "favorite_color": null} // {"name": "B", "favorite_number": 2, "favorite_color": "c2"}
转载请注明出处:http://zhongmingmao.me/2018/10/16/kafka-avro-introduction/
访问原文「 Kafka学习笔记 -- Avro入门 」获取最佳阅读体验并参与讨论
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web性能权威指南
Ilya Grigorik / 李松峰 / 人民邮电出版社 / 2013-9 / 69
本书是谷歌公司高性能团队核心成员的权威之作,堪称实战经验与规范解读完美结合的产物。本书目标是涵盖Web 开发者技术体系中应该掌握的所有网络及性能优化知识。全书以性能优化为主线,从TCP、UDP 和TLS 协议讲起,解释了如何针对这几种协议和基础设施来优化应用。然后深入探讨了无线和移动网络的工作机制。最后,揭示了HTTP 协议的底层细节,同时详细介绍了HTTP 2.0、 XHR、SSE、WebSoc......一起来看看 《Web性能权威指南》 这本书的介绍吧!