In cassandra DB I am planning to store an array-of-object. What is the best way to do that. object mapping to Data mapping with model class in java.
Class Test{
@Column(name = "id")
int id,
@Column(name = "name")
String name,
Address[] address
class Address{
String add1,
String city,
String state
}
}
Should I put all(id, name, add1, city, state) in one table by adding columns to same keyspace with add1, city, state also? or add new table for address Or any other options..
I have tried to add TYPE
But throwing error as: "Error from server: code=2200 [Invalid query] message="A user type cannot contain non-frozen UDTs
"
From the error and type syntax I have used keyword 'frozen
', but not luck. Altering table also gives similar Error something like : "mismatched input 'frozen' expecting EOF
"
Also,
What if I have to save column of type 'String[ ]'
As it is not custom type like Address[]. it is of String or text.? Do we need to just add alter statement? if so how it looks like
For your case, first, you need to create a UDT(user defined type) in Cassandra. Create TYPE address( add1 text, city text, state text ); Then create a table including this UDT. Create table Test( id int, name text, address list<frozen<address>>, primary key(id) );
One of the popular features of MongoDB is the ability to store arbitrarily nested objects and be able to index on any nested field. In this post I will show how to store nested objects in Cassandra using composite columns.
1. SET: A Set is a collection data where we can store a group of elements such that a user can have multiple Email address then to stored such type of data we used set collection data type which returns sorted elements when querying.
For your case, first, you need to create a UDT(user defined type) in Cassandra.
Create TYPE address(
add1 text,
city text,
state text
);
Then create a table including this UDT.
Create table Test(
id int,
name text,
address list<frozen<address>>,
primary key(id)
);
If you want to know more about UTD and the usages, visit following links:
EDIT:
Also, What if I have to save column of type 'String[ ]' As it is not custom type like Address[]. it is of String or text.? Do we need to just add alter statement? if so how it looks like
Answer: Alter table test add stringarr list<text>
Check this links to get more idea about cassandra data types: CQL data types
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