I have a class that looks like this...
public class LegionInputFormat
extends FileInputFormat<NullWritable, LegionRecord> {
@Override
public RecordReader<NullWritable, LegionRecord>
createRecordReader(InputSplit split, TaskAttemptContext context) {
/* Skipped code for getting recordDelimiterBytes */
return new LegionRecordReader(recordDelimiterBytes);
}
}
I'd like to use a generic type so it could return any type of RecordReader specified by the user, like so:
public class LegionInputFormat<T extends RecordReader<NullWritable, LegionRecord>>
extends FileInputFormat<NullWritable, LegionRecord> {
@Override
public RecordReader<NullWritable, LegionRecord>
createRecordReader(InputSplit split, TaskAttemptContext context) {
/* Skipped code for getting recordDelimiterBytes */
return new T(recordDelimiterBytes);
}
}
As the post title suggests, I'm being told I "cannot instantiate the Type T." From other Stack Exchange posts, I've gathered that this is not possible due to something with how generics work. What I've not been able to gather is an intuitive explanation of why that's the case. I learn best by understanding, so that would be really helpful if somebody can offer it.
I'm also interested in the best practice for accomplishing what I'm looking to do here. Should the constructor for LegionInputFormat
accept a RecordReader
class, store that, and then reference it later to create a new instance? Or is there a better solution?
(Additional background - context here is Hadoop, but I doubt it matters. I'm a fairly accomplished Data Scientist, but I'm pretty new to Java.)