-
Notifications
You must be signed in to change notification settings - Fork 2
Extractor Examples
Cellmate offers two concrete CellExtractor classes to read different typed cell value contents from arbitrary cell implementations. SingleMultiValueCellExtractor contains a number of methods to filter cells by label, regular expression, and perform direct label lookup for a typed value.
Most of the methods in these helper extractor classes require the Label and Value annotations be on your cell classes. Many people implementing their own cells to model domain objects may simply have getters/setters that that can directly dereference their data. These users may find little to no value-add in the Label/Value annotations, and hence very little use for the Extraction API. There are trade-offs to both approaches. This guide assumes you're using the Cell classes to hold simple label/value pairs, with a few potential auxiliary fields.
The following code looks up the label and value for a particular cell.
String label = extractor.getLabel(cell);
String value = extractor.getStringValue(cell);
The following code performs a direct lookup on 'name' expecting to find a single String. If more than one is found, or the value is null, this method throws a CellExtractorException.
SingleMultiValueCellExtractor extractorSingle = new SingleMultiValueCellExtractor();
String value = extractor.getStringValueByLabel(cellGroup.getInternalList(), "name");
The following code performs a regular expression filter over a cell group containing StringValueCell that returns only cells with labels starting with the character 'l'.
Collection<StringValueCell> cells = extractor.regexMatchLabel(cellGroup.getInternalList(), "^[l]+[a-zA-Z]+");
The following code returns a list of String values pertaining to cells that matched the supplied 'name' label.
List<String> items = extractor.getAllStringCellValuesWithLabel(cellGroup.getInternalList(), "event");
You can use this class to inline your own filtering rules for cell collections using Google Guava Predicates The following example shows an inline filter with an anonymous class. The cell type in this particular example happens to use the StringValueCell but the inline definition can support any cell type.
extractor.filterCellsByPredicate(cellGroup.getInternalList(), new Predicate<StringValueCell>() {
public boolean apply(StringValueCell stringValueCell) {
//evaluate some condition.
}
})
There's also CommonAuxiliaryFieldsCellExtractor for when your cell class contains custom CellAuxiliaryField annotations that store more information than just a label/value pair. The most common example would be timestamp. All the typed Accumulo SecurityValueCells contain auxiliary fields for both column visibility and timestamp. (See: SecurityStringValueCell for example). The auxiliary extractor offers several helper methods for filtering cells that don't contain timestamp labels, as well as finding the cell in a group with the most recent timestamp.
Both these classes wrap the functionality found in CellReflector. This class offers a number of reflective methods that help should you decide to implement your own CellExtractor.