avro-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scott Carey (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (AVRO-1274) Add a schema builder API
Date Thu, 02 May 2013 02:58:16 GMT

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

Scott Carey commented on AVRO-1274:
-----------------------------------

I think the answer to my question would be:

{code}
  public static final org.apache.avro.Schema SCHEMA$;
  static {
    SCHEMA$ = SchemaBuilder
      .recordType("HandshakeRequest")
      .namespace("org.apache.avro.ipc")
      .requiredFixed("clientHash", MD5.SCHEMA$)
      .unionType("clientProtocol", SchemaBuilder.unionType(
          SchemaBuilder.NULL,
          SchemaBuilder.STRING)
          .build())
          .addFieldProp("avro.java.string", "String")
      .requiredFixed("serverHash", MD5.SCHEMA$)
      .unionType("meta", SchemaBuilder.unionType(
          SchemaBuilder.NULL,
          SchemaBuilder.mapType(SchemaBuilder.BYTES)
            .addFieldProp("avro.java.string", "String")
            .build())
          .build())
      .build();
  }
{code}

but I am not sure.  Also "addFieldProp()" does not exist.

What is odd is that there are two unionType() methods, one takes varargs and the other does
not.  I suspect that the intention was for both to use varargs so that the nested union building
is not required by the user.

It would be much simpler if unions without defaults had a shortcut:

{code}
  public static final org.apache.avro.Schema SCHEMA$;
  static {
    SCHEMA$ = SchemaBuilder
      .recordType("HandshakeRequest")
      .namespace("org.apache.avro.ipc")
      .requiredFixed("clientHash", MD5.SCHEMA$)
      .nullableString("clientProtocol")
         .addFieldProp("avro.java.string", "String")
      .requiredFixed("serverHash", MD5.SCHEMA$)
      .nullableMap(SchemaBuilder.BYTES)
        .addFieldProp("avro.java.string", "String")
      .build()
  }
{code}

Building unions in general feels clunky as well since you have to break chaining and use SchemaBuilder
again.  Instead of taking a varargs list of schemas in the union, the type returned could
be a UnionBuilder.  So instead of:
{code}
  public static final org.apache.avro.Schema SCHEMA$;
  static {
    SCHEMA$ = SchemaBuilder
      .recordType("Test")
      .namespace("org.apache.avro")
      .unionString("stringField", "defaultVal", 
         SchemaBuilder.INT,
         SchemaBuilder.arrayType(SchemaBuilder.INT).build()
         SchemaBuilder.mapType(SchemaBuilder.unionType(
           SchemaBuilder.INT, SchemaBuilderLONG)
           )
        )
      .build()
  }
{code}

we could write something more like:
{code}
  public static final org.apache.avro.Schema SCHEMA$;
  static {
    SCHEMA$ = SchemaBuilder
      .recordType("Test")
      .namespace("org.apache.avro")
      .unionString("stringFieldName", "defaultVal")
         .andInt()
         .andArrayOf().int()
         .andMapOf().unionInt().andLong()
      .build()
  }
{code}
                
> Add a schema builder API
> ------------------------
>
>                 Key: AVRO-1274
>                 URL: https://issues.apache.org/jira/browse/AVRO-1274
>             Project: Avro
>          Issue Type: New Feature
>          Components: java
>            Reporter: Tom White
>            Assignee: Tom White
>             Fix For: 1.7.5
>
>         Attachments: AVRO-1274.patch, AVRO-1274.patch, AVRO-1274.patch, AVRO-1274.patch,
AVRO-1274.patch, AVRO-1274.patch, AVRO-1274.patch, TestDefaults.patch
>
>
> It would be nice to have a fluent API that made it easier to construct record schemas.

--
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

Mime
View raw message