Cassandra gives you a way to define your own types to extend its data model. These user-defined types (UDTs) are easier to use than tuples since you can specify the values by name rather than position.
CREATE TYPE address (
street text,
city text,
state text,
zip_code int
);
A UDT is scoped by the keyspace in which it is defined.
DESCRIBE KEYSPACE ${keyspace.name};
Now that you have defined the address
type, you can use it in the user
table. Rather than simply adding a single address, you can use a map to store multiple addresses to which you can give names such as "home", "work", and so on. However, you immediately run into a problem.
ALTER TABLE user ADD addresses map<text, address>;
InvalidRequest: Error from server: code=2200 [Invalid query] message="Non-frozen UDTs are not allowed inside collections: map<text, address>"
It turns out that a user-defined data type is considered as collection, as its implementation is similar to a set
, list
, or map
. You've asked Cassandra to nest one collection inside another.