TakeSoUp
TakeSoUp

Reputation: 8087

How to read/parse *only* the JSON schema from a file containing an avro message in binary format?

I have an avro message in binary format in a file.

Obj^A^D^Vavro.schemaÞ^B{"type":"record","name":"rec","namespace":"ns","fields":[{"name":"id","type":["int","null"]},{"name":"name","type":["string","null"]},{"name":"foo_id","type":["int","null"]}]}^Tavro.codec^Lsnappy^@¤²/n¹¼Bù<9b> à«_^NÌ^W

I'm just interested in the SCHEMA. Is there a way to read/parse just the schema from this file? I'm currently parsing this file by hand to extract the schema, but I was hoping avro would help me a standard way of doing that.

Upvotes: 2

Views: 1396

Answers (2)

Heapify
Heapify

Reputation: 2901

Using avro-tools is the quickest and easiest way to get avro schema from an avro file. Just use the following command:

avro-tools getchema myfile.avro > myfile.avsc

Upvotes: 0

Cl&#233;ment MATHIEU
Cl&#233;ment MATHIEU

Reputation: 3171

Avro does provide an API to get the schema from a file:

    File file = new File("myFile.avro")

    FileReader<?> reader = DataFileReader.openReader(file, new GenericDatumReader<>());
    Schema schema = reader.getSchema();
    System.out.println(schema);

I think that it should match your definition of "just the schema", let me know if it doesn't.

You could also use the getschema command from avro-tools if you have no reason to do it programmatically.

Upvotes: 1

Related Questions