You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by David Rosenstrauch <da...@darose.net> on 2010/07/07 21:22:36 UTC
Puzzled about Avro serialization
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>
Re: Puzzled about Avro serialization
Posted by Philip Zeyliger <ph...@cloudera.com>.
Take a look also at
lang/java/src/java/org/apache/avro/tool/JsonToBinaryFragmentTool.java
if you just want to play with fragments of avro-encoded data.
-- Philip
On Wed, Jul 7, 2010 at 12:44 PM, David Rosenstrauch <da...@darose.net> wrote:
> On 07/07/2010 03:22 PM, David Rosenstrauch wrote:
>>
>> 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
>
> Doh! Posted too soon.
>
> Googling for "avro serialization example" turned up a blog post
> (http://blog.voidsearch.com/bigdata/apache-avro-in-practice/) with the magic
> incantation: encoder.flush();
>
> Sorry for the noise.
>
> DR
>
Re: Puzzled about Avro serialization
Posted by David Rosenstrauch <da...@darose.net>.
On 07/07/2010 03:22 PM, David Rosenstrauch wrote:
> 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
Doh! Posted too soon.
Googling for "avro serialization example" turned up a blog post
(http://blog.voidsearch.com/bigdata/apache-avro-in-practice/) with the
magic incantation: encoder.flush();
Sorry for the noise.
DR