Create a user-defined type (UDT)

User-defined types (UDTs) are stored per keyspace. UDTs can store multiple data fields, each named and typed, to a single column. The fields used to create a UDT may be any valid data type, including collections and other existing UDTs. After a UDT is created, it can be used to define a table column. It can be used in multiple tables, if required. You can access a UDT from another keyspace by using the <keyspace_name>.<type_name> notation.

Prerequisites

Create a user-defined type named basic_info.

CREATE TYPE IF NOT EXISTS cycling.basic_info ( 
  birthday timestamp, 
  nationality text, 
  height text,
  weight text
);
DESCRIBE TYPE cycling.basic_info;
Results
WARNING: cqlsh was built against 5.0-beta1, but this server is 5.0.  All features may not work!

CREATE TYPE cycling.basic_info (
    birthday timestamp,
    nationality text,
    height text,
    weight text
);

Create a table for storing cyclist data in columns of type basic_info. Use the frozen keyword in the definition of the user-defined type column.

The frozen keyword is not required for UDTs that contain only non-collection fields.

When using the frozen keyword, you cannot update parts of a user-defined type value. The entire value must be overwritten. The database treats the value of a frozen, user-defined type like a blob.

CREATE TABLE IF NOT EXISTS cycling.cyclist_stats ( 
  id UUID PRIMARY KEY,
  lastname text,
  basics basic_info
);

A user-defined type can be nested in another column type. The example below nests a UDT in a frozen list named races.

CREATE TYPE IF NOT EXISTS cycling.race (
  race_title text,
  race_date timestamp,
  race_time text
);
DESCRIBE TYPE cycling.race;
Results
WARNING: cqlsh was built against 5.0-beta1, but this server is 5.0.  All features may not work!

CREATE TYPE cycling.race (
    race_title text,
    race_date timestamp,
    race_time text
);
CREATE TABLE IF NOT EXISTS cycling.cyclist_races (
  id UUID PRIMARY KEY,
  lastname text,
  firstname text,
  races list<FROZEN <race>>
);
Results
WARNING: cqlsh was built against 5.0-beta1, but this server is 5.0.  All features may not work!

CREATE TABLE cycling.cyclist_races (
    id uuid PRIMARY KEY,
    firstname text,
    lastname text,
    races list<frozen<race>>
) WITH additional_write_policy = '99p'
    AND allow_auto_snapshot = true
    AND bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND cdc = false
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.UnifiedCompactionStrategy', 'max_sstables_to_compact': '64', 'min_sstable_size': '100MiB', 'scaling_parameters': 'T4', 'sstable_growth': '0.3333333333333333', 'target_sstable_size': '1GiB'}
    AND compression = {'chunk_length_in_kb': '16', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND memtable = 'default'
    AND crc_check_chance = 1.0
    AND default_time_to_live = 0
    AND extensions = {}
    AND gc_grace_seconds = 864000
    AND incremental_backups = true
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair = 'BLOCKING'
    AND speculative_retry = '99p';

See also:

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com