Creating user-defined type (UDT) schema

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
// START-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

// START-createUDT_fullname
schema.type('fullname').
  ifNotExists().
  property('firstname', Text).
  property('lastname', Text).
  create()
// END-createUDT_fullname

//Using a nested user defined type via typeOf:
// START-createUDT_locDet
schema.type('location_details').
  ifNotExists().
  property('loc_address', frozen(typeOf('address'))).
  property('telephone', listOf(Text)).
  create()
// END-createUDT_locDet