Search in sources :

Example 1 with SetterBoolean

use of org.apache.apex.malhar.lib.util.PojoUtils.SetterBoolean 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 2 with SetterBoolean

use of org.apache.apex.malhar.lib.util.PojoUtils.SetterBoolean 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)2 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 SetterInt (org.apache.apex.malhar.lib.util.PojoUtils.SetterInt)2 SetterLong (org.apache.apex.malhar.lib.util.PojoUtils.SetterLong)2 DataType (com.datastax.driver.core.DataType)1 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)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