Search code examples
cassandranosqlcqlsuper-columns

Representing a super column in Cassandra CQL


Currently, I have the following schema for one of my tables:

id
name
...
country
  - id
  - name
  - city
    - id
    - name

I was looking around the Cassandra documentation, and I cannot find any clear examples or demonstrations as to how I would represent my super columns in a column family. The code I have is as follows:

CREATE COLUMNFAMILY table (
    id varint,
    name varchar,
    <<regular columns omitted>>
    country ..?,
    PRIMARY KEY = (id)
);

Solution

  • You can create a user-defined type to attach multiple data fields to a column.

    For example, in your case

    country
      - id
      - name
      - city
        - id
        - name
    

    Can be represented in a UDT as

    CREATE TYPE mykeyspace.countryudt (
      id uuid,
      name text,
      city map<uuid, text>
    );
    

    Now the table definition will look like,

    CREATE COLUMNFAMILY table (
        id varint,
        name varchar,
        <<regular columns omitted>>
        country frozen <countryudt>,
        PRIMARY KEY = (id)
    );
    

    Additional reference for UDT here.