内容简介:路径: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入门 」获取最佳阅读体验并参与讨论
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
尽在双11:阿里巴巴技术演进与超越
阿里巴巴集团双11技术团队 / 电子工业出版社 / 2017-4 / 79
“双 11”,诞生于杭州,成长于阿里,风行于互联网,成就于新经济,贡献于全世界。 从 2009 年淘宝商城起,双 11 已历经八年。每年的双 11 既是当年的结束,又是走向未来的起点。技术的突破创新,商业模式的更替交互,推动着双 11 迈步向前。 《尽在双11——阿里巴巴技术演进与超越》是迄今唯一由阿里巴巴集团官方出品、全面阐述双 11 八年以来在技术和商业上演进和创新历程的书籍。内容......一起来看看 《尽在双11:阿里巴巴技术演进与超越》 这本书的介绍吧!