java 无法使用Reactive Cassandra访问具有复合键的Cassandra表

bgtovc5b  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(158)

我正在使用Spring-Reactive和Reactive-Cassandra [spring-boot-starter-data-cassandra-reactive]来访问Cassandra上的表。我能够访问具有表示主键的单个列的简单表,例如:

user_name: partition_key
address: regular

但是当涉及到一个更复杂的表,如下面,我无法从 cassandra 获取任何信息:

user_name: partition_key
department_name: clustering
address: regular

以下是我在Spring reactive中编写@Configuration和@Repository的方法:
配置:

@AllArgsConstructor
@Table(value = "user_table")
public class UserConfig {
    @Getter
    @PrimaryKey
    private UserKey key;

    @Column("address")
    @Getter
    private String address;
}

主键类:

@Getter
@PrimaryKeyClass
@AllArgsConstructor
public class UserKey {
    @PrimaryKeyColumn(name = "user_name", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    private String userName;
    @PrimaryKeyColumn(name = "department_name", ordinal = 1, type = PrimaryKeyType.CLUSTERED)
    private String departmentName;
}

存储库:

@Repository
public interface UserRepository extends ReactiveCassandraRepository<UserConfig, UserKey> {
    Mono<UserConfig> findByKeyUserNameAndKeyDepartmentName(String userName, String departmentName);
}

我的Cassandra配置类如下:

@Configuration
@EnableReactiveCassandraRepositories
public class CassandraConfig extends AbstractReactiveCassandraConfiguration {
    @Value("${spring.cassandra.contact-points}")
    private String contactPoints;

    @Value("${spring.cassandra.local-datacenter}")
    private String localDatacenter;
    
    @Value("${spring.cassandra.port}")
    private int port;
    
    @Value("${spring.cassandra.keyspace-name}")
    private String keySpace;
    
    @Value("${spring.cassandra.username}")
    private String username;
    
    @Value("${spring.cassandra.password}")
    private String password;

    @Value("${spring.cassandra.schema-action}")
    private SchemaAction schemaAction;

    @Override
    protected String getContactPoints() {
        return contactPoints;
    }

    @Override
    protected int getPort() {
        return port;
    }

    @Override
    public SchemaAction getSchemaAction() {
        return schemaAction;
    }

    @Override
    protected String getKeyspaceName() {
        return keySpace;
    }

    @Bean
    @Override
    public CqlSessionFactoryBean cassandraSession() {
        CqlSessionFactoryBean cqlSessionFactoryBean = new CqlSessionFactoryBean();
        cqlSessionFactoryBean.setUsername(username);
        cqlSessionFactoryBean.setPassword(password);
        cqlSessionFactoryBean.setKeyspaceName(keySpace);
        cqlSessionFactoryBean.setLocalDatacenter(localDatacenter);
        cqlSessionFactoryBean.setPort(port);
        cqlSessionFactoryBean.setContactPoints(contactPoints);
        return cqlSessionFactoryBean;
    }
}

我所遵循的所有文档都做了与我上面所做的完全相同的事情。我在这里遗漏了什么?请帮助。

jgwigjjp

jgwigjjp1#

这对我很有效:
表类:

@Data
@AllArgsConstructor
@Table(value = "user_table")
public class UserConfig implements Serializable {
    @PrimaryKeyColumn(name = "user_name", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
    private String userName;
    @PrimaryKeyColumn(name = "department_name", ordinal = 1, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.ASCENDING)
    private String departmentName;
    @Column(value = "address")
    private String address;
}

存储库:

@Repository
public interface UserRepository extends ReactiveCassandraRepository<UserConfig, String> {

    @Query("SELECT * from user_table where user_name=?0")
    Flux<UserConfig> findByUserName(String userName);

    @Query("SELECT * from user_table where user_name=?0 and department_name=?1")
    Flux<UserConfig> findByUserNameAndDepartmentName(String userName, String departmentName);
}

这里的变化主要是关于如何使用@Query定义Repository接口方法。

相关问题