6

I'm following the Cassandra java object mapper tutorial on Datastax website.

and while defining accessors

@Query("SELECT * FROM testks.user ")
Result<User> getAll();

running this query give me a

com.datastax.driver.core.exceptions.InvalidQueryException: Some partition key parts are missing: id

Looking around it seems that you cannot query in cassandra without providing a partition key. Is that the case? This seems like a strange requirement. If I want a select all query, how would I go about doing that?

the table is defined as

CREATE TABLE testks.user (
id text PRIMARY KEY,
name text

)

Pita
  • 1,444
  • 1
  • 19
  • 29

1 Answers1

0

You didn't provide many details. if you follow these below steps you shouldn't get any error

You should define the user model like below

User.java

@Table(name = "user")
public class User {

    @PartitionKey
    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" + "id=" + id + ", name=" + name + '}';
    }
}

And Repository like below

UserAccessor.java

@Accessor
public interface UserAccessor {

    @Query("SELECT * FROM user")
    Result<User> getAll();
}

Here is how you can use the repository

Main.Java

public static void main(String[] args) {
    try (Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withCredentials("cassandra", "cassandra").build(); Session session = cluster.connect("test")) {
        MappingManager manager = new MappingManager(session);

        UserAccessor userAccessor = manager.createAccessor(UserAccessor.class);
        System.out.println(userAccessor.getAll().all());
    }
}
Ashraful Islam
  • 12,470
  • 3
  • 32
  • 53