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.
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.
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.
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.
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.
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With