6

I have two types of AvroRecords that both extend avro.SpecificRecord. Is there a way to make one a subclass of the other in Java? One of them is PersonRecord and the one I would like to be its subclass is EmployeeRecord. The reason I don't want to populate normal Java classes with the avro data is that I am using hadoop and would like to work with the avro files directly if it is possible.

To clarify, it is the polymorphism that I am interested in. I would like to be able to use a function that takes as argument a PersonRecord with an EmployeeRecord.

Thanks!

grasshopper
  • 3,988
  • 3
  • 23
  • 29

1 Answers1

6

I think I understand what you're trying to do ("subclass" an Avro record in the Avro Schema definition file) but I don't think it's possible.

Instead, a way to do this would be to have EmployeeRecord have a PersonRecord member nested within it, and then the Employee-specific related info following. For example :

{ 
  "type": "record",
  "name": "PersonRecord",
  "namespace": "com.yourapp",
  "fields": [
    {
      "name": "first",
      "type": "string"
    },
    { etc... }
    ]
}


{ 
  "type": "record",
  "name": "EmployeeRecord",
  "namespace": "com.yourapp",
  "fields": [
    {
      "name": "PersonInfo",
      "type": "PersonRecord"
    },
    { 
      "name": "salary",
      "type": "int"
     },
    { etc... }
    ]
}
jtravaglini
  • 1,676
  • 11
  • 19
  • 1
    Thanks. It indeed seems impossible to subclass avro records from the schema itself. We still wouldn't be able to use an EmployeeRecord as a PersonRecord though with what you propose. It's the polymorphism that I want. Ie, a function(personRecord) that can be applied to an employeeRecord argument. I ended up manually adding an interface to the generated java class (from the avro) and it works-- now I write functions of the type function(interface) and added some Unit Tests to remind other devs to add this interface since avro tools won't. Still looking for a better solution :) – grasshopper Jan 10 '14 at 16:38
  • 1
    you may be able to do this with the Avro Java API - in particular the Generic/SpecificData's induce() method, to create in effect a subclassed Avro schema - but in doing so will lose the convenience of maintaining your schemas independently of code. – jtravaglini Jan 10 '14 at 16:49
  • also you might want to check out http://stackoverflow.com/questions/20864470/polymorphism-and-inheritance-in-avro-schemas – jtravaglini Jan 10 '14 at 17:21