DataType.asJavaClass - Is there a replacement/alternative ?

225 views
Skip to first unread message

IP

unread,
Jan 10, 2016, 12:10:11 AM1/10/16
to DataStax Java Driver for Apache Cassandra User Mailing List
Is there way to get the Java Class of the Datatype on the java driver version > 2.1 ?

Looks like the method that does it( com.datastax.driver.core.DataType.asJavaClass() )  was removed from on v2.2 on.

Here is the piece of code that depends on that method.

-----
        ResultSet resultSet = session.execute(boundStatement);

        int totRows = resultSet.getAvailableWithoutFetching();

        if (totRows > 0) {

            tList = new ArrayList<T>(totRows);

            List<ColumnDefinitions.Definition> columnDefinitions = resultSet.getColumnDefinitions().asList();

            Iterator<Row> rowsIterator = resultSet.iterator();
            while (rowsIterator.hasNext()) {

                Row row = rowsIterator.next();
                T instance = (T) clazz.newInstance();

                for (ColumnDefinitions.Definition columnDefinition : columnDefinitions) {
                   
                    Object value = row.getObject(columnDefinition.getName());
                   
                    PropertyDescriptor propertyDescriptor = new PropertyDescriptor(columnDefinition.getName(),
                            columnDefinition.getType().asJavaClass());

                    Method method = propertyDescriptor.getWriteMethod();

                    method.invoke(instance, value);

                }

            }

        }

        return tList;

    }
-----   
   

Thanks
IPVP

DuyHai Doan

unread,
Jan 10, 2016, 8:38:18 AM1/10/16
to DataStax Java Driver for Apache Cassandra User Mailing List

Since the introduction of the codec system, the .asJavaClass() method has been removed. Alternative way:

TypeToken<?> typeToken = session.getCluster().getConfiguration().getCodecRegistry().codecFor(myDataType).getJavaType()

From the TypeToken of Guava, you can call getRawType() to obtain a raw Class<?> or getImmediateRawTypes()

Jeremy Truelove

unread,
Jan 27, 2016, 5:48:19 PM1/27/16
to DataStax Java Driver for Apache Cassandra User Mailing List
What about the getCustomTypeClassName function what is the replacement on DataType for that now?

Alexandre Dutra

unread,
Jan 28, 2016, 4:02:08 AM1/28/16
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi Jeremy,

Try this:

DataType dataType = ...;
if (dataType instanceof DataType.CustomType) {
    String customTypeClassName = ((DataType.CustomType) dataType).getCustomTypeClassName();
}

Hope that helps,

Alexandre


--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.
--
Alexandre Dutra
Driver & Tools Engineer @ DataStax

Jacques-Henri Berthemet

unread,
Jan 29, 2016, 6:11:16 PM1/29/16
to java-dri...@lists.datastax.com

You now have to use getJavaClass() from a CassandraSession instance;

 

Regards,

--

Jacques-Henri Berthemet

Reply all
Reply to author
Forward
0 new messages