Search in sources :

Example 6 with SupportType

use of org.apache.apex.malhar.lib.util.FieldInfo.SupportType in project apex-malhar by apache.

the class RedisPOJOOutputOperator method convertObjectToMap.

private Map<String, String> convertObjectToMap(Object tuple) {
    Map<String, String> mappedObject = new HashMap<String, String>();
    for (int i = 0; i < dataColumns.size(); i++) {
        final SupportType type = dataColumns.get(i).getType();
        final String columnName = dataColumns.get(i).getColumnName();
        if (i < getters.size()) {
            Getter<Object, Object> obj = (Getter<Object, Object>) (getters.get(i));
            Object value = obj.get(tuple);
            mappedObject.put(columnName, value.toString());
        }
    }
    return mappedObject;
}
Also used : HashMap(java.util.HashMap) Getter(org.apache.apex.malhar.lib.util.PojoUtils.Getter) SupportType(org.apache.apex.malhar.lib.util.FieldInfo.SupportType)

Example 7 with SupportType

use of org.apache.apex.malhar.lib.util.FieldInfo.SupportType in project apex-malhar by apache.

the class AvroToPojo method getPOJOFromGenericRecord.

/**
 * Returns a POJO from a Generic Record
 *
 * @return Object
 */
@SuppressWarnings("unchecked")
private Object getPOJOFromGenericRecord(GenericRecord tuple) throws InstantiationException, IllegalAccessException {
    Object newObj = getPojoClass().newInstance();
    try {
        for (int i = 0; i < columnFieldSetters.size(); i++) {
            AvroToPojo.ActiveFieldInfo afi = columnFieldSetters.get(i);
            SupportType st = afi.fieldInfo.getType();
            Object val = null;
            try {
                val = tuple.get(afi.fieldInfo.getColumnName());
            } catch (Exception e) {
                LOG.error("Could not find field -" + afi.fieldInfo.getColumnName() + "- in the generic record", e);
                val = null;
                fieldErrorCount++;
            }
            if (val == null) {
                continue;
            }
            try {
                switch(st) {
                    case BOOLEAN:
                        ((PojoUtils.SetterBoolean<Object>) afi.setterOrGetter).set(newObj, (boolean) tuple.get(afi.fieldInfo.getColumnName()));
                        break;
                    case DOUBLE:
                        ((PojoUtils.SetterDouble<Object>) afi.setterOrGetter).set(newObj, (double) tuple.get(afi.fieldInfo.getColumnName()));
                        break;
                    case FLOAT:
                        ((PojoUtils.SetterFloat<Object>) afi.setterOrGetter).set(newObj, (float) tuple.get(afi.fieldInfo.getColumnName()));
                        break;
                    case INTEGER:
                        ((PojoUtils.SetterInt<Object>) afi.setterOrGetter).set(newObj, (int) tuple.get(afi.fieldInfo.getColumnName()));
                        break;
                    case STRING:
                        ((PojoUtils.Setter<Object, String>) afi.setterOrGetter).set(newObj, new String(tuple.get(afi.fieldInfo.getColumnName()).toString()));
                        break;
                    case LONG:
                        ((PojoUtils.SetterLong<Object>) afi.setterOrGetter).set(newObj, (long) tuple.get(afi.fieldInfo.getColumnName()));
                        break;
                    default:
                        throw new AvroRuntimeException("Invalid Support Type");
                }
            } catch (AvroRuntimeException e) {
                LOG.error("Exception in setting value", e);
                fieldErrorCount++;
            }
        }
    } catch (Exception ex) {
        LOG.error("Generic Exception in setting value" + ex.getMessage());
        errorCount++;
        newObj = null;
    }
    return newObj;
}
Also used : AvroRuntimeException(org.apache.avro.AvroRuntimeException) AvroRuntimeException(org.apache.avro.AvroRuntimeException) SupportType(org.apache.apex.malhar.lib.util.FieldInfo.SupportType)

Example 8 with SupportType

use of org.apache.apex.malhar.lib.util.FieldInfo.SupportType in project apex-malhar by apache.

the class RedisPOJOInputOperator method processFirstTuple.

public void processFirstTuple(Map<String, String> value) throws ClassNotFoundException {
    objectClass = Class.forName(getOutputClass());
    final int size = dataColumns.size();
    for (int i = 0; i < size; i++) {
        final SupportType type = dataColumns.get(i).getType();
        final String getterExpression = dataColumns.get(i).getPojoFieldExpression();
        final Object setter;
        switch(type) {
            case STRING:
                setter = PojoUtils.createSetter(objectClass, getterExpression, String.class);
                break;
            case BOOLEAN:
                setter = PojoUtils.createSetterBoolean(objectClass, getterExpression);
                break;
            case SHORT:
                setter = PojoUtils.createSetterShort(objectClass, getterExpression);
                break;
            case INTEGER:
                setter = PojoUtils.createSetterInt(objectClass, getterExpression);
                break;
            case LONG:
                setter = PojoUtils.createSetterLong(objectClass, getterExpression);
                break;
            case FLOAT:
                setter = PojoUtils.createSetterFloat(objectClass, getterExpression);
                break;
            case DOUBLE:
                setter = PojoUtils.createSetterDouble(objectClass, getterExpression);
                break;
            default:
                setter = PojoUtils.createSetter(objectClass, getterExpression, Object.class);
                break;
        }
        setters.add(setter);
    }
}
Also used : SupportType(org.apache.apex.malhar.lib.util.FieldInfo.SupportType)

Aggregations

SupportType (org.apache.apex.malhar.lib.util.FieldInfo.SupportType)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Getter (org.apache.apex.malhar.lib.util.PojoUtils.Getter)1 Setter (org.apache.apex.malhar.lib.util.PojoUtils.Setter)1 SetterBoolean (org.apache.apex.malhar.lib.util.PojoUtils.SetterBoolean)1 SetterDouble (org.apache.apex.malhar.lib.util.PojoUtils.SetterDouble)1 SetterFloat (org.apache.apex.malhar.lib.util.PojoUtils.SetterFloat)1 SetterInt (org.apache.apex.malhar.lib.util.PojoUtils.SetterInt)1 SetterLong (org.apache.apex.malhar.lib.util.PojoUtils.SetterLong)1 SetterShort (org.apache.apex.malhar.lib.util.PojoUtils.SetterShort)1 AvroRuntimeException (org.apache.avro.AvroRuntimeException)1 ParseException (org.json.simple.parser.ParseException)1