Home | About | Sematext search-lucene.com search-hadoop.com
NEW: Monitor These Apps!
elasticsearch, apache solr, apache hbase, hadoop, redis, casssandra, amazon cloudwatch, mysql, memcached, apache kafka, apache zookeeper, apache storm, ubuntu, centOS, red hat, debian, puppet labs, java, senseiDB
 Search Hadoop and all its subprojects:

Switch to Plain 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"

Alan
NEW: Monitor These Apps!
elasticsearch, apache solr, apache hbase, hadoop, redis, casssandra, amazon cloudwatch, mysql, memcached, apache kafka, apache zookeeper, apache storm, ubuntu, centOS, red hat, debian, puppet labs, java, senseiDB