简体   繁体   中英

Cassandra Java driver 3 EnumNameCodec

I am migrating from Cassandra 2.1 to v3 and so using new 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.

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. 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.

If some one could help me about how to chnage @Enumerated(EnumType.STRING) to use EnumNameCodec thanks in advance.

It looks they put enum, codecs and DAO model in the same file

It's just a code example. In a real project those 3 code blocs are put it different files

When you create the Cluster object, you can register a codec registry (create one with new CodecRegistry ). This codec registry will be in charge of converting non-native types (like Java enums) into supported CQL Java types.

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

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):

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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