7

I'm using following tool

<dependency>
  <groupId>tech.allegro.schema.json2avro</groupId>
  <artifactId>converter</artifactId>
  <version>0.2.7</version>
</dependency>

to convert json to avro, but I'm getting exception

Caused by: org.apache.avro.SchemaParseException: "enum" is not a defined name. The type of the "enumValue" field must be a defined name or a {"type": ...} expression.
at org.apache.avro.Schema.parse(Schema.java:1265)
at org.apache.avro.Schema$Parser.parse(Schema.java:1032)
at org.apache.avro.Schema$Parser.parse(Schema.java:997)
... removed by me, our code ...

for avro schema:

{
  "namespace": "test",
  "type": "record",
  "name": "test",
  "fields": [
    {
      "name": "enumValue",
      "type": "enum",
      "symbols": [
        "val_a",
        "val_b"
      ]
    }
  ]
}

and json:

{
  "enumValue": "val_a"
}

dependency tree shows avro 1.8.2

[INFO] +- org.apache.kafka:kafka-clients:jar:2.0.0:compile
[INFO] |  +- org.lz4:lz4-java:jar:1.4.1:compile
[INFO] |  +- org.xerial.snappy:snappy-java:jar:1.1.7.1:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- org.apache.avro:avro:jar:1.8.2:compile
[INFO] |  +- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] |  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[INFO] |  |  \- (org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile - omitted for duplicate)
[INFO] |  +- com.thoughtworks.paranamer:paranamer:jar:2.7:compile
[INFO] |  +- (org.xerial.snappy:snappy-java:jar:1.1.1.3:compile - omitted for conflict with 1.1.7.1)
[INFO] |  +- org.apache.commons:commons-compress:jar:1.8.1:compile
[INFO] |  +- org.tukaani:xz:jar:1.5:compile
[INFO] |  \- (org.slf4j:slf4j-api:jar:1.7.7:compile - omitted for conflict with 1.7.25)
[INFO] +- tech.allegro.schema.json2avro:converter:jar:0.2.7:compile
[INFO] |  \- (org.apache.avro:avro:jar:1.8.2:compile - omitted for duplicate)
[INFO] +- commons-io:commons-io:jar:2.6:compile
[INFO] +- info.picocli:picocli:jar:3.7.0:compile
[INFO] \- org.slf4j:slf4j-nop:jar:1.7.25:compile
[INFO]    \- (org.slf4j:slf4j-api:jar:1.7.25:compile - omitted for duplicate)

while enum definition seems to be quite the same as the one from avro home page:

For example, playing card suits might be defined with:

{ "type": "enum",
  "name": "Suit",
  "symbols" : ["SPADES", "HEARTS", "DIAMONDS", "CLUBS"]
}

So what can be wrong here? Definitions seems to be OK (is it?), latest avro seems to be used and that one should support enums. So what is wrong here?

1 Answer 1

17

I was wrong, correct definition looks like:

{
  "namespace": "test",
  "type": "record",
  "name": "test",
  "fields": [
    {
      "name": "enumValue",
      "type": {
        "name": "EnumType",
        "type": "enum",
        "symbols": [
          "val_a",
          "val_b"
        ]
      }
    }
  ]
}
4
  • but yet the example you ref suggests otherwise
    – juanchito
    Commented Jul 21, 2020 at 18:25
  • I probably don't understand. What is wrong in your opinion? Commented Jul 22, 2020 at 7:34
  • Your original code seems to match the cards example provided by Avro docs perfectly but yet you have to implement differently to work. Seems the docs are found in want
    – juanchito
    Commented Jul 22, 2020 at 12:39
  • 2
    Yes avro documentation is sometimes hard to read/find. What's found in avro homepage as enum definition is element, which is value of "type" element. Anyways, if you compare avro in question and aswer it should show the difference; the example in avro homepage isn't incorrect, it's just not complete and it might be slightly misguiding. Commented Jul 22, 2020 at 14:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.