Search in sources :

Example 1 with Setter

use of org.apache.apex.malhar.lib.util.PojoUtils.Setter in project apex-malhar by apache.

the class CouchDBPOJOInputOperator method getTuple.

@Override
@SuppressWarnings("unchecked")
public Object getTuple(Row value) throws IOException {
    Object obj;
    try {
        obj = objectClass.newInstance();
    } catch (InstantiationException ex) {
        throw new RuntimeException(ex);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException(ex);
    }
    if (setterDocId != null) {
        setterDocId.set(obj, value.getId());
    }
    JsonNode val = value.getValueAsNode();
    for (int i = 0; i < setterDoc.size(); i++) {
        Class<?> type = fieldType.get(i);
        if (type.isPrimitive()) {
            if (type == int.class) {
                ((SetterInt) setterDoc.get(i)).set(obj, val.get(columns.get(i)).getIntValue());
            } else if (type == boolean.class) {
                ((SetterBoolean) setterDoc.get(i)).set(obj, val.get(columns.get(i)).getBooleanValue());
            } else if (type == long.class) {
                ((SetterLong) setterDoc.get(i)).set(obj, val.get(columns.get(i)).getLongValue());
            } else if (type == double.class) {
                ((SetterDouble) setterDoc.get(i)).set(obj, val.get(columns.get(i)).getDoubleValue());
            } else {
                throw new RuntimeException("Type is not supported");
            }
        } else {
            ((Setter<Object, Object>) setterDoc.get(i)).set(obj, mapper.readValue(val.get(columns.get(i)), type));
        }
    }
    return obj;
}
Also used : SetterLong(org.apache.apex.malhar.lib.util.PojoUtils.SetterLong) Setter(org.apache.apex.malhar.lib.util.PojoUtils.Setter) JsonNode(org.codehaus.jackson.JsonNode) SetterInt(org.apache.apex.malhar.lib.util.PojoUtils.SetterInt)

Example 2 with Setter

use of org.apache.apex.malhar.lib.util.PojoUtils.Setter in project apex-malhar by apache.

the class RedisPOJOInputOperator method convertMapToObject.

@SuppressWarnings("unchecked")
private Object convertMapToObject(Map<String, String> tuple) {
    try {
        Object mappedObject = objectClass.newInstance();
        for (int i = 0; i < dataColumns.size(); i++) {
            final SupportType type = dataColumns.get(i).getType();
            final String columnName = dataColumns.get(i).getColumnName();
            if (i < setters.size()) {
                String value = tuple.get(columnName);
                switch(type) {
                    case STRING:
                        ((Setter<Object, String>) setters.get(i)).set(mappedObject, value);
                        break;
                    case BOOLEAN:
                        ((SetterBoolean) setters.get(i)).set(mappedObject, Boolean.parseBoolean(value));
                        break;
                    case SHORT:
                        ((SetterShort) setters.get(i)).set(mappedObject, Short.parseShort(value));
                        break;
                    case INTEGER:
                        ((SetterInt) setters.get(i)).set(mappedObject, Integer.parseInt(value));
                        break;
                    case LONG:
                        ((SetterLong) setters.get(i)).set(mappedObject, Long.parseLong(value));
                        break;
                    case FLOAT:
                        ((SetterFloat) setters.get(i)).set(mappedObject, Float.parseFloat(value));
                        break;
                    case DOUBLE:
                        ((SetterDouble) setters.get(i)).set(mappedObject, Double.parseDouble(value));
                        break;
                    default:
                        break;
                }
            }
        }
        return mappedObject;
    } catch (Exception e) {
        DTThrowable.wrapIfChecked(e);
    }
    return null;
}
Also used : SetterLong(org.apache.apex.malhar.lib.util.PojoUtils.SetterLong) SetterDouble(org.apache.apex.malhar.lib.util.PojoUtils.SetterDouble) SetterShort(org.apache.apex.malhar.lib.util.PojoUtils.SetterShort) SupportType(org.apache.apex.malhar.lib.util.FieldInfo.SupportType) Setter(org.apache.apex.malhar.lib.util.PojoUtils.Setter) SetterFloat(org.apache.apex.malhar.lib.util.PojoUtils.SetterFloat) SetterBoolean(org.apache.apex.malhar.lib.util.PojoUtils.SetterBoolean) SetterInt(org.apache.apex.malhar.lib.util.PojoUtils.SetterInt)

Example 3 with Setter

use of org.apache.apex.malhar.lib.util.PojoUtils.Setter in project apex-malhar by apache.

the class FieldValueSerializableGenerator method deserializeObject.

public Object deserializeObject(byte[] bytes) {
    Object obj = getKryo().readClassAndObject(new Input(bytes));
    if (fieldGetterMap == null || fieldGetterMap.isEmpty()) {
        return obj;
    }
    // the obj in fact is a map, convert from map to object
    try {
        Map valueMap = (Map) obj;
        obj = clazz.newInstance();
        for (Map.Entry<T, Setter<Object, Object>> entry : fieldSetterMap.entrySet()) {
            T fieldInfo = entry.getKey();
            Setter<Object, Object> setter = entry.getValue();
            if (setter != null) {
                setter.set(obj, valueMap.get(fieldInfo.getColumnName()));
            }
        }
        return obj;
    } catch (Exception e) {
        logger.warn("Coverting map to obj exception. ", e);
        return obj;
    }
}
Also used : Input(com.esotericsoftware.kryo.io.Input) Setter(org.apache.apex.malhar.lib.util.PojoUtils.Setter) Map(java.util.Map)

Example 4 with Setter

use of org.apache.apex.malhar.lib.util.PojoUtils.Setter in project apex-malhar by apache.

the class CassandraPOJOInputOperator method getTuple.

@Override
@SuppressWarnings("unchecked")
public Object getTuple(Row row) {
    Object obj;
    try {
        // This code will be replaced after integration of creating POJOs on the fly utility.
        obj = pojoClass.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        throw new RuntimeException(ex);
    }
    for (int i = 0; i < columnDataTypes.size(); i++) {
        DataType type = columnDataTypes.get(i);
        String columnName = fieldInfos.get(i).getColumnName();
        switch(type.getName()) {
            case UUID:
                final UUID id = row.getUUID(columnName);
                ((Setter<Object, UUID>) setters.get(i)).set(obj, id);
                break;
            case ASCII:
            case VARCHAR:
            case TEXT:
                final String ascii = row.getString(columnName);
                ((Setter<Object, String>) setters.get(i)).set(obj, ascii);
                break;
            case BOOLEAN:
                final boolean bool = row.getBool(columnName);
                ((SetterBoolean) setters.get(i)).set(obj, bool);
                break;
            case INT:
                final int intValue = row.getInt(columnName);
                ((SetterInt) setters.get(i)).set(obj, intValue);
                break;
            case BIGINT:
            case COUNTER:
                final long longValue = row.getLong(columnName);
                ((SetterLong) setters.get(i)).set(obj, longValue);
                break;
            case FLOAT:
                final float floatValue = row.getFloat(columnName);
                ((SetterFloat) setters.get(i)).set(obj, floatValue);
                break;
            case DOUBLE:
                final double doubleValue = row.getDouble(columnName);
                ((SetterDouble) setters.get(i)).set(obj, doubleValue);
                break;
            case DECIMAL:
                final BigDecimal decimal = row.getDecimal(columnName);
                ((Setter<Object, BigDecimal>) setters.get(i)).set(obj, decimal);
                break;
            case SET:
                Set<?> set = row.getSet(columnName, Object.class);
                ((Setter<Object, Set<?>>) setters.get(i)).set(obj, set);
                break;
            case MAP:
                final Map<?, ?> map = row.getMap(columnName, Object.class, Object.class);
                ((Setter<Object, Map<?, ?>>) setters.get(i)).set(obj, map);
                break;
            case LIST:
                final List<?> list = row.getList(columnName, Object.class);
                ((Setter<Object, List<?>>) setters.get(i)).set(obj, list);
                break;
            case TIMESTAMP:
                final Date date = new Date(row.getDate(columnName).getMillisSinceEpoch());
                ((Setter<Object, Date>) setters.get(i)).set(obj, date);
                break;
            default:
                throw new RuntimeException("unsupported data type " + type.getName());
        }
    }
    return obj;
}
Also used : SetterDouble(org.apache.apex.malhar.lib.util.PojoUtils.SetterDouble) SetterFloat(org.apache.apex.malhar.lib.util.PojoUtils.SetterFloat) BigDecimal(java.math.BigDecimal) Date(java.util.Date) SetterLong(org.apache.apex.malhar.lib.util.PojoUtils.SetterLong) Setter(org.apache.apex.malhar.lib.util.PojoUtils.Setter) DataType(com.datastax.driver.core.DataType) SetterBoolean(org.apache.apex.malhar.lib.util.PojoUtils.SetterBoolean) UUID(java.util.UUID) SetterInt(org.apache.apex.malhar.lib.util.PojoUtils.SetterInt)

Aggregations

Setter (org.apache.apex.malhar.lib.util.PojoUtils.Setter)4 SetterInt (org.apache.apex.malhar.lib.util.PojoUtils.SetterInt)3 SetterLong (org.apache.apex.malhar.lib.util.PojoUtils.SetterLong)3 SetterBoolean (org.apache.apex.malhar.lib.util.PojoUtils.SetterBoolean)2 SetterDouble (org.apache.apex.malhar.lib.util.PojoUtils.SetterDouble)2 SetterFloat (org.apache.apex.malhar.lib.util.PojoUtils.SetterFloat)2 DataType (com.datastax.driver.core.DataType)1 Input (com.esotericsoftware.kryo.io.Input)1 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)1 Map (java.util.Map)1 UUID (java.util.UUID)1 SupportType (org.apache.apex.malhar.lib.util.FieldInfo.SupportType)1 SetterShort (org.apache.apex.malhar.lib.util.PojoUtils.SetterShort)1 JsonNode (org.codehaus.jackson.JsonNode)1