You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Sachin Goyal (JIRA)" <ji...@apache.org> on 2014/07/28 21:00:42 UTC

[jira] [Commented] (AVRO-1554) Avro should have support for common constructs like UUID and Date

    [ https://issues.apache.org/jira/browse/AVRO-1554?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14076591#comment-14076591 ] 

Sachin Goyal commented on AVRO-1554:
------------------------------------

This could be done in a couple of ways:

1) Add Date and UUID in Schema.Type (so that there will be DateSchema and UUIDSchema as well) and in Symbol.java as:
{code}
final Symbol DATE = new Symbol.Terminal("date");
final Symbol UUID = new Symbol.Terminal("uuid");
{code}

2) Add support for class-level CustomEncoders in ReflectDatumWriter.java and ReflectDatumReader.java as:
{code}
ReflectDatumReader.get().addCustomEncoder (UUID.class, org.apache.avro.encoders.default.UUIDAsStringEncoder.class);
ReflectDatumReader.get().addCustomEncoder (Date.class, org.apache.avro.encoders.default.DateAsLongEncoder.class);
{code}

3) Just mark these two as stringable and override ReflectDatumReader as:
{code}
protected Object newInstanceFromString(Class c, String s) {
    if (c == java.util.UUID.class) {
      return UUID.fromString(s);
    }
    if (c == java.util.Date.class) {
      return DateFormat.parse(s);
    }
    return super.newInstanceFromString(c, s);
  }
{code}

#2 seems to be the most extensible.
Please vote how we can proceed with this (or if the same can be achieved by some existing method).


> Avro should have support for common constructs like UUID and Date
> -----------------------------------------------------------------
>
>                 Key: AVRO-1554
>                 URL: https://issues.apache.org/jira/browse/AVRO-1554
>             Project: Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.7.6
>            Reporter: Sachin Goyal
>
> Consider the following code:
> {code}
> public class AvroExample
> {
>     public static void main (String [] args) throws Exception
>     {
>         ReflectData rdata = ReflectData.AllowNull.get();
>         Schema schema = rdata.getSchema(Temp.class);
>         
>         ReflectDatumWriter<Temp> datumWriter = 
>                new ReflectDatumWriter (Temp.class, rdata);
>         DataFileWriter<Temp> fileWriter = 
>                new DataFileWriter<Temp> (datumWriter);
>         ByteArrayOutputStream baos = new ByteArrayOutputStream();
>         fileWriter.create(schema, baos);
>         fileWriter.append(new Temp());
>         fileWriter.close();
>         byte[] bytes = baos.toByteArray();
>         GenericDatumReader<GenericRecord> datumReader = 
>                 new GenericDatumReader<GenericRecord> ();
>         SeekableByteArrayInput avroInputStream = 
>                 new SeekableByteArrayInput(bytes);
>         DataFileReader<GenericRecord> fileReader = 
>                 new DataFileReader<GenericRecord>(avroInputStream, datumReader);
>         schema = fileReader.getSchema();
>         GenericRecord record = null;
>         record = fileReader.next(record);
>         System.out.println (record);
>         System.out.println (record.get("id"));
>     }
> }
> class Temp
> {
>     UUID id = UUID.randomUUID();
>     Date date = new Date();
>     BigInteger bi = BigInteger.TEN;
> }
> {code}
> Output from this code is:
> {code:javascript}
> {"id": {}, "date": {}, "bi": "10"}
> {code}
> UUID and Date type fields are very common in Java and can be found a lot in third-party code as well (where it may be difficult to put annotations).
> So Avro should include a default serialization/deserialization support for such fields.



--
This message was sent by Atlassian JIRA
(v6.2#6252)