How to extract schema from an avro file in java

mba12 :

How do you extract first the schema and then the data from an avro file in java? Identical to this question except in java.

I've seen examples of how to get the schema from an avsc file but not an avro file. Any direction much appreciated.

Schema schema = new Schema.Parser().parse(new File("/home/Hadoop/Avro/schema/emp.avsc"));
Helder Pereira :

If you want know the schema of a Avro file without having to generate the corresponding classes or care about which class the file belongs to, you can use the GenericDatumReader:

DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(new File("file.avro"), datumReader);
Schema schema = dataFileReader.getSchema();
System.out.println(schema);

And then you can read the data inside the file:

GenericRecord record = null;
while (dataFileReader.hasNext()) {
    record = dataFileReader.next(record);
    System.out.println(record);
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=429200&siteId=1