将Cassandra BoundStatement的ResultSet映射/转换/转换为使用Object-mapping API构建的Java classe的最有效方法是什么? [英] What is the most efficient way to map/transform/cast a Cassandra BoundStatement's ResultSet to a Java classe built using the Object-mapping API?
问题描述
DataStax Java中是否存在用于Apache Cassandra的内置方法,将来自BoundStatement的ResultSet映射到使用Object-mapping API构建的域对象Java类?
<我是一个从Mapper + Accessor方法转向BoundStatement方法的新手,并希望继续使用使用Object-mapping API构建的域对象的Java类,这样我在转到我的DAO方法的实现时做了很小的改动。 BoundStatement。我希望以通用方式执行此操作,并避免迭代每个ResultSet行并为每个域对象逐个执行row.get。
使用时Mapper + Accessor方法,Result.all()做得非常好。找不到类似于BoundStatement方法的任何内容。
谢谢
IPVP
您可以完成将 ResultSet
映射到 com.datastax.driver.mapping.Result
通过为您的对象实例化 Mapper
,然后使用 Mapper.map
。这是一个例子,取自 java驱动程序的测试从定期执行的查询中获取 ResultSet
并将其映射到结果< Post>
,然后迭代结果以访问每个映射的帖子:
MappingManager manager = new MappingManager(session);
Mapper< Post> m = manager.mapper(Post.class);
...
//使用仅检索某些字段的投影查询检索帖子
ResultSet rs = session.execute(select user_id,post_id,title from user_id = + u1.getUserId());
结果<发布> result = m.map(rs);
for(帖子:结果){
assertThat(post.getUserId())。isEqualTo(u1.getUserId());
assertThat(post.getPostId())。isNotNull();
assertThat(post.getTitle())。isNotNull();
assertThat(post.getDevice())。isNull();
assertThat(post.getTags())。isNull();
}
Is there a builtin way in DataStax Java for Apache Cassandra to map the ResultSet coming from a BoundStatement to the domain objects Java classes built with using the Object-mapping API?
I am a newbie moving from the Mapper + Accessor approach to BoundStatement approach and would like to continue using the domain objects' Java classes built with the Object-mapping API so I do minimal changes to the implementation of my DAO methods while moving to the BoundStatement. I am looking to do it in a generic way and avoid to iterate over each ResultSet row and do a row.get one by one for each domain object.
While using the Mapper + Accessor approach, the Result.all() do that very well. Could not find anything similar to the BoundStatement approach.
Thanks
IPVP
You can accomplish mapping a ResultSet
to a com.datastax.driver.mapping.Result
by instantiating a Mapper
for your object, and then using Mapper.map
. Here is an example, taken from the java driver's tests that takes a ResultSet
from a regularly executed query and maps it to a Result<Post>
, and then iterates over the Result to access each mapped 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();
}
这篇关于将Cassandra BoundStatement的ResultSet映射/转换/转换为使用Object-mapping API构建的Java classe的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!