{"username":"miguno","tweet":"Rock: Nerf paper, scissors is fine.","timestamp":1366150681} {"username":"BlizzardCS","tweet":"Works as intended. Terran is IMBA.","timestamp":1366154481}
新建twitter.avsc文件,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
{ "type":"record", "name":"twitter_schema", "namespace":"com.miguno.avro", "fields":[ {"name":"username", "type":"string", "doc":"Name of the user account on Twitter.com"}, { "name":"tweet", "type":"string", "doc":"The content of the user‘s Twitter message"}, { "name":"timestamp", "type":"long", "doc":"Unix epoch time in seconds"} ], "doc:":"A basic schema for storing Twitter messages" }
// Construct via builder Useruser3= User.newBuilder() .setName("Charlie") .setFavoriteColor("blue") .setFavoriteNumber(null) .build(); // Serialize user1, user2 and user3 to disk DatumWriter<User> userDatumWriter = newSpecificDatumWriter<User>(User.class); DataFileWriter<User> dataFileWriter = newDataFileWriter<User>(userDatumWriter); try { dataFileWriter.create(user1.getSchema(), newFile("users.avro")); dataFileWriter.append(user1); dataFileWriter.append(user2); dataFileWriter.append(user3); dataFileWriter.close(); }catch (Exception e){ System.out.println(); }
//反序列化 // Deserialize Users from disk DatumReader<User> userDatumReader = newSpecificDatumReader<User>(User.class);
// 使用这种方式可以避免因为对象太多而造成的对 gc 的不良影响。 Useruser=null; try{ DataFileReader<User> dataFileReader = newDataFileReader<User>( newFile("users.avro"), userDatumReader); 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); } }catch (Exception e){ System.out.println(e); } } }