Home » Java » java – Is there any way to generate an Avro schema from a hashmap of values?-Exceptionshub

java – Is there any way to generate an Avro schema from a hashmap of values?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

We are putting all our data points for one row into a hashmap. We don’t want to use a pojo because the values are a different set each time. For example, we might get “place” on some records and we might get “hometown” on others. Actually we have thousands of different column names to choose from. Our code looks like this:

Map<String, Object> aMap = new HashMap<>();
aMap.put("id", Integer.valueOf(1));
aMap.put("age", Integer.valueOf(45));
aMap.put("name", "mark");
aMap.put("place", "home");

final GenericRecord record = new GenericData.Record(avroSchema);
aMap.forEach((k, v) -> {
  record.put(k, v);
});

writer.write(record);

We would like to put all the values in a map and then generate a schema. Since using the Reflect api, it can be done for a pojo, I was wondering if it could be done from a hashmap as well?

As a side question, Is there any way to eliminate the forEach above and just write the map?

How to&Answers: