gpt4 book ai didi

java - Cassandra CodecNotFoundException : Codec not found for requested operation

转载 作者:行者123 更新时间:2023-11-29 04:22:54 25 4
gpt4 key购买 nike

我有一个枚举字段,我想映射到 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。这是模型类的片段

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 创建表

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

终于有了我的映射代码

    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 如下所示。希望对您有所帮助。

枚举名称解码器

A codec that serializes Enum instances as CQL varchars representing their programmatic names as returned by Enum.name().

枚举顺序编解码器

A codec that serializes Enum instances as CQL ints representing their ordinal values as returned by Enum.ordinal().

链接到 Javadoc

关于java - Cassandra CodecNotFoundException : Codec not found for requested operation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47996457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com