Search in sources :

Example 1 with TypeCodec

use of com.datastax.driver.core.TypeCodec in project cassandra by apache.

the class CQLSSTableWriterTest method testWritesWithUdts.

@Test
@SuppressWarnings("unchecked")
public void testWritesWithUdts() throws Exception {
    final String KS = "cql_keyspace3";
    final String TABLE = "table3";
    final String schema = "CREATE TABLE " + KS + "." + TABLE + " (" + "  k int," + "  v1 list<frozen<tuple2>>," + "  v2 frozen<tuple3>," + "  PRIMARY KEY (k)" + ")";
    File tempdir = Files.createTempDir();
    File dataDir = new File(tempdir.getAbsolutePath() + File.separator + KS + File.separator + TABLE);
    assert dataDir.mkdirs();
    CQLSSTableWriter writer = CQLSSTableWriter.builder().inDirectory(dataDir).withType("CREATE TYPE " + KS + ".tuple2 (a int, b int)").withType("CREATE TYPE " + KS + ".tuple3 (a int, b int, c int)").forTable(schema).using("INSERT INTO " + KS + "." + TABLE + " (k, v1, v2) " + "VALUES (?, ?, ?)").build();
    UserType tuple2Type = writer.getUDType("tuple2");
    UserType tuple3Type = writer.getUDType("tuple3");
    for (int i = 0; i < 100; i++) {
        writer.addRow(i, ImmutableList.builder().add(tuple2Type.newValue().setInt("a", i * 10).setInt("b", i * 20)).add(tuple2Type.newValue().setInt("a", i * 30).setInt("b", i * 40)).build(), tuple3Type.newValue().setInt("a", i * 100).setInt("b", i * 200).setInt("c", i * 300));
    }
    writer.close();
    loadSSTables(dataDir, KS);
    UntypedResultSet resultSet = QueryProcessor.executeInternal("SELECT * FROM " + KS + "." + TABLE);
    TypeCodec collectionCodec = UDHelper.codecFor(DataType.CollectionType.frozenList(tuple2Type));
    TypeCodec tuple3Codec = UDHelper.codecFor(tuple3Type);
    assertEquals(resultSet.size(), 100);
    int cnt = 0;
    for (UntypedResultSet.Row row : resultSet) {
        assertEquals(cnt, row.getInt("k"));
        List<UDTValue> values = (List<UDTValue>) collectionCodec.deserialize(row.getBytes("v1"), ProtocolVersion.NEWEST_SUPPORTED);
        assertEquals(values.get(0).getInt("a"), cnt * 10);
        assertEquals(values.get(0).getInt("b"), cnt * 20);
        assertEquals(values.get(1).getInt("a"), cnt * 30);
        assertEquals(values.get(1).getInt("b"), cnt * 40);
        UDTValue v2 = (UDTValue) tuple3Codec.deserialize(row.getBytes("v2"), ProtocolVersion.NEWEST_SUPPORTED);
        assertEquals(v2.getInt("a"), cnt * 100);
        assertEquals(v2.getInt("b"), cnt * 200);
        assertEquals(v2.getInt("c"), cnt * 300);
        cnt++;
    }
}
Also used : UDTValue(com.datastax.driver.core.UDTValue) TypeCodec(com.datastax.driver.core.TypeCodec) ImmutableList(com.google.common.collect.ImmutableList) File(java.io.File) UserType(com.datastax.driver.core.UserType) Test(org.junit.Test)

Example 2 with TypeCodec

use of com.datastax.driver.core.TypeCodec in project cassandra by apache.

the class CQLSSTableWriterTest method testWritesWithDependentUdts.

@Test
@SuppressWarnings("unchecked")
public void testWritesWithDependentUdts() throws Exception {
    final String KS = "cql_keyspace4";
    final String TABLE = "table4";
    final String schema = "CREATE TABLE " + KS + "." + TABLE + " (" + "  k int," + "  v1 frozen<nested_tuple>," + "  PRIMARY KEY (k)" + ")";
    File tempdir = Files.createTempDir();
    File dataDir = new File(tempdir.getAbsolutePath() + File.separator + KS + File.separator + TABLE);
    assert dataDir.mkdirs();
    CQLSSTableWriter writer = CQLSSTableWriter.builder().inDirectory(dataDir).withType("CREATE TYPE " + KS + ".nested_tuple (c int, tpl frozen<tuple2>)").withType("CREATE TYPE " + KS + ".tuple2 (a int, b int)").forTable(schema).using("INSERT INTO " + KS + "." + TABLE + " (k, v1) " + "VALUES (?, ?)").build();
    UserType tuple2Type = writer.getUDType("tuple2");
    UserType nestedTuple = writer.getUDType("nested_tuple");
    TypeCodec tuple2Codec = UDHelper.codecFor(tuple2Type);
    TypeCodec nestedTupleCodec = UDHelper.codecFor(nestedTuple);
    for (int i = 0; i < 100; i++) {
        writer.addRow(i, nestedTuple.newValue().setInt("c", i * 100).set("tpl", tuple2Type.newValue().setInt("a", i * 200).setInt("b", i * 300), tuple2Codec));
    }
    writer.close();
    loadSSTables(dataDir, KS);
    UntypedResultSet resultSet = QueryProcessor.executeInternal("SELECT * FROM " + KS + "." + TABLE);
    assertEquals(resultSet.size(), 100);
    int cnt = 0;
    for (UntypedResultSet.Row row : resultSet) {
        assertEquals(cnt, row.getInt("k"));
        UDTValue nestedTpl = (UDTValue) nestedTupleCodec.deserialize(row.getBytes("v1"), ProtocolVersion.NEWEST_SUPPORTED);
        assertEquals(nestedTpl.getInt("c"), cnt * 100);
        UDTValue tpl = nestedTpl.getUDTValue("tpl");
        assertEquals(tpl.getInt("a"), cnt * 200);
        assertEquals(tpl.getInt("b"), cnt * 300);
        cnt++;
    }
}
Also used : UDTValue(com.datastax.driver.core.UDTValue) TypeCodec(com.datastax.driver.core.TypeCodec) File(java.io.File) UserType(com.datastax.driver.core.UserType) Test(org.junit.Test)

Example 3 with TypeCodec

use of com.datastax.driver.core.TypeCodec in project nifi by apache.

the class AbstractCassandraProcessor method getCassandraObject.

protected static Object getCassandraObject(Row row, int i, DataType dataType) {
    if (dataType.equals(DataType.blob())) {
        return row.getBytes(i);
    } else if (dataType.equals(DataType.varint()) || dataType.equals(DataType.decimal())) {
        // AvroRuntimeException such as: "Unknown datum type: java.math.BigDecimal: 38"
        return row.getObject(i).toString();
    } else if (dataType.equals(DataType.cboolean())) {
        return row.getBool(i);
    } else if (dataType.equals(DataType.cint())) {
        return row.getInt(i);
    } else if (dataType.equals(DataType.bigint()) || dataType.equals(DataType.counter())) {
        return row.getLong(i);
    } else if (dataType.equals(DataType.ascii()) || dataType.equals(DataType.text()) || dataType.equals(DataType.varchar())) {
        return row.getString(i);
    } else if (dataType.equals(DataType.cfloat())) {
        return row.getFloat(i);
    } else if (dataType.equals(DataType.cdouble())) {
        return row.getDouble(i);
    } else if (dataType.equals(DataType.timestamp())) {
        return row.getTimestamp(i);
    } else if (dataType.equals(DataType.date())) {
        return row.getDate(i);
    } else if (dataType.equals(DataType.time())) {
        return row.getTime(i);
    } else if (dataType.isCollection()) {
        List<DataType> typeArguments = dataType.getTypeArguments();
        if (typeArguments == null || typeArguments.size() == 0) {
            throw new IllegalArgumentException("Column[" + i + "] " + dataType.getName() + " is a collection but no type arguments were specified!");
        }
        // Get the first type argument, to be used for lists and sets (and the first in a map)
        DataType firstArg = typeArguments.get(0);
        TypeCodec firstCodec = codecRegistry.codecFor(firstArg);
        if (dataType.equals(DataType.set(firstArg))) {
            return row.getSet(i, firstCodec.getJavaType());
        } else if (dataType.equals(DataType.list(firstArg))) {
            return row.getList(i, firstCodec.getJavaType());
        } else {
            // Must be an n-arg collection like map
            DataType secondArg = typeArguments.get(1);
            TypeCodec secondCodec = codecRegistry.codecFor(secondArg);
            if (dataType.equals(DataType.map(firstArg, secondArg))) {
                return row.getMap(i, firstCodec.getJavaType(), secondCodec.getJavaType());
            }
        }
    } else {
        // than numbers or booleans to strings by using the toString() method.
        return row.getObject(i).toString();
    }
    return null;
}
Also used : TypeCodec(com.datastax.driver.core.TypeCodec) DataType(com.datastax.driver.core.DataType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with TypeCodec

use of com.datastax.driver.core.TypeCodec in project nifi by apache.

the class PutCassandraQL method setStatementObject.

/**
 * Determines how to map the given value to the appropriate Cassandra data type and returns the object as
 * represented by the given type. This can be used in a Prepared/BoundStatement.
 *
 * @param statement  the BoundStatement for setting objects on
 * @param paramIndex the index of the parameter at which to set the object
 * @param attrName   the name of the attribute that the parameter is coming from - for logging purposes
 * @param paramValue the value of the CQL parameter to set
 * @param paramType  the Cassandra data type of the CQL parameter to set
 * @throws IllegalArgumentException if the PreparedStatement throws a CQLException when calling the appropriate setter
 */
protected void setStatementObject(final BoundStatement statement, final int paramIndex, final String attrName, final String paramValue, final String paramType) throws IllegalArgumentException {
    if (paramValue == null) {
        statement.setToNull(paramIndex);
        return;
    } else if (paramType == null) {
        throw new IllegalArgumentException("Parameter type for " + attrName + " cannot be null");
    } else {
        // Parse the top-level type and any parameterized types (for collections)
        final Matcher matcher = CQL_TYPE_PATTERN.matcher(paramType);
        // If the matcher doesn't match, this should fall through to the exception at the bottom
        if (matcher.find() && matcher.groupCount() > 1) {
            String mainTypeString = matcher.group(1).toLowerCase();
            DataType mainType = getPrimitiveDataTypeFromString(mainTypeString);
            if (mainType != null) {
                TypeCodec typeCodec = codecRegistry.codecFor(mainType);
                // Need the right statement.setXYZ() method
                if (mainType.equals(DataType.ascii()) || mainType.equals(DataType.text()) || mainType.equals(DataType.varchar()) || mainType.equals(DataType.timeuuid()) || mainType.equals(DataType.uuid()) || mainType.equals(DataType.inet()) || mainType.equals(DataType.varint())) {
                    // These are strings, so just use the paramValue
                    statement.setString(paramIndex, paramValue);
                } else if (mainType.equals(DataType.cboolean())) {
                    statement.setBool(paramIndex, (boolean) typeCodec.parse(paramValue));
                } else if (mainType.equals(DataType.cint())) {
                    statement.setInt(paramIndex, (int) typeCodec.parse(paramValue));
                } else if (mainType.equals(DataType.bigint()) || mainType.equals(DataType.counter())) {
                    statement.setLong(paramIndex, (long) typeCodec.parse(paramValue));
                } else if (mainType.equals(DataType.cfloat())) {
                    statement.setFloat(paramIndex, (float) typeCodec.parse(paramValue));
                } else if (mainType.equals(DataType.cdouble())) {
                    statement.setDouble(paramIndex, (double) typeCodec.parse(paramValue));
                } else if (mainType.equals(DataType.blob())) {
                    statement.setBytes(paramIndex, (ByteBuffer) typeCodec.parse(paramValue));
                } else if (mainType.equals(DataType.timestamp())) {
                    statement.setTimestamp(paramIndex, (Date) typeCodec.parse(paramValue));
                }
                return;
            } else {
                // Get the first parameterized type
                if (matcher.groupCount() > 2) {
                    String firstParamTypeName = matcher.group(3);
                    DataType firstParamType = getPrimitiveDataTypeFromString(firstParamTypeName);
                    if (firstParamType == null) {
                        throw new IllegalArgumentException("Nested collections are not supported");
                    }
                    // Check for map type
                    if (DataType.Name.MAP.toString().equalsIgnoreCase(mainTypeString)) {
                        if (matcher.groupCount() > 4) {
                            String secondParamTypeName = matcher.group(5);
                            DataType secondParamType = getPrimitiveDataTypeFromString(secondParamTypeName);
                            DataType mapType = DataType.map(firstParamType, secondParamType);
                            statement.setMap(paramIndex, (Map) codecRegistry.codecFor(mapType).parse(paramValue));
                            return;
                        }
                    } else {
                        // Must be set or list
                        if (DataType.Name.SET.toString().equalsIgnoreCase(mainTypeString)) {
                            DataType setType = DataType.set(firstParamType);
                            statement.setSet(paramIndex, (Set) codecRegistry.codecFor(setType).parse(paramValue));
                            return;
                        } else if (DataType.Name.LIST.toString().equalsIgnoreCase(mainTypeString)) {
                            DataType listType = DataType.list(firstParamType);
                            statement.setList(paramIndex, (List) codecRegistry.codecFor(listType).parse(paramValue));
                            return;
                        }
                    }
                } else {
                    throw new IllegalArgumentException("Collection type " + mainTypeString + " needs parameterized type(s), such as set<text>");
                }
            }
        }
    }
    throw new IllegalArgumentException("Cannot create object of type " + paramType + " using input " + paramValue);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Matcher(java.util.regex.Matcher) TypeCodec(com.datastax.driver.core.TypeCodec) DataType(com.datastax.driver.core.DataType) ByteBuffer(java.nio.ByteBuffer) Map(java.util.Map)

Example 5 with TypeCodec

use of com.datastax.driver.core.TypeCodec 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)

Aggregations

TypeCodec (com.datastax.driver.core.TypeCodec)6 UDTValue (com.datastax.driver.core.UDTValue)3 UserType (com.datastax.driver.core.UserType)3 CodecRegistry (com.datastax.driver.core.CodecRegistry)2 DataType (com.datastax.driver.core.DataType)2 File (java.io.File)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1