Search in sources :

Example 1 with CodecRegistry

use of com.datastax.driver.core.CodecRegistry in project storm by apache.

the class ObjectMapperCqlStatementMapper method getMappingManager.

private MappingManager getMappingManager(Session session) {
    synchronized (mappingManagers) {
        MappingManager mappingManager = mappingManagers.get(session);
        if (mappingManager == null) {
            mappingManager = new MappingManager(session);
            mappingManagers.put(session, mappingManager);
            CodecRegistry codecRegistry = session.getCluster().getConfiguration().getCodecRegistry();
            for (TypeCodec<?> codec : codecs) {
                codecRegistry.register(codec);
            }
            for (Class<?> udtClass : udtClasses) {
                mappingManager.udtCodec(udtClass);
            }
        }
        return mappingManager;
    }
}
Also used : MappingManager(com.datastax.driver.mapping.MappingManager) CodecRegistry(com.datastax.driver.core.CodecRegistry)

Example 2 with CodecRegistry

use of com.datastax.driver.core.CodecRegistry in project ats-framework by Axway.

the class CassandraDbProvider method extractObjectFromResultSet.

private static Object extractObjectFromResultSet(Row row, Definition columnDefinition) {
    Object object;
    String columnName = columnDefinition.getName();
    DataType dataType = columnDefinition.getType();
    Name columnTypeName = dataType.getName();
    if (columnTypeName.equals(DataType.Name.UUID)) {
        object = row.getUUID(columnName);
    } else if (columnTypeName.equals(DataType.Name.TIMEUUID)) {
        object = row.getUUID(columnName);
    } else if (columnTypeName.equals(DataType.Name.BOOLEAN)) {
        /* By default null values are deserialized to false (False) 
             * That's why this check is needed, in order to distinguish between null and false values
             * */
        if (row.isNull(columnName)) {
            object = null;
        } else {
            object = row.getBool(columnName);
        }
    } else if (columnTypeName.equals(DataType.Name.INT)) {
        /* By default null values are deserialized to 0
             * That's why this check is needed, in order to distinguish between null and 0 values
             * */
        if (row.isNull(columnName)) {
            object = null;
        } else {
            object = row.getInt(columnName);
        }
    } else if (columnTypeName.equals(DataType.Name.BIGINT)) {
        /* By default null values are deserialized to 0
             * That's why this check is needed, in order to distinguish between null and 0 values
             * */
        if (row.isNull(columnName)) {
            object = null;
        } else {
            object = row.getLong(columnName);
        }
    } else if (columnTypeName.equals(DataType.Name.FLOAT)) {
        /* By default null values are deserialized to 0
             * That's why this check is needed, in order to distinguish between null and 0 values
             * */
        if (row.isNull(columnName)) {
            object = null;
        } else {
            object = row.getFloat(columnName);
        }
    } else if (columnTypeName.equals(DataType.Name.DOUBLE)) {
        /* By default null values are deserialized to 0
             * That's why this check is needed, in order to distinguish between null and 0 values
             * */
        if (row.isNull(columnName)) {
            object = null;
        } else {
            object = row.getDouble(columnName);
        }
    } else if (columnTypeName.equals(DataType.Name.COUNTER)) {
        /* By default null values are deserialized to 0
             * That's why this check is needed, in order to distinguish between null and 0 values
             * */
        if (row.isNull(columnName)) {
            object = null;
        } else {
            object = row.getLong(columnName);
        }
    } else if (columnTypeName.equals(DataType.Name.DECIMAL)) {
        object = row.getDecimal(columnName);
    } else if (columnTypeName.equals(DataType.Name.TEXT) || columnTypeName.equals(DataType.Name.VARCHAR)) {
        object = row.getString(columnName);
    } else if (columnTypeName.equals(DataType.Name.TIMESTAMP)) {
        object = row.getTimestamp(columnName);
    } else if (columnTypeName.equals(DataType.Name.DATE)) {
        object = row.getDate(columnName);
    } else if (columnTypeName.equals(DataType.Name.BLOB)) {
        object = row.getBytes(columnName);
    } else if (columnTypeName.equals(DataType.Name.SET)) {
        // this is the class of the set's elements (i.e. for a Set<String>, clazz variable will be equal to String.class)
        Class<?> clazz = new CodecRegistry().codecFor(dataType.getTypeArguments().get(0)).getJavaType().getRawType();
        object = row.getSet(columnName, clazz);
    } else if (columnTypeName.equals(DataType.Name.LIST)) {
        // this is the class of the list's elements (i.e. for a Set<String>, clazz variable will be equal to String.class)
        Class<?> clazz = new CodecRegistry().codecFor(dataType.getTypeArguments().get(0)).getJavaType().getRawType();
        object = row.getList(columnName, clazz);
    } else if (columnTypeName.equals(DataType.Name.MAP)) {
        /* this is the class of the map's key and value elements
             * for a Map<Integer, String>,
             * keyClazz variable will be equal to Integer.class
             * and valueClazz variable will be equal to String.class
             */
        Class<?> keyClazz = new CodecRegistry().codecFor(dataType.getTypeArguments().get(0)).getJavaType().getRawType();
        Class<?> valueClazz = new CodecRegistry().codecFor(dataType.getTypeArguments().get(1)).getJavaType().getRawType();
        object = row.getMap(columnName, keyClazz, valueClazz);
    } else {
        throw new DbException("Unsupported data type '" + columnDefinition.getType().toString() + "' for table '" + columnDefinition.getTable() + "' and column '" + columnName + "'");
    }
    return object;
}
Also used : DataType(com.datastax.driver.core.DataType) CodecRegistry(com.datastax.driver.core.CodecRegistry) Name(com.datastax.driver.core.DataType.Name) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 3 with CodecRegistry

use of com.datastax.driver.core.CodecRegistry in project apex-malhar by apache.

the class AbstractUpsertOutputOperator method registerCodecs.

private void registerCodecs() {
    complexTypeCodecs = getCodecsForUserDefinedTypes();
    if (complexTypeCodecs != null) {
        CodecRegistry registry = cluster.getConfiguration().getCodecRegistry();
        if (cluster.getConfiguration().getProtocolOptions().getProtocolVersion().toInt() < 4) {
            LOG.error("Custom codecs are not supported for protocol version < 4");
            throw new RuntimeException("Custom codecs are not supported for protocol version < 4");
        }
        for (String typeCodecStr : complexTypeCodecs.keySet()) {
            TypeCodec codec = complexTypeCodecs.get(typeCodecStr);
            registry.register(codec);
            userDefinedTypesClass.put(typeCodecStr, codec.getJavaType().getRawType());
        }
    } else {
        complexTypeCodecs = new HashMap<>();
    }
}
Also used : TypeCodec(com.datastax.driver.core.TypeCodec) CodecRegistry(com.datastax.driver.core.CodecRegistry)

Example 4 with CodecRegistry

use of com.datastax.driver.core.CodecRegistry in project apex-malhar by apache.

the class UserUpsertOperator method getCodecsForUserDefinedTypes.

@Override
public Map<String, TypeCodec> getCodecsForUserDefinedTypes() {
    Map<String, TypeCodec> allCodecs = new HashMap<>();
    CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry();
    UserType addressType = cluster.getMetadata().getKeyspace(getConnectionStateManager().getKeyspaceName()).getUserType("address");
    TypeCodec<UDTValue> addressTypeCodec = codecRegistry.codecFor(addressType);
    AddressCodec addressCodec = new AddressCodec(addressTypeCodec, Address.class);
    allCodecs.put("currentaddress", addressCodec);
    UserType userFullNameType = cluster.getMetadata().getKeyspace(getConnectionStateManager().getKeyspaceName()).getUserType("fullname");
    TypeCodec<UDTValue> userFullNameTypeCodec = codecRegistry.codecFor(userFullNameType);
    FullNameCodec fullNameCodec = new FullNameCodec(userFullNameTypeCodec, FullName.class);
    allCodecs.put("username", fullNameCodec);
    return allCodecs;
}
Also used : UDTValue(com.datastax.driver.core.UDTValue) HashMap(java.util.HashMap) TypeCodec(com.datastax.driver.core.TypeCodec) CodecRegistry(com.datastax.driver.core.CodecRegistry) UserType(com.datastax.driver.core.UserType)

Aggregations

CodecRegistry (com.datastax.driver.core.CodecRegistry)4 TypeCodec (com.datastax.driver.core.TypeCodec)2 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 DataType (com.datastax.driver.core.DataType)1 Name (com.datastax.driver.core.DataType.Name)1 UDTValue (com.datastax.driver.core.UDTValue)1 UserType (com.datastax.driver.core.UserType)1 MappingManager (com.datastax.driver.mapping.MappingManager)1 HashMap (java.util.HashMap)1