下面是我们的实体类
@Entity(defaultKeyspace = CASSANDRA_KEYSPACE)
@CqlName(CASSANDRA_TABLE)
public static class Scientist implements Serializable {
@CqlName("person_name")
public String name;
@Computed("writetime(person_name)")
@CqlName("name_ts")
public Long nameTs;
@CqlName("person_id")
@PartitionKey
public Integer id;
public Scientist() {}
public Scientist(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return id + ":" + name;
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Scientist scientist = (Scientist) o;
return id.equals(scientist.id) && Objects.equal(name, scientist.name);
}
@Override
public int hashCode() {
return Objects.hashCode(name, id);
}
}
@Dao
public interface ScientistDao {
@GetEntity
MappedAsyncPagingIterable<Scientist> map(AsyncResultSet resultSet);
@Delete
CompletionStage<Void> deleteAsync(Scientist entity);
@Insert
CompletionStage<Void> saveAsync(Scientist entity);
}所面临的问题是,如果没有选择计算字段(在上面的例子中是writetime(person_name) )作为查询的一部分,映射就会失败。
在3.x驱动程序中: ResultSet中不存在的映射字段被忽略。link
在4.x驱动程序中:对于每个实体字段,数据库表或UDT必须包含具有相应名称的列。link
请建议一种可能的解决方案/解决方法,其中此计算字段可以根据需要作为查询的一部分,并且映射可以在不引发IllegalArgumentException的情况下成功进行。
编辑:
科学家表模式
CREATE TABLE beam_ks.scientist (person_id int PRIMARY KEY,person_name text);以下是已尝试的查询:
select person_id,writetime(person_name) as name_ts from beam_ks.scientist where person_id=10使用@GetEntity映射结果集失败,错误如下:
Caused by: java.lang.IllegalArgumentException: person_name is not a column in this row
at com.datastax.oss.driver.internal.core.cql.DefaultRow.firstIndexOf(DefaultRow.java:110)
at com.datastax.oss.driver.api.core.data.GettableByName.get(GettableByName.java:144)
at org.apache.beam.sdk.io.cassandra.CassandraIOTest_ScientistHelper__MapperGenerated.get(CassandraIOTest_ScientistHelper__MapperGenerated.java:89)CassandraIOTest_ScientistHelper__MapperGenerated:中的get方法
@Override
public CassandraIOTest.Scientist get(GettableByName source) {
CassandraIOTest.Scientist returnValue = new CassandraIOTest.Scientist();
Integer propertyValue = source.get("person_id", Integer.class);
returnValue.setId(propertyValue);
String propertyValue1 = source.get("person_name", String.class);
returnValue.setName(propertyValue1);
return returnValue;
}而且,documentation没有指定是否为计算值添加getter和setter方法。因此,它们将从实体类中删除
发布于 2021-04-19 19:39:06
在使用@GetEntity方法时,您有责任提供一个与实体定义100%兼容的结果集对象。
这里的Scientist实体包含两个常规字段:person_id (整数)和person_name (文本)。因此,您的结果集必须(至少)包含具有这些名称和类型的两列。
但是您说您提供了以下查询:select person_id,writetime(person_name) as name_ts from beam_ks.scientist where person_id=10。
此查询不包含所需的列。您应该将您的查询更改为以下查询或类似的查询:
select person_id, person_name from beam_ks.scientist where person_id=10请注意,@GetEntity方法不识别计算值,只能识别常规值。没有必要包含writetime(person_name) as name_ts,它无论如何都不会被映射。
https://stackoverflow.com/questions/66985742
复制相似问题