Search code examples
cassandradatastax-java-driver

Query Build using DataStax Driver


I have my table as below,

create table contact( id uuid primary key, 
                      personName text, 
                      updatedTime timestamp
                    );

and trying to execute the below prepare statement,

 String query = "SELECT * FROM CONTACT WHERE personName IN (:personNameList) " +
                "AND updatedTime > ':startTime' AND  updatedTime < :endTime ALLOW FILTERING;";


        SimpleStatement simpleStatement = SimpleStatement.builder(query)
                                            .setConsistencyLevel(DefaultConsistencyLevel.QUORUM)
                                            .build();

        PreparedStatement preparedStatement = cqlSession.prepare(simpleStatement);
        BoundStatement boundStatement = preparedStatement.bind();

        personList = ["John","Alex"];

        boundStatement.setString("startTime", "2020-08-16 14:44:32+0000");  // Issue with setting
        boundStatement.setString("endTime", "2020-08-16 14:60:32+0000");  // Issue with setting
        boundStatement.setList("personNameList", personList, String.class); //  Codec not found for requested operation: [TEXT <-> java.util.List<java.lang.String>]

        ResultSet execute = cqlSession.execute(boundStatement);


        // List<Person> personList = // Mapping

from 4.7.2 of driver mapping, it is different type to mapping as per my understanding, I couldn't get answer from Google. Any suggestions?


 <dependency>
            <groupId>com.datastax.oss</groupId>
            <artifactId>java-driver-mapper-processor</artifactId>
            <version>4.7.2</version>
            <scope>test</scope>
        </dependency>


Solution

  • Object mapper has changed significantly in the Java driver 4.x, so it requires setting up of the compile-time processor to generate auxiliary classes that are necessary for it work. But you can still use it to convert Row object into your POJO, by declaring the method with GetEntry annotation in your DAO interface, like this:

    @Dao
    public interface PersonDao {
      @GetEntity
      Person asPerson(Row row);
    }
    

    But if you're going to use object Mapper, I would recommend to use it for everything - in your case, you can declare a method with the Query annotation, and pass parameters for bindings.