Return-Path: X-Original-To: apmail-avro-dev-archive@www.apache.org Delivered-To: apmail-avro-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5B551D4A8 for ; Tue, 28 May 2013 13:54:28 +0000 (UTC) Received: (qmail 16789 invoked by uid 500); 28 May 2013 13:54:28 -0000 Delivered-To: apmail-avro-dev-archive@avro.apache.org Received: (qmail 16358 invoked by uid 500); 28 May 2013 13:54:23 -0000 Mailing-List: contact dev-help@avro.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@avro.apache.org Delivered-To: mailing list dev@avro.apache.org Received: (qmail 15598 invoked by uid 99); 28 May 2013 13:54:22 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 May 2013 13:54:22 +0000 Date: Tue, 28 May 2013 13:54:22 +0000 (UTC) From: "Vincenz Priesnitz (JIRA)" To: dev@avro.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Created] (AVRO-1341) Allow controlling avro via java annotations when using reflection. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 Vincenz Priesnitz created AVRO-1341: --------------------------------------- Summary: Allow controlling avro via java annotations when using reflection. Key: AVRO-1341 URL: https://issues.apache.org/jira/browse/AVRO-1341 Project: Avro Issue Type: New Feature Components: java Reporter: Vincenz Priesnitz Assignee: Vincenz Priesnitz It would be great if one could control avro with java annotations. As of now, it is already possible to mark fields as Nullable or classes being encoded as a String. I propose a bigger set of annotations to control the behavior of avro on fields and classes. Such annotations have proven useful with jacksons json serialization and morphias mongoDB serialization. I propose the following additional annotations: @AvroName("alternativeName") @AvroIgnore @AvroMeta(key="K", value="V") @AvroEncode(using=CustomEncoding.class) Java fields with the @AvroName("alternativeName") annotation will be renamed in the induced schema. When reading an avro file via reflection, the reflection reader will look for fields in the schema with "alternativeName". For example: {code} @AvroName("foo") int bar; {code} is serialized as {code} { "name" : "foo", "type" : "int" } {code} Fields with the @AvroIgnore annotation will be treated as if they had a transient modifier, i.e. they will not be written to or read from avro files. The @AvroMeta(key="K", value="V") annotation allows you to store an arbitrary key : value pair at every node in the schema. {code} @AvroMeta(key="fieldKey", value="fieldValue") int foo; {code} will create the following schema {code} {"name" : "foo", "type" : "int", "fieldKey" : "fieldValue" } {code} Fields can be custom encoded with the AvroEncode(using=CustomEncoding.class) annotation. This annotation is a generalization of the @Stringable annotation. The @Stringable annotation is limited to classes with string argument constructors. Some classes can be similarly reduced to a smaller class or even a single primitive, but dont fit the requirements for @Stringable. A prominent example is java.util.Date, which instances can essentially be described with a single long. Such classes can now be encoded with a CustomEncoding, which reads and writes directly from the encoder/decoder. One simply extends the abstract CustomEncodings class by implementing a schema, a read method and a write method. A java field can then be annotated like this: {code} @AvroEncode(using=DateAslongEncoding.class) Date date; {code} The custom encoding implementation would look like {code} public class DateAsLongEncoding extends CustomEncoding { { schema = Schema.create(Schema.Type.LONG); schema.addProp("CustomEncoding", "DateAsLongEncoding"); } @Override public void write(Object datum, Encoder out) throws IOException { out.writeLong(((Date)datum).getTime()); } @Override public Date read(Object reuse, Decoder in) throws IOException { if (reuse != null) { ((Date)reuse).setTime(in.readLong()); return (Date)reuse; } else return new Date(in.readLong()); } } {code} I implemented said annotations and a custom encoding for java.util.Date as a proof of concept and also extended the @Stringable annotations to fields. This issue is a followup of AVRO-1328 and AVRO-1330. -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators For more information on JIRA, see: http://www.atlassian.com/software/jira