Java Code Examples for com.datastax.driver.core.DataType#Name

The following examples show how to use com.datastax.driver.core.DataType#Name . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PojoField.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Gets field value as an object having Cassandra compatible type.
 * This it could be stored directly into Cassandra without any conversions.
 *
 * @param obj Object instance.
 * @param serializer {@link org.apache.ignite.cache.store.cassandra.serializer.Serializer} to use.
 * @return Object to store in Cassandra table column.
 */
public Object getValueFromObject(Object obj, Serializer serializer) {
    Object val = accessor.getValue(obj);

    if (val == null)
        return null;

    DataType.Name cassandraType = PropertyMappingHelper.getCassandraType(val.getClass());

    if (cassandraType != null)
        return val;

    if (serializer == null) {
        throw new IllegalStateException("Can't serialize value from object '" +
            val.getClass().getName() + "' field '" + name + "', cause there is no BLOB serializer specified");
    }

    return serializer.serialize(val);
}
 
Example 2
Source File: ColumnDefinitions.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
DataType.Name checkType(int i, DataType.Name name1, DataType.Name name2) {
    DataType defined = getType(i);
    if (name1 != defined.getName() && name2 != defined.getName())
        throw new InvalidTypeException(String.format("Column %s is of type %s", getName(i), defined));

    return defined.getName();
}
 
Example 3
Source File: DatastaxGenericFloatGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
private DataType.Name validateName(DataType dataType) {

        final DataType.Name name = dataType.getName();
        if (DataTypeHelper.isNumber(name)) {
            return name;
        }

        throw new IllegalArgumentException("Datatype " + dataType + " not a number");
    }
 
Example 4
Source File: DatastaxGenericIntegerGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
private DataType.Name validateName(DataType dataType) {

        final DataType.Name name = dataType.getName();
        if (DataTypeHelper.isNumber(name)) {
            return name;
        }

        throw new IllegalArgumentException("Datatype " + dataType + " not a number");
    }
 
Example 5
Source File: DatastaxGenericByteGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
private DataType.Name validateName(DataType dataType) {

        final DataType.Name name = dataType.getName();
        if (DataTypeHelper.isNumber(name)) {
            return name;
        }
        throw new IllegalArgumentException("Datatype " + dataType + " not a number");
    }
 
Example 6
Source File: CellValidator.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * private constructor.
 */
private CellValidator(String validatorClassName, Kind validatorKind, Collection<String> validatorTypes,
                      DataType.Name cqlTypeName) {
    this.validatorClassName = validatorClassName != null ? validatorClassName : DEFAULT_VALIDATOR_CLASSNAME;
    this.validatorKind = validatorKind;
    this.validatorTypes = validatorTypes;
    this.cqlTypeName = cqlTypeName;
}
 
Example 7
Source File: CellValidator.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a CellValidator for a generic instance of an object.
 * We need the actual instance in order to differentiate between an UUID and a TimeUUID.
 *
 * @param obj an instance to use to build the new CellValidator.
 * @param <T> the generic type of the provided object instance.
 * @return a new CellValidator associated to the provided object.
 */
public static <T> CellValidator cellValidator(T obj) {
    if (obj == null) {
        return null;
    }

    Kind kind = Kind.objectToKind(obj);
    AbstractType<?> tAbstractType = CassandraUtils.marshallerInstance(obj);
    String validatorClassName = tAbstractType.getClass().getCanonicalName();
    Collection<String> validatorTypes = null;
    DataType.Name cqlTypeName = MAP_JAVA_TYPE_TO_DATA_TYPE_NAME.get(validatorClassName);// tAbstractType.get

    return new CellValidator(validatorClassName, kind, validatorTypes, cqlTypeName);
}
 
Example 8
Source File: ColumnDefinitions.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
DataType.Name checkType(int i, DataType.Name name1, DataType.Name name2, DataType.Name name3) {
    DataType defined = getType(i);
    if (name1 != defined.getName() && name2 != defined.getName() && name3 != defined.getName())
        throw new InvalidTypeException(String.format("Column %s is of type %s", getName(i), defined));

    return defined.getName();
}
 
Example 9
Source File: CassandraHelper.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if two Java classes are Cassandra compatible - mapped to the same Cassandra type.
 *
 * @param type1 First type.
 * @param type2 Second type.
 * @return {@code true} if classes are compatible and {@code false} if not.
 */
public static boolean isCassandraCompatibleTypes(Class type1, Class type2) {
    if (type1 == null || type2 == null)
        return false;

    DataType.Name t1 = PropertyMappingHelper.getCassandraType(type1);
    DataType.Name t2 = PropertyMappingHelper.getCassandraType(type2);

    return t1 != null && t2 != null && t1.equals(t2);
}
 
Example 10
Source File: PojoField.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes field info from property descriptor.
 *
 * @param accessor {@link PojoFieldAccessor} accessor.
 */
private void init(PojoFieldAccessor accessor) {
    DataType.Name cassandraType = PropertyMappingHelper.getCassandraType(accessor.getFieldType());
    cassandraType = cassandraType == null ? DataType.Name.BLOB : cassandraType;

    this.colDDL = "\"" + col + "\" " + cassandraType.toString();

    this.accessor = accessor;
}
 
Example 11
Source File: DatastaxGenericLongGetter.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
private DataType.Name validateName(DataType dataType) {

        final DataType.Name name = dataType.getName();
        if (DataTypeHelper.isNumber(name)) {
            return name;
        }

        throw new IllegalArgumentException("Datatype " + dataType + " not a number");
    }
 
Example 12
Source File: ColumnDefinitions.java    From cassandra-jdbc-wrapper with Apache License 2.0 4 votes vote down vote up
void checkType(int i, DataType.Name name) {
    DataType defined = getType(i);
    if (name != defined.getName())
        throw new InvalidTypeException(String.format("Column %s is of type %s", getName(i), defined));
}
 
Example 13
Source File: CQLUtilsTest.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Test
public void testLegacyCachingOptions() throws Exception{

    final CassandraFig cassandraFig = mock(CassandraFig.class);
    when(cassandraFig.getVersion()).thenReturn("2.0");

    Map<String, DataType.Name> columns = new HashMap<>();
    columns.put("key", DataType.Name.BLOB);
    columns.put("column1", DataType.Name.TEXT);
    columns.put("value", DataType.Name.BLOB);

    List<String> partitionKeys = new ArrayList<>();
    partitionKeys.add("key");

    List<String> columnKeys = new ArrayList<>();
    columnKeys.add("column1");

    Map<String, String> clusteringOrder = new HashMap<>();
    clusteringOrder.put("column1", "DESC");



    TableDefinitionImpl table1 = new TableDefinitionImpl( cassandraFig.getApplicationKeyspace(),
        CQLUtils.quote("table1"),
        partitionKeys,
        columnKeys,
        columns,
        TableDefinitionImpl.CacheOption.KEYS,
        clusteringOrder
    );

    String createCQL = table1.getTableCQL(cassandraFig, TableDefinition.ACTION.CREATE);
    logger.info(createCQL);
    assertTrue(
        createCQL.contains( "\"keys_only\"" ) &&
            !createCQL.contains( "'keys':'ALL'"  )

    );



}
 
Example 14
Source File: CellValidator.java    From deep-spark with Apache License 2.0 4 votes vote down vote up
/**
 * @return the original CQL3 type name (if known, null otherwise)
 */
public DataType.Name getCqlTypeName() {
    return cqlTypeName;
}
 
Example 15
Source File: CQLUtilsTest.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Test
public void testTableCQL() throws Exception {


    Map<String, DataType.Name> columns = new HashMap<>();
    columns.put("key", DataType.Name.BLOB);
    columns.put("column1", DataType.Name.TEXT);
    columns.put("value", DataType.Name.BLOB);

    List<String> partitionKeys = new ArrayList<>();
    partitionKeys.add("key");

    List<String> columnKeys = new ArrayList<>();
    columnKeys.add("column1");

    Map<String, String> clusteringOrder = new HashMap<>();
    clusteringOrder.put("column1", "DESC");



    TableDefinitionImpl table1 = new TableDefinitionImpl( cassandraFig.getApplicationKeyspace(),
        CQLUtils.quote("table1"),
        partitionKeys,
        columnKeys,
        columns,
        TableDefinitionImpl.CacheOption.KEYS,
        clusteringOrder
        );

    String createCQL = table1.getTableCQL(cassandraFig, TableDefinition.ACTION.CREATE);
    String updateCQL = table1.getTableCQL(cassandraFig, TableDefinition.ACTION.UPDATE);

    assertTrue(
        createCQL.contains(CQLUtils.CREATE_TABLE ) &&
            !createCQL.contains( CQLUtils.ALTER_TABLE )  &&
            createCQL.contains( DataType.Name.BLOB.toString() ) &&
            createCQL.contains( DataType.Name.TEXT.toString() )

    );
    assertTrue(
        updateCQL.contains( CQLUtils.ALTER_TABLE ) &&
            !updateCQL.contains( CQLUtils.CREATE_TABLE ) &&
            !updateCQL.contains( DataType.Name.BLOB.toString() ) &&
            !updateCQL.contains( DataType.Name.TEXT.toString() )
    );
    logger.info(createCQL);
    logger.info(updateCQL);

}
 
Example 16
Source File: CQLUtilsTest.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Test
public void testCachingOptions() throws Exception {

    final CassandraFig cassandraFig = mock(CassandraFig.class);
    when(cassandraFig.getVersion()).thenReturn("2.1");

    Map<String, DataType.Name> columns = new HashMap<>();
    columns.put("key", DataType.Name.BLOB);
    columns.put("column1", DataType.Name.TEXT);
    columns.put("value", DataType.Name.BLOB);

    List<String> partitionKeys = new ArrayList<>();
    partitionKeys.add("key");

    List<String> columnKeys = new ArrayList<>();
    columnKeys.add("column1");

    Map<String, String> clusteringOrder = new HashMap<>();
    clusteringOrder.put("column1", "DESC");



    TableDefinitionImpl table1 = new TableDefinitionImpl( cassandraFig.getApplicationKeyspace(),
        CQLUtils.quote("table1"),
        partitionKeys,
        columnKeys,
        columns,
        TableDefinitionImpl.CacheOption.KEYS,
        clusteringOrder
    );

    String createCQL = table1.getTableCQL(cassandraFig, TableDefinition.ACTION.CREATE);
    logger.info(createCQL);
    assertTrue(
        createCQL.contains( "'keys':'ALL'"  ) &&
        !createCQL.contains( "\"keys_only\"" )

    );


}
 
Example 17
Source File: CassandraDataHandler.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
private ODataDataType getDataType(DataType.Name dataTypeName) {
    ODataDataType dataType;
    switch (dataTypeName) {
        case ASCII:
/* fall through */
        case TEXT:
/* fall through */
        case VARCHAR:
/* fall through */
        case TIMEUUID:
            dataType = ODataDataType.STRING;
            break;
        case UUID:
            dataType = ODataDataType.GUID;
            break;
        case BIGINT:
/* fall through */
        case VARINT:
/* fall through */
        case COUNTER:
            dataType = ODataDataType.INT64;
            break;
        case BLOB:
            dataType = ODataDataType.BINARY;
            break;
        case BOOLEAN:
            dataType = ODataDataType.BOOLEAN;
            break;
        case DECIMAL:
/* fall through */
        case FLOAT:
            dataType = ODataDataType.DECIMAL;
            break;
        case DOUBLE:
            dataType = ODataDataType.DOUBLE;
            break;
        case INT:
            dataType = ODataDataType.INT32;
            break;
        case TIMESTAMP:
            dataType = ODataDataType.DATE_TIMEOFFSET;
            break;
        case TIME:
            dataType = ODataDataType.TIMEOFDAY;
            break;
        case DATE:
            dataType = ODataDataType.DATE;
            break;
        default:
            dataType = ODataDataType.STRING;
            break;
    }
    return dataType;
}
 
Example 18
Source File: DatastaxGenericShortGetter.java    From SimpleFlatMapper with MIT License 3 votes vote down vote up
private DataType.Name validateName(DataType dataType) {

        final DataType.Name name = dataType.getName();

        if (DataTypeHelper.isNumber(name)) {
            return name;
        }

        throw new IllegalArgumentException("Datatype " + dataType + " not a number");
    }
 
Example 19
Source File: DatastaxGenericBigDecimalGetter.java    From SimpleFlatMapper with MIT License 3 votes vote down vote up
private DataType.Name validateName(DataType dataType) {

        final DataType.Name name = dataType.getName();

        if (DataTypeHelper.isNumber(name)) {
            return name;
        }

        throw new IllegalArgumentException("Datatype " + dataType + " not a number");
    }
 
Example 20
Source File: PropertyMappingHelper.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * Maps Cassandra type to specified Java type.
 *
 * @param clazz java class.
 *
 * @return Cassandra type.
 */
public static DataType.Name getCassandraType(Class clazz) {
    return JAVA_TO_CASSANDRA_MAPPING.get(clazz);
}