use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class MarginKeyVal method endWindow.
/**
* Generates tuples for each key and emits them. Only keys that are in the
* denominator are iterated on If the key is only in the numerator, it gets
* ignored (cannot do divide by 0) Clears internal data
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void endWindow() {
Double val;
for (Map.Entry<K, MutableDouble> e : denominators.entrySet()) {
K key = e.getKey();
MutableDouble nval = numerators.get(key);
if (nval == null) {
nval = new MutableDouble(0.0);
} else {
// so that all left over keys can be reported
numerators.remove(key);
}
if (percent) {
val = (1 - nval.doubleValue() / e.getValue().doubleValue()) * 100;
} else {
val = 1 - nval.doubleValue() / e.getValue().doubleValue();
}
margin.emit(new KeyValPair(key, getValue(val.doubleValue())));
}
numerators.clear();
denominators.clear();
}
use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class MultiWindowSumKeyVal method endWindow.
/**
* Emit only at the end of windowSize window boundary.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void endWindow() {
boolean emit = (++windowCount) % windowSize == 0;
if (!emit) {
return;
}
// Emit only at the end of application window boundary.
boolean dosum = sum.isConnected();
if (dosum) {
for (Map.Entry<K, SumEntry> e : sums.entrySet()) {
K key = e.getKey();
if (dosum) {
sum.emit(new KeyValPair(key, getValue(e.getValue().sum.doubleValue())));
}
}
}
// Clear cumulative sum at the end of application window boundary.
sums.clear();
}
use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class MultiWindowRangeKeyVal method endWindow.
/**
* Emits range for each key at application window boundary. If no data is received, no emit is done
* Clears the internal data before return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void endWindow() {
boolean emit = (++windowCount) % windowSize == 0;
if (!emit) {
return;
}
for (Map.Entry<K, V> e : high.entrySet()) {
HighLow<V> hl = new HighLow<V>();
hl.setHigh(getValue(e.getValue().doubleValue()));
// cannot be null
hl.setLow(getValue(low.get(e.getKey()).doubleValue()));
range.emit(new KeyValPair(e.getKey(), hl));
}
clearCache();
}
use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class JavaExpressionParser method getGetterForVariable.
private KeyValPair<String, ? extends Class<?>> getGetterForVariable(String var, Class<?> inputClassType) {
try {
final Field field = inputClassType.getField(var);
if (Modifier.isPublic(field.getModifiers())) {
return new KeyValPair<>(var, field.getType());
}
logger.debug("Field {} is not publicly accessible. Proceeding to locate a getter method.", var);
} catch (NoSuchFieldException ex) {
logger.debug("{} does not have field {}. Proceeding to locate a getter method.", inputClassType.getName(), var);
}
String[] methodAccessors = new String[] { GET, IS };
for (String m : methodAccessors) {
String methodName = m + var.substring(0, 1).toUpperCase() + var.substring(1);
try {
Method method = inputClassType.getMethod(methodName);
if (Modifier.isPublic(method.getModifiers())) {
return new KeyValPair<>(methodName + "()", method.getReturnType());
}
logger.debug("Method {} of {} is not accessible. Proceeding to locate another getter method.", methodName, inputClassType.getName());
} catch (NoSuchMethodException ex) {
logger.debug("{} does not have method {}. Proceeding to locate another getter method", inputClassType.getName(), methodName);
}
}
return new KeyValPair<>(var, inputClassType);
}
use of org.apache.apex.malhar.lib.util.KeyValPair in project apex-malhar by apache.
the class RegexParser method processTuple.
@Override
public void processTuple(byte[] tuple) {
if (tuple == null) {
if (err.isConnected()) {
err.emit(new KeyValPair<String, String>(null, "Blank/null tuple"));
}
errorTupleCount++;
return;
}
String incomingString = new String(tuple);
if (StringUtils.isBlank(incomingString)) {
if (err.isConnected()) {
err.emit(new KeyValPair<String, String>(incomingString, "Blank tuple"));
}
errorTupleCount++;
return;
}
try {
if (out.isConnected() && clazz != null) {
Matcher matcher = pattern.matcher(incomingString);
boolean patternMatched = false;
Constructor<?> ctor = clazz.getConstructor();
Object object = ctor.newInstance();
if (matcher.find()) {
for (int i = 0; i <= matcher.groupCount() - 1; i++) {
if (delimitedParserSchema.getFields().get(i).getType() == DelimitedSchema.FieldType.DATE) {
DateTimeConverter dtConverter = new DateConverter();
dtConverter.setPattern((String) delimitedParserSchema.getFields().get(i).getConstraints().get(DelimitedSchema.DATE_FORMAT));
ConvertUtils.register(dtConverter, Date.class);
}
BeanUtils.setProperty(object, delimitedParserSchema.getFields().get(i).getName(), matcher.group(i + 1));
}
patternMatched = true;
}
if (!patternMatched) {
throw new ConversionException("The incoming tuple do not match with the Regex pattern defined.");
}
out.emit(object);
emittedObjectCount++;
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException | ConversionException e) {
if (err.isConnected()) {
err.emit(new KeyValPair<String, String>(incomingString, e.getMessage()));
logger.debug("Regex Expression : {} Incoming tuple : {}", splitRegexPattern, incomingString);
}
errorTupleCount++;
logger.error("Tuple could not be parsed. Reason {}", e.getMessage());
}
}
Aggregations