What is the most efficient way to map / convert / translate a Cassandra BoundStatement ResultSet to a Java class built using the object mapping API?

Is there a built-in way in Java's DataStax for Apache Cassandra to map the ResultSet coming from the BoundStatement to objects in Java domain objects built using the object map API?

I'm a newbie moving from the Mapper + Accessor approach to the BoundStatement approach and would like to continue using the Java classes of domain objects built using the object mapping APIs, so I make minimal changes to the implementation of my DAO methods while moving to BoundStatement. I want to do this in a general way and avoid repeating each row of the ResultSet and doing row.get one by one for each domain object.

Using the Mapper + Accessor approach, Result.all () does it very well. Could not find anything similar to the BoundStatement approach.

thanks

IPVP

+4
source share
1 answer

ResultSet com.datastax.driver.mapping.Result Mapper , Mapper.map. , java driver, ResultSet Result<Post>, :

MappingManager manager = new MappingManager(session);

Mapper<Post> m = manager.mapper(Post.class);
...
// Retrieve posts with a projection query that only retrieves some of the fields
ResultSet rs = session.execute("select user_id, post_id, title from posts where user_id = " + u1.getUserId());

Result<Post> result = m.map(rs);
for (Post post : result) {
    assertThat(post.getUserId()).isEqualTo(u1.getUserId());
    assertThat(post.getPostId()).isNotNull();
    assertThat(post.getTitle()).isNotNull();

    assertThat(post.getDevice()).isNull();
    assertThat(post.getTags()).isNull();
}
+7

Source: https://habr.com/ru/post/1623621/


All Articles