简体   繁体   English

Cassandra Java驱动程序3 EnumNameCodec

[英]Cassandra Java driver 3 EnumNameCodec

I am migrating from Cassandra 2.1 to v3 and so using new Java Driver v3. 我正在从Cassandra 2.1迁移到v3,因此使用了新的Java Driver v3。 A change has been made about @Enumerated(EnumType.STRING) or ORDINAL which has been swapped by driver-extras module EnumOrdinalCodec and EnumNameCodec. 已对@Enumerated(EnumType.STRING)或ORDINAL进行了更改,这些更改已由驱动程序扩展模块EnumOrdinalCodec和EnumNameCodec交换。

In my project I have it: 在我的项目中,我有:

@Column(name = "myColumn")
    @Enumerated(EnumType.STRING)
    private myEnum         currentMyEnum         ;

I look up for some example about how to use them but did not really understand how they work. 我查找了一些有关如何使用它们的示例,但并没有真正理解它们的工作原理。 The main info I found was that example: 我发现的主要信息是该示例:

enum Foo {...}
enum Bar {...}

// register the appropriate codecs
CodecRegistry.DEFAULT_INSTANCE
    .register(new EnumOrdinalCodec<Foo>(Foo.class))
    .register(new EnumNameCodec<Bar>(Bar.class))

// the following mappings are handled out-of-the-box
@Table
public class MyPojo {
    private Foo foo;
    private List<Bar> bars;
    ...
}

Which is not clear to me. 我不清楚。 It looks they put enum, codecs and DAO model in the same file. 看起来他们将枚举,编解码器和DAO模型放在同一文件中。 Within my project the enum and DAO model are in file different and when I try to put the "CodecRegistry.DEFAULT_INSTANCE.register(new EnumOrdinalCodec(myEnum.class))" in etheir the enum or DAO file I get error from IDE Eclipse. 在我的项目中,枚举和DAO模型的文件不同,并且当我尝试在枚举或DAO文件中放置“ CodecRegistry.DEFAULT_INSTANCE.register(new EnumOrdinalCodec(myEnum.class))”时,我从IDE Eclipse中得到错误。

If some one could help me about how to chnage @Enumerated(EnumType.STRING) to use EnumNameCodec thanks in advance. 如果有人可以帮助我了解如何更改@Enumerated(EnumType.STRING)以使用EnumNameCodec,请先感谢。

It looks they put enum, codecs and DAO model in the same file 看起来他们将枚举,编解码器和DAO模型放在同一文件中

It's just a code example. 这只是一个代码示例。 In a real project those 3 code blocs are put it different files 在实际的项目中,这3个代码块放置在不同的文件中

When you create the Cluster object, you can register a codec registry (create one with new CodecRegistry ). 创建Cluster对象时,可以注册一个编解码器注册表(使用new CodecRegistry创建一个)。 This codec registry will be in charge of converting non-native types (like Java enums) into supported CQL Java types. 此编解码器注册表将负责将非本地类型(例如Java枚举)转换为受支持的CQL Java类型。

By registering new EnumOrdinalCodec<Foo>(Foo.class) for example, you can use the enum Foo in any Java bean (even inside collections like list<Foo>) and the object mapper will autodetect that there is a codec to convert the enum Foo into a CQL integer 例如,通过注册new EnumOrdinalCodec<Foo>(Foo.class) ,您可以在任何Java Bean中使用枚举Foo (即使在list <Foo>之类的内部集合中),对象映射器也会自动检测到是否存在一个可将枚举转换为枚举的编解码器Foo变成CQL整数

Example of code to register a custom codec (taken from the driver doc): 注册自定义编解码器的代码示例(取自驱动程序文档):

Cluster cluster = Cluster.builder()..... ;  //Create the cluster singleton somewhere

// Create the enum codec
EnumOrdinalCodec<Foo> myEnumCodec = new EnumOrdinalCodec<Foo>(Foo.class)

// Retrieve the codec registry from the cluster configuration
// IF you didn't configure any codec registry, a default empty codec registry
// will be returned
CodecRegistry myCodecRegistry = cluster.getConfiguration().getCodecRegistry();

// Register your codec here
myCodecRegistry.register(myEnumCodec);

In order to avoid registration of all your codecs in cluster object and localize their usage only in DAO models I would propose the following way (kotlin): 为了避免在集群对象中注册所有编解码器并仅在DAO模型中本地化它们的用法,我建议采用以下方式(kotlin):

class MyEnumCodec: EnumNameCodec<MyEnum>(MyEnum::class.java)

data class MyData(

    @ClusteringColumn(0)
    var id: String,

    @Column(name = "my_enum", codec = MyEnumCodec::class)
    var myEnum: MyEnum? = null
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM