fr.figarocms.flume.formatter.mapping.Mapping Maven / Gradle / Ivy
package fr.figarocms.flume.formatter.mapping;
import com.cloudera.flume.core.Event;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import static com.google.common.collect.Maps.newHashMap;
import static fr.figarocms.flume.formatter.mapping.converter.Converters.convert;
public class Mapping {
private List attributes;
public void setAttributes(List attributes) {
this.attributes = attributes;
}
// Required by Yaml Parser
public List getAttributes() {
return attributes;
}
// ~ Methods --------------------------------------------------------------------------------------------------------
public Map map(Event e) {
Map map = newHashMap();
// Map Event properties
map.put("body", convert(e.getBody(), "string"));
map.put("timestamp", e.getTimestamp());
map.put("host", e.getHost());
map.put("priority", e.getPriority());
map.put("nanos", e.getNanos());
// Map Event attributes
map.putAll(mapAttributes(e, attributes));
return map;
}
protected Map mapAttributes(Event e, List attributes) {
ImmutableMap mappedAttributes = ImmutableMap.of();
Map map = newHashMap();
// Create an index base on attribute name
if (attributes != null) {
mappedAttributes = Maps.uniqueIndex(attributes, new Function() {
@Override
public String apply(AttributeMapping mapping) {
return mapping.getName();
}
});
}
// Convert attributes
Map attrs = e.getAttrs();
for (Map.Entry s : attrs.entrySet()) {
if (mappedAttributes.containsKey(s.getKey())) {
AttributeMapping mapping = mappedAttributes.get(s.getKey());
// Convert attribute
map.put(s.getKey(), convert(s.getValue(), mapping.getType()));
} else {
// By default attribute is converted to string
map.put(s.getKey(), convert(s.getValue(), "string"));
}
}
return map;
}
}