Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Threaded View
Avro >> mail # user >> how to get get field type

Copy link to this message
how to get get field type
I'm writng Avro files for time-related data (data-yyyy-mm-dd.avro) via Java/HDFS
and have approx 50 fields of various types. This all works and I'm good there.

But now, different people (various teams in my organization) will be writing
their own classes to parse the records.

I understand that an Avro file can only have one schema, correct?

The avro files are self documenting because the schema is written
in the file's header, but how can the reader "autodetect" the field types?

I tried this which gives me the field names but how do I get each field's type:
    // MyRecord.java was auto-generated from MyRecord.avsc
    MyRecord myRecord = new MyRecord();
    Schema schema = myRecord.getSchema();
    List<Field> fieldList = schema.getFields();
            for (Field field : fieldList) {
                String name = field.name();
                String type = field.getProp("type");
                System.out.printf("Name: %s\tType: %s\n", name, type);

In the above:
     field.getProp("type");    returns "null"
     field.schema().getType().toString(); returns "UNION"