Hi. Newbie here, kicking the tires on Avro.
Although I read through the docs pretty carefully, I think I'm somehow
missing some key point about how Avro serialization works. I tried
testing it out with the following code, and schema, but I'm not getting
any serialized output. Anyone know what I'm doing wrong?
Thanks,
DR
--------
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.avro.Schema;
import org.apache.avro.io.JsonEncoder;
import org.apache.avro.specific.SpecificDatumWriter;
public class TestAvroSerialization extends TestCase {
protected void setUp() {
schemaFile = new File("test_data/avro/SimpleRecord.avsc");
}
public void testParseSchema() throws IOException {
doParseSchema();
}
public void testSerializeRecord() throws IOException {
Schema recordSchema = doParseSchema();
System.out.println(recordSchema.toString(true));
SpecificDatumWriter<SimpleRecord> writer = new
SpecificDatumWriter<SimpleRecord>(recordSchema);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JsonEncoder encoder = new JsonEncoder(recordSchema, out);
SimpleRecord record = new SimpleRecord(recordSchema);
writer.write(record, encoder);
System.out.println(out.toString() + "<eof>");
fail("todo");
}
public void testDeserializeRecord() throws IOException {
Schema recordSchema = doParseSchema();
fail("todo");
}
private Schema doParseSchema() throws IOException {
Schema recordSchema = Schema.parse(schemaFile);
return recordSchema;
}
private File schemaFile;
}
--------
import org.apache.avro.Schema;
import org.apache.avro.specific.SpecificRecordBase;
public class SimpleRecord extends SpecificRecordBase {
private Schema schema;
public SimpleRecord(Schema schema) {
this.schema = schema;
}
public Object get(int i) {
if (i != 0) {
throw new IllegalArgumentException();
}
return val;
}
public void put(int i, Object val) {
if (i != 0) {
throw new IllegalArgumentException();
}
if (!(val instanceof Integer)) {
throw new IllegalArgumentException();
}
Integer intVal = (Integer)val;
this.val = intVal;
}
public Schema getSchema() {
return schema;
}
private int val = 1;
}
--------
test_data/avro/SimpleRecord.avsc
--------
{
"type": "record",
"name": "SimpleRecord",
"fields" : [
{"name": "val", "type": "int"}
]
}
--------
Output
--------
{
"type" : "record",
"name" : "SimpleRecord",
"fields" : [ {
"name" : "val",
"type" : "int"
} ]
}
<eof>