Created
November 9, 2019 07:24
-
-
Save masayuki038/0d48d1b5596b21f37600d43e877e7384 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AvroSchema extends AbstractSchema { | |
private Map<String, Table> tableMap; | |
private File directory; | |
public AvroSchema(File directory) { | |
this.directory = directory; | |
} | |
@Override | |
public Map<String, Table> getTableMap() { | |
if (tableMap == null) { | |
tableMap = new HashMap<>(); | |
File[] avroFiles = directory.listFiles((dir, name) -> name.endsWith(".avro")); | |
Arrays.stream(avroFiles).forEach(file -> { | |
GenericDatumReader<GenericData.Record> datum = new GenericDatumReader<>(); | |
try (DataFileReader<GenericData.Record> reader = new DataFileReader<>(file, datum)) { | |
Schema schema = reader.getSchema(); | |
List<GenericData.Record> records = new ArrayList<>(); | |
while (reader.hasNext()) { | |
GenericData.Record record = new GenericData.Record(schema); | |
reader.next(record); | |
records.add(record); | |
} | |
tableMap.put( | |
trim(file.getName(), ".avro").toUpperCase(), | |
new AvroTable(schema, records)); | |
} catch (IOException e) { | |
throw new IllegalStateException(e); | |
} | |
}); | |
} | |
return tableMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment