本文介绍了Cassandra CodecNotFoundException:找不到请求的操作的编解码器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举字段,我想映射到cassandra中的列

I have an enum field that i want to map to a column in cassandra

public enum MyEnum {
    Aa,
    Bb,
    Cc,
}

我是怎么注册的

CodecRegistry myCodecRegistry = new CodecRegistry();
             myCodecRegistry.register(new EnumOrdinalCodec<MyEnum>(MyEnum.class));

            cluster = Cluster.builder().addContactPoint(cassandraHosts).withPort(cassandraPort).withCodecRegistry(myCodecRegistry).build();

我能够连接到cassandra很好。这是模型类的摘要

i am able to connect to cassandra just fine. Here is a snippet of the model class

public class MyModel
{
    @PartitionKey
    @Column(name="id")
    private String id;

    @ClusteringColumn
    @Column(name="name")
    private String name = "";

    @Column(name="enum")
    private MyEnum enum;

    public MyModel(String i, String n)
    {
        id=i;
        name=n
        enum=MyEnum.Aa;
    }
}

这是我使用CQL创建表的方式

and here is how i created the table using CQL

CREATE TABLE IF NOT EXISTS
tab
    (
        name varchar ,
        id varchar,
        enum varchar,
        PRIMARY KEY ((id), name));

最后,我有我的映射代码

finally, i have my mapping code

    manager = new MappingManager(session);
    mapper = manager.mapper(MyModel.class);
    accessor = manager.createAccessor(MyAccessor.class);

这是 MyAccessor

@Accessor
public interface MyAccessor{

    @Query("SELECT * FROM case WHERE id=? and name=? ALLOW FILTERING;")
    MyModel readByIdAndName(String id,String name);
}

所以当我尝试执行时

MyModel m = new MyModel("asdasf","qw");
mapper.save(m);

我得到

com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [varchar <-> my.pack.MyEnum]
    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.save(Mapper.java:289)

我在做什么错了?

推荐答案

EnumOrdinalCodec仅用于在int<-> Enum之间进行映射。由于您的Enum是基于字符串的,因此请改用 EnumNameCodec 类。这两个编解码器类的Javadoc如下。希望对您有所帮助。

EnumOrdinalCodec is used to map between int <-> Enum only. As your Enum is based on string, so please use EnumNameCodec class instead. Javadoc for these two codec classes are as per below. Hope it helps.

EnumNameCodec

EnumOrdinalCodec

链接到

这篇关于Cassandra CodecNotFoundException:找不到请求的操作的编解码器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:54