将 Cassandra BoundStatement 的 ResultSet 映射/转换/转换为使用对象映射 API 构建的 Java 类的最有效方法是什么? [英] 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?

查看:24
本文介绍了将 Cassandra BoundStatement 的 ResultSet 映射/转换/转换为使用对象映射 API 构建的 Java 类的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataStax Java 中是否有用于 Apache Cassandra 的内置方法将来自 BoundStatement 的 ResultSet 映射到使用对象映射 API 构建的域对象 Java 类?

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?

我是一个从 Mapper + Accessor 方法转向 BoundStatement 方法的新手,并希望继续使用使用对象映射 API 构建的域对象的 Java 类,因此我在移动时对我的 DAO 方法的实现进行了最少的更改到绑定语句.我希望以一种通用的方式来完成它,并避免迭代每个 ResultSet 行并执行一行.为每个域对象一个一个地获取.

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.

虽然使用 Mapper + Accessor 方法,但 Result.all() 做得很好.找不到任何类似于 BoundStatement 方法的内容.

While using the Mapper + Accessor approach, the Result.all() do that very well. Could not find anything similar to the BoundStatement approach.

谢谢

IPVP

推荐答案

您可以通过实例化来完成将 ResultSet 映射到 com.datastax.driver.mapping.Result对象的 Mapper,然后使用 Mapper.map.这是一个示例,取自 java 驱动程序测试,它从定期执行的查询中获取 ResultSet 并将其映射到 Result,然后遍历 Result 以访问每个映射的 Post:

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 映射/转换/转换为使用对象映射 API 构建的 Java 类的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆