* Converts a nodemap into an Avro-serialized byte array
*/
static protected byte[] serializeNodeMap(ListMultimap<String, String> nodeMap)
throws IOException {
DatumWriter<AvroFlumeNodeMap> datumWriter = new SpecificDatumWriter<AvroFlumeNodeMap>();
AvroFlumeNodeMap avromap = new AvroFlumeNodeMap();
Map<CharSequence, List<CharSequence>> map = new HashMap<CharSequence, List<CharSequence>>();
for (Entry<String, Collection<String>> e : nodeMap.asMap().entrySet()) {
String name = e.getKey();
GenericArray<CharSequence> out = new GenericData.Array<CharSequence>(e
.getValue().size(), Schema.createArray(Schema.create(Type.STRING)));
for (String s : e.getValue()) {
out.add(new String(s));
}
map.put(name, out);
}
avromap.nodemap = map;
datumWriter.setSchema(avromap.getSchema());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataFileWriter<AvroFlumeNodeMap> fileWriter = new DataFileWriter<AvroFlumeNodeMap>(
datumWriter);
fileWriter.create(avromap.getSchema(), baos);
fileWriter.append(avromap);
fileWriter.close();
return baos.toByteArray();
}