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 label book:

    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 as French, American, and Desserts.

  • Create a list named macro in the vertex label meal_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. The macro property will store multiple choices, 70, 20, and 10. 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 label person:

    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 is Text and the value is Date. The badge property will store multiple choices, such as:

    • gold, 2010-01-01, silver, 2011-02-09 for review levels

    • sous-chef, 2016-10-26 and chef, 2019-01-01 for chef levels These choices define the badge level and data achieved.

  • Create a tuple named country in the vertex label person:

    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 property country is defined as a nest list of tuples; multiple choices of a person’s residency in each location can be stored.

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