注意:列名已重命名以隐藏敏感信息
我正在尝试使用Spring中的CassandraRepository从Cassandra获取特定的列(基本上执行与select distinct foo from mytable
等效的操作),但是由于某种原因,ColumnReader期望未获取的列会出现错误。这是我的存储库的样子:
package com.my.data.repositories;
import com.my.data.schemas.Mydata;
import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
@Repository
public interface MytableRepository extends CassandraRepository<Mydata, String> {
@Query(value="SELECT * FROM mytable WHERE foo = ?0 AND bar= ?1")
Mydata getAllColumns(String foo, String bar);
@Query(value="SELECT DISTINCT foo FROM mytable")
ArrayList<Mydata> getOneColumn();
}
第一个查询按预期工作,第二个查询引发以下错误:
2018-12-04 20:36:53.774 ERROR 9 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Column does not exist in Cassandra table: bar] with root cause
java.lang.IllegalArgumentException: Column does not exist in Cassandra table: bar
at org.springframework.data.cassandra.core.convert.ColumnReader.getColumnIndex(ColumnReader.java:174) ~[spring-data-cassandra-2.0.8.RELEASE.jar!/:2.0.8.RELEASE]
at org.springframework.data.cassandra.core.convert.ColumnReader.get(ColumnReader.java:63) ~[spring-data-cassandra-2.0.8.RELEASE.jar!/:2.0.8.RELEASE]
at org.springframework.data.cassandra.core.convert.ColumnReader.get(ColumnReader.java:55) ~[spring-data-cassandra-2.0.8.RELEASE.jar!/:2.0.8.RELEASE]
Mydata模式如下:
package com.my.data.schemas;
import lombok.Getter;
import lombok.Setter;
import lombok.Builder;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.core.mapping.Table;
import java.util.ArrayList;
import static org.springframework.data.cassandra.core.cql.PrimaryKeyType.CLUSTERED;
import static org.springframework.data.cassandra.core.cql.PrimaryKeyType.PARTITIONED;
@Setter
@Getter
@Builder
@Table("mytable")
public class Mydata {
@PrimaryKeyColumn(name = "foo", ordinal = 0, type = PARTITIONED)
private final String foo;
@PrimaryKeyColumn(name = "bar", ordinal = 1, type = CLUSTERED)
private final String bar;
@Column("baz")
private final ArrayList<String> "baz";
}
相应的cassandra表是通过以下子句创建的:
CREATE TABLE IF NOT EXISTS mytable (
foo text,
bar text,
baz set<text>,
PRIMARY KEY((foo), bar)
);
我要完成的工作是利用getOneColumn()
方法来获取唯一的“ foo”条目列表,而无需获取我不需要的混乱列,而不必在Java中针对我很容易的内容进行过滤能够通过Cassandra本身进行过滤。相反,出于某种原因,CassandraRepository一直尝试处理bar
列,但我不知道为什么。我假设在架构中,我未提取的列将被简单地设置为null。不是吗?我该如何解决?