Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum data type in Cassandra

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.

like image 434
Nayan Avatar asked May 08 '14 07:05

Nayan


People also ask

What is enum data type in database?

Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in a number of programming languages. An example of an enum type might be the days of the week, or a set of status values for a piece of data.

Which type of data can be stored in Cassandra?

Cassandra can store data in sets of key-value pairs using the Map data type. It allows you to store data and assign labels (key names) to it for easier sorting. Sets. You can store multiple unique values, using the Set data type.

What is enum with example?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

What is type enum in SQL?

An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.


2 Answers

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/

like image 114
ahll Avatar answered Sep 19 '22 08:09

ahll


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.

like image 27
ftrujillo Avatar answered Sep 22 '22 08:09

ftrujillo