Database Reference
In-Depth Information
▪ A traditional serialization model where a developer authors a schema, runs a compiler to
create models based on that schema, and then uses those models in their application
▪ A runtime model where Avro builds records based on a schema file provided at runtime
In our example, we'll use the runtime model because this is one of the most interesting dif-
ferentiators for Avro.
We start out by defining a schema in a file that we'll call review.avsc :
{"namespace": "example.elephant",
"type": "record",
"name": "Review",
"fields": [
{"name": "reviewer", "type": "string"},
{"name": "movieTitle", "type": "string"},
{"name": "rating", "type": "int"}
]
}
Now we can create an object based on this schema and write it out to disk:
//Bind the schema
Schema schema = new
new Parser (). parse ( new
new File ( "review.avsc" ));
//Build a record
GenericRecord review = new
new GenericData . Record ( schema );
review . put ( "reviewer" , "Kevin" );
review . put ( "movieTitle" , "Dune" );
review . put ( "rating" , 10 );
// Serialize our review to disk
File file = new
new File ( "review.avro" );
DatumWriter < GenericRecord > datumWriter =
new
new GenericDatumWriter < GenericRecord >( schema );
DataFileWriter < GenericRecord > dataFileWriter =
new
new DataFileWriter < GenericRecord >( datumWriter );
dataFileWriter . create ( schema , file );
dataFileWriter . append ( user1 );
dataFileWriter . append ( user2 );
dataFileWriter . close ();
Search WWH ::




Custom Search