The java-class attribute is supported by the reflect implementation,
not by the code-generating specific implementation. So you could
define Foo in Java with something like:
public class Foo {
private long batchId;
@Stringable private Timestamp timestamp;
public Foo() {}
public Foo(long batchId, Timestamp timestamp) { ... }
}
then use ReflectData to read/write instances. Note that
java.sql.Timestamp doesn't have a string constructor. Are you using a
different timestamp class? If you're defining your own then you could
instead add the @Stringable annotation to your Timestamp class rather
than to each field where it is used.
Reflect-defined schemas can refer to specific-defined classes, but not
vice-versa, since the compiler doesn't use reflection to discover
schemas, but rather always generates from the schema alone.
Doug
On Wed, Jul 2, 2014 at 8:05 AM, Ian Hummel <[email protected]> wrote:
> Hi gang,
>
> I'm trying to build a JSON schema with a custom type as the field instead of
> just a String. Is "java-class" supposed to work in that use case? I can't
> seem to make any progress.
>
> Example schema (Foo.avsc):
>
> {
> "namespace" : "com.example",
> "type" : "record",
> "name" : "Foo",
> "fields" : [
> { "name" : "batchId", "type" : "long" },
> { "name" : "timestamp", "type" : "string", "java-class" :
> "com.example.Timestamp" }
> ]
> }
>
> The Timestamp class has a public constructor which takes a single String
> argument. I even tried annotating it with @Stringable. However, the
> generated java class always uses String, not my custom type.
>
> $ java -jar ~/Downloads/avro-tools-1.7.6.jar compile -string schema
> src/main/avro/Foo.avsc /tmp/foo
>
> From the generated .java file
>
> ...
>
> /**
>
> * All-args constructor.
>
> */
>
> public Foo(java.lang.Long batchId, java.lang.String timestamp) {
>
> this.batchId = batchId;
>
> this.timestamp = timestamp;
>
> }
>
> ...
>
>
> Any help appreciated,
>
> - Ian.