Creating collection and tuple schema
DataStax introduced collection (set, list, map) and tuple schema as data types in DataStax Graph (DSG) as vertex and edge label properties. Unlike user-defined types (UDTs), they are not created separately, but are created in vertex labels or edge labels.
You can create nested collections and tuples, which provides flexibility in creating property storage of various types for vertex and edge labels.
Prerequisites
Create a graph and use either Gremlin console or DataStax Studio to access the graph. See the DSG QuickStart configuration.
Procedure
-
Create a set named
category
in the vertex labelbook
:schema.vertexLabel('book'). ifNotExists(). partitionBy('book_id', Int). property('name', Text). property('publish_year', Int). property('isbn', Text). property('category', setOf(Text)). create()
Sets must define the type of data in the set. In this case, the property
category
is defined as a set, a collection of unordered choices. The property will store multiple choices, such asFrench
,American
, andDesserts
. -
Create a list named
macro
in the vertex labelmeal_item
:schema.vertexLabel('meal_item'). ifNotExists(). partitionBy('item_id', Int). property('name', Text). property('serv_amt', Text). property('macro', listOf(Int)). property('calories', Int). create()
Lists must define the type of data contained in the list. In this case, the property
macro
is defined as a list, a collection of ordered choices. Themacro
property will store multiple choices,70
,20
, and10
. These choices define the fat, carbohydrate, and protein percentages and must be stored in a specific order. -
Create a map named
badge
in the vertex labelperson
:schema.vertexLabel('person'). ifNotExists(). partitionBy('person_id', Uuid). property('name', Text). property('gender', Text). property('nickname', setOf(Text)). property('cal_goal', Int). property('macro_goal', listOf(Int)). property('country', listOf(tupleOf(Text, Date, Date))). property('badge', mapOf(Text, Date)). create()
Maps must define the type of data contained in the map for both the key and the value. In this case, the property
badge
is defined as a map, a collection of key:value pairs where the key isText
and the value isDate
. Thebadge
property will store multiple choices, such as:-
gold, 2010-01-01
,silver, 2011-02-09
for review levels -
sous-chef, 2016-10-26
andchef, 2019-01-01
for chef levels These choices define the badge level and data achieved.
-
-
Create a tuple named
country
in the vertex labelperson
:schema.vertexLabel('person'). ifNotExists(). partitionBy('person_id', Uuid). property('name', Text). property('gender', Text). property('nickname', setOf(Text)). property('cal_goal', Int). property('macro_goal', listOf(Int)). property('country', listOf(tupleOf(Text, Date, Date))). property('badge', mapOf(Text, Date)). create()
Tuples must define the type of data contained in the tuple. In this case, the property
country
is defined as a list of tuples that represent the country, start date, and end date of residency. Because the propertycountry
is defined as a nest list of tuples; multiple choices of a person’s residency in each location can be stored.