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

6.7k Views Asked by At

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

2

There are 2 best solutions below

1
On
 Map<String, Object> values = new HashMap<>();
 for (Field field : genericRecord.getSchema().getFields()) {
    values.put(field.name(), genericRecord.get(field.name()));
 }
0
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())));