How to convert "org.apache.avro.generic.GenericRecord" to "java.util.Map"?

6.6k views Asked by At

Easy way to convert "org.apache.avro.generic.GenericRecord" to "java.util.Map"

2

There are 2 answers

1
Tsolak Barseghyan On
 Map<String, Object> values = new HashMap<>();
 for (Field field : genericRecord.getSchema().getFields()) {
    values.put(field.name(), genericRecord.get(field.name()));
 }
0
Ville Venäläinen On

With java 8 lambdas you can do this like

Map<String, Object> map = new HashMap<>();
genericRecord.getSchema().getFields().forEach(field -> 
    map.put(field.name(), genericRecord.get(field.name())));