mgerbracht
mgerbracht

Reputation: 81

How to handle avro schema without namespace

I would like to use auto-generated java classes from an avro schema to consume kafka messages. Unfortunately the avro schema has no namespace defined (and the owner of the schema is a little hesistant to add one).

If I generate the classes using the avro maven plugin they end up in the root package. So it seems that I can not import and use them in my java classes.

So the question is how to handle avro schemas without namespace?

I had the following ideas:

  1. Configure a default namespace when generating the classes. But I couldn't find a configuration option for that. I guess it would be in the avro maven plugin in the pom file but there doesn't seem to be an option for it.
  2. Add a "random" namespace to the avro schema by myself. That way java wise everything is fine - classes are generated and I can use them but the Kafka consumer shows an error (see below) which I guess(!) has to do with my invalid namespace
  3. Generate classes in the root package and use reflection to get them on the class path - but is this a good idea?
  4. I found a similar feature request (https://github.com/davidmc24/gradle-avro-plugin/issues/81) - but I don't understand the workaround mentioned there.

Error-Message I get when trying case 2 (namespace added by myself):

org.apache.kafka.common.errors.SerializationException: Could not find class MyClass specified in writer's schema whilst finding reader's schema for a SpecificRecord.

Upvotes: 0

Views: 2523

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191681

The workaround in the issue is to read the raw AVSC file as JSON and add a namespace to the record if one doesn't exist, which is what you already proposed.

Without doing something like that, as you've discovered, then you'll be unable to import any generated class in your Java project.

Regarding Kafka, it's not clear what deserializer you're using. If using Confluent's, you may need to define your own that doesn't lookup the writer schema in the schema registry.

Reminder - you can always use GenericRecord types instead.

Upvotes: 1

Related Questions