Search in sources :

Example 21 with KeyValPair

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();
}
Also used : MutableDouble(org.apache.commons.lang.mutable.MutableDouble) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) Map(java.util.Map) HashMap(java.util.HashMap)

Example 22 with KeyValPair

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();
}
Also used : KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Map(java.util.Map)

Example 23 with KeyValPair

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();
}
Also used : HighLow(org.apache.apex.malhar.lib.util.HighLow) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Map(java.util.Map)

Example 24 with KeyValPair

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);
}
Also used : Field(java.lang.reflect.Field) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) Method(java.lang.reflect.Method)

Example 25 with KeyValPair

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());
    }
}
Also used : ConversionException(org.apache.commons.beanutils.ConversionException) DateConverter(org.apache.commons.beanutils.converters.DateConverter) Matcher(java.util.regex.Matcher) InvocationTargetException(java.lang.reflect.InvocationTargetException) DateTimeConverter(org.apache.commons.beanutils.converters.DateTimeConverter) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair)

Aggregations

KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)42 Test (org.junit.Test)16 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)15 Map (java.util.Map)10 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)4 List (java.util.List)4 WindowOption (org.apache.apex.malhar.lib.window.WindowOption)4 LocalMode (com.datatorrent.api.LocalMode)3 Calendar (java.util.Calendar)3 Date (java.util.Date)3 MachineInfo (org.apache.apex.examples.machinedata.data.MachineInfo)3 MachineKey (org.apache.apex.examples.machinedata.data.MachineKey)3 ResourceType (org.apache.apex.examples.machinedata.data.ResourceType)3 Function (org.apache.apex.malhar.lib.function.Function)3 TimeBucketKey (org.apache.apex.malhar.lib.util.TimeBucketKey)3 TriggerOption (org.apache.apex.malhar.lib.window.TriggerOption)3 MutableDouble (org.apache.commons.lang.mutable.MutableDouble)3 DAG (com.datatorrent.api.DAG)2 Multimap (com.google.common.collect.Multimap)2