【问题标题】:Cassandra mapper not handling CQLType TupleCassandra 映射器不处理 CQLType 元组
【发布时间】:2017-01-17 07:55:22
【问题描述】:

我正在使用 datastax cassandra 3.1.0。我在 cassandra 中创建了下表并插入了一条记录。

CREATE TABLE collect_things (   k int PRIMARY KEY,   v frozen <tuple <int, text, double>> );
INSERT INTO collect_things (k, v) VALUES(0, (3, 'bar', 2.1));
select * from collect_things ;
k | v
---+-----------------
 0 | (3, 'bar', 2.1)

当我尝试使用带有以下代码的 Cassandra Mapper 在 Java 中获取上述行时:

this.cluster = Cluster.builder().addContactPoint(node).withPort(port).build();
session = cluster.connect();
MappingManager manager = new MappingManager(session);
Mapper<CollectThings> mapper = manager.mapper(CollectThings.class);
Integer  k = 0;
mapper.get(k);

我收到以下异常:

Exception in thread "main" com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [frozen<tuple<int, varchar, double>> <-> com.datastax.driver.core.TupleType]
    at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:56)
    at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:25)
    at com.datastax.driver.mapping.DriverThrowables.propagateCause(DriverThrowables.java:41)
    at com.datastax.driver.mapping.Mapper.get(Mapper.java:432)

我为上表准备了以下 java bean。 CollectThings.java

import com.datastax.driver.core.TupleType;
import com.datastax.driver.mapping.annotations.Frozen;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;

@Table(keyspace = "user_info", name = "collect_things")
public class CollectThings {

    private Integer k;
    private TupleType v;

    public CollectThings() {

    }

    @PartitionKey
    public Integer getK() {
        return k;
    }

    public void setK(Integer k) {
        this.k = k;
    }

    @Frozen
    public TupleType getV() {
        return v;
    }

    public void setV(TupleType v) {
        this.v = v;
    }
}

有人可以在这里告诉如何处理元组类型吗?开箱即用的映射器不支持 cql 元组类型吗?

【问题讨论】:

    标签: datastax-java-driver cassandra-3.0


    【解决方案1】:

    您应该使用TupleValue 而不是TupleTypeTupleType 表示特定元组的类型信息,其中TupleValue 表示实际值。使用您的示例,我能够通过将 TupleType 的所有引用更改为 TupleValue 来检索数据。

    【讨论】:

    • 那行得通。我曾考虑使用 TupleValue,但认为 TupleValue 将无法处理所有值,并且只表示其中一个值,它具有 get() 方法和索引。非常感谢!!
    • 请再问一个问题,从 db 中获取数据时上述方法很好,但是当我想使用 mapper 持久化数据时呢?我将如何创建 TupleValue 的新实例。我的代码是这样的: session.execute(mapper.saveQuery(bean));但是要创建 bean,我没有办法设置 TupleValue v.
    • 创建一个TupleValue,需要有代表性的TupleType。你可以从 Cluster.getMetadata().newTupleType(DataType.x ...); 中得到它。然后使用 TupleType 使用 newTupleValue 从该 TupleType 创建一个 TupleValue。
    • 能否请您检查我的其他问题 - stackoverflow.com/questions/42144862/…
    猜你喜欢
    • 1970-01-01
    • 2012-12-02
    • 2016-10-02
    • 2017-06-27
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多