Grokbase Groups Avro user July 2010
FAQ
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>

Search Discussions

  • David Rosenstrauch at Jul 7, 2010 at 7:43 pm

    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
  • Philip Zeyliger at Jul 7, 2010 at 7:52 pm
    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 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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupuser @
categoriesavro
postedJul 7, '10 at 7:22p
activeJul 7, '10 at 7:52p
posts3
users2
websiteavro.apache.org
irc#avro

People

Translate

site design / logo © 2021 Grokbase