cassandracassandra-3.0cassandra-2.0spring-data-cassandracassandra-2.1

How do you nest a UDT inside another UDT in Cassandra?


I have created the following user defined types (UDT) in cassandra :

CREATE TYPE keyspace.location (
    latitude text,
    longitude text,
    accuracy text,
    address text
);

CREATE TYPE keyspace.person (
    name text,
    contact_no text,
    alternate_contact text
);

and I want to use these to create another UDT

CREATE TYPE keyspace.pinpoint(
    user <person>,
    location <location>
);

Solution

  • You can nest UDTs by simply specifying your UDT as the type within another UDT in this manner:

    CREATE TYPE keyspace.pinpoint (
        user person,
        location location
    );
    

    You don't enclose them in <> brackets because those are used for collections.

    As a side note, I personally wouldn't nest UDTs unless you have no other option. UDTs are not as flexible as native columns. Inserting or updating data in a nested UDT can get very complicated and hard to maintain.

    Whenever possible, try to use generic table definitions. For example instead of defining the type pinpoint, try to use a table with native column types and clustered rows. Cheers!