How to Query Cassandra using CassandraRepository w

2019-09-18 15:58发布

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);

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)

However, when I run the equivalent CQL command in CQLSHell I get the exact row I was looking for...

Why is this happening ?

1条回答
Melony?
2楼-- · 2019-09-18 16:35

There was a mistake in writing the controller for the SpringBoot app.

The proper controller is thus:

@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;
    }

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.

查看更多
登录 后发表回答