Creating user-defined type (UDT) schema

User-defined types (UDTs) are a method of defining non-standard data types for use in both Cassandra and DataStax Graph (DSG). UDTs can be created with either CQL or Gremlin for use in vertex or edge label schema for graphs. UDTs can be creatd with all data types, as well as other UDTs. They are a useful way to construct groupings of properties that are associated. UDT schema have three operations:

  • create a UDT with create()

  • describe a UDT with describe()

  • drop a UDT with drop()

Prior existence of a UDT can be checked using ifNotExists() before creating a UDT.

Prerequisites

Create a graph and use either Gremlin console or DataStax Studio to access the graph. See the DSG QuickStart configuration if you need refreshing.

Procedure

  1. Create a UDT with several properties:

    schema.type('address').
      ifNotExists().
      property('address1', Text).
      property('address2', Text).
      property('city_code', Text).
      property('state_code', Text).
      property('zip_code', Text).
      create()
  2. Create a UDT with a property that uses another UDT:

    schema.type('location_details').
      ifNotExists().
      property('loc_address', frozen(typeOf('address'))).
      property('telephone', listOf(Text)).
      create()

    A new UDT is created that combines an address, a previously created UDT, with a list of telephone numbers. This UDT can be used for location information associated with homes or stores.

Example

The UDTs used for the DataStax Graph QuickStart example used throughout the documentation:

// Create user-defined types (UDTs) with Gremlin
// VERTEX LABELS
// ********
// SYNTAX:
// schema.type('typename')
//    [ .ifNotExists() ]
//    [ .property(property, propertyType) ]
//    [ .create() | .describe() ]

// USER-DEFINED TYPE
// tag::createUDT_address[]
schema.type('address').
  ifNotExists().
  property('address1', Text).
  property('address2', Text).
  property('city_code', Text).
  property('state_code', Text).
  property('zip_code', Text).
  create()
// end::createUDT_address[]

// tag::createUDT_fullname[]
schema.type('fullname').
  ifNotExists().
  property('firstname', Text).
  property('lastname', Text).
  create()
// end::createUDT_fullname[]

//Using a nested user defined type via typeOf:
// tag::createUDT_locDet[]
schema.type('location_details').
  ifNotExists().
  property('loc_address', frozen(typeOf('address'))).
  property('telephone', listOf(Text)).
  create()
// end::createUDT_locDet[]

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