如何使用 CassandraRepository 和 Spring Data Cassandra 和 Spring Boot 查询 Cassandra? [英] How to Query Cassandra using CassandraRepository with Spring Data Cassandra and Spring Boot?
问题描述
我的 Cassandra 集群中有一个使用以下命令构建的表:
I have a Table in my Cassandra Cluster built using these commands:
CREATE KEYSPACE IF NOT EXISTS activitylogs WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
CREATE TABLE IF NOT EXISTS activitylogs.activities2 (
activity_id timeuuid,
actor_id text,
app_id text,
item_id text,
viewer_id text,
activity_type int,
ts timestamp,
PRIMARY KEY (actor_id, activity_id, app_id)
) WITH CLUSTERING ORDER BY (activity_id DESC, app_id ASC);
这是我在 Spring 项目中的存储库的样子:
This is what my Repository in Spring Project looks like:
public interface ActivityRepository extends CassandraRepository<Activity> {
@Query("SELECT actor_id FROM activities2 WHERE actor_id='actorA'")
Iterable<Activity> findByActor_Id(String actor_Id);
}
现在,当我访问与此查询检索器关联的端点时,出现以下错误:
Now, when I access the endpoint associated with this query retriever , I get the following error:
Invalid null value in condition for column actor_id
at com.datastax.driver.core.Responses$Error.asException(Responses.java:136)
但是,当我在 CQLSHell 中运行等效的 CQL 命令时,我得到了我正在寻找的确切行...
However, when I run the equivalent CQL command in CQLSHell I get the exact row I was looking for...
为什么会这样?
推荐答案
为 SpringBoot 应用编写控制器时出现错误.
There was a mistake in writing the controller for the SpringBoot app.
因此合适的控制器是:
@RequestMapping(value = "/activity/{actor_id}",method = RequestMethod.GET)
@ResponseBody
public List<Activity> activity1(**@PathVariable("actor_id")** String actor_id) {
List<Activity> activities = new ArrayList<>();
activityRepository.findByActor_Id(actor_id).forEach(e->activities.add(e));
return activities;
}
较早的实现(在编写问题时)没有 @PathVariable 注释,因此,控制器没有将任何值传递给存储库接口函数.
Earlier implementation (at the time of writing the question) did not have @PathVariable annotation hence, controller wasn't passing any value to the repository interface function.
这篇关于如何使用 CassandraRepository 和 Spring Data Cassandra 和 Spring Boot 查询 Cassandra?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!