Enum data type in Cassandra
Asked Answered
H

4

14

I am trying to migrate my database from MySQL to Cassasndra. The problem I am facing is with one of the column type defined as Enum (enum('GP','NGP','PGP','PAGP')). Cassandra does not support Enum data types (it supports collections though). Is there a way to implement Enum data type in Cassandra, so that the value of a column should be restricted from a set of values? I am using Apache Cassandra version 2.0.7.

Highminded answered 8/5, 2014 at 7:15 Comment(1)
See also Datastax java driver 3.0.0 Enumerated annotation not foundEchelon
K
13

See datastax cassandra Object-mapping API,

http://docs.datastax.com/en/developer/java-driver/2.1/java-driver/reference/crudOperations.html

enum Gender { FEMALE, MALE };

// FEMALE will be persisted as 'FEMALE'
@Enumerated(EnumType.STRING)
private Gender gender;

// FEMALE will be persisted as 0, MALE as 1
@Enumerated(EnumType.ORDINAL)
private Gender gender

for cassandra 3.0

enum State {INIT, RUNNING, STOPPING, STOPPED}

cluster.getConfiguration().getCodecRegistry()
        .register(new EnumNameCodec<State>(State.class));

// schema: create table name_example(id int PRIMARY KEY, state text)
session.execute("insert into name_example (id, state) values (1, ?)", State.INIT);
// state is saved as 'INIT'

http://docs.datastax.com/en/developer/java-driver/3.1/manual/custom_codecs/extras/

Karrikarrie answered 3/7, 2015 at 8:30 Comment(0)
C
5

As far I know and after reading the documentation about cql types, you can not use directly enum in cql statements (I check this for the java clients).

So the option you have is convert the Enum to String to include the field in a cql statement. BY this way all your application use the Enum but in the backend layer use the string representation for the enum.

Cherlynchernow answered 8/5, 2014 at 8:1 Comment(4)
Yes in the documentation there is no data type like Enum. I think I need to replicate the behavior. But what it the option if I plan to move my current DB from MySQL to Cassandra?Highminded
Options? What do you mean?Cherlynchernow
I meant currently I am using MySQL which supports Enum. But based on my certain business factors, I might migrate to Cassandra. In that case how can I support that Enum type column in the Cassandra. It may be the case that I am not interacting with Cassandra via a client, may interacting directly using CQLSH. In that case how to use that Enum type column?Highminded
You can not use Enum to interact with Cassandra, you have to convert the Enum to String before executing the cql.Cherlynchernow
K
3

I was facing the same issue with an integer enum... here's what I did:

MappingConfiguration.Global.Define(
    new[] { 
       new Map<Login>()
            .TableName("logins")
            .PartitionKey(el => el.UserId)
            .Column(el => el.UserId, cm => cm.WithName("user_id")),
            .Column(el => el.Gender, cm => cm.WithName("gender_id").WithDbType<int>()),
    });

Using C# driver 2.5 and DSE 4.7.

Kassandrakassaraba answered 7/6, 2015 at 0:25 Comment(0)
H
2

there is more or less native support of enums in cassandra

http://docs.datastax.com/en/developer/java-driver/2.1/java-driver/reference/crudOperations.html

As far as I know you can write your own custom serializers etc for cassandra and it will be able to understand your specific enum. But the those jars should be places in cassandra folder.

You can also store it as String or ordinal int value

Houselights answered 2/7, 2015 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.