Creating edge label schema

Creating edge label database schema.

Edge labels, as discussed in the data model, define the name of the edge label, incoming and outgoing vertex labels connected by an edge, and associated property keys for the edge. Property keys and vertex labels must be created prior to using them in edge label creation. Edge label schema can be created with create(), and property keys or connections can be added to existing schema with add(). The cardinality of an edge label is multiple cardinality by default, but single cardinality can be assigned. Edge labels can be created with a specific Time-To-Live (TTL) value, or prior existence of a edge label can be checked using ifNotExists().

A key component of an edge label is the connection() which identifies the two types of vertices that are connected with an edge using the edge label. The connection has directionality specified by the order, going from the outgoing vertex label listed first to the incoming vertex label listed second.

Prerequisites

Create property key schema.

Procedure

Single and multiple cardinality edge labels
  1. Define a multiple cardinality edge label:
    The name of the edgeLabel is ate, it is defined with multiple cardinality explicitly stated with multiple():
    schema.edgeLabel('ate').multiple().create()
    Since multiple cardinality is the default, multiple() can be omitted:
    schema.edgeLabel('ate').create()
    To check if an edge label already exists before creating it, use ifNotExists():
    schema.edgeLabel('knows').multiple().create().ifNotExists().create()
  2. Define a single cardinality edge label:
    The name of the edgeLabel is has, it is defined with single cardinality:
    schema.edgeLabel('has').single().create()
    Since single cardinality is not the default, single() must be included.
Associating property keys with edge labels
  1. Properties can be defined in either the create() or add()statement:
    schema.edgeLabel('ate').multiple().create()
    schema.edgeLabel('ate').properties('mealDate').add()
    or
    schema.edgeLabel('ate').multiple().properties('mealDate').create()
    Important: If cardinality (single or multiple) or properties() are specified, those options must precede the connection() option in the schema statement.
Connections
  1. Connections can be defined in either the create() or add()statement:
    schema.edgeLabel('ate').multiple().create()
    schema.edgeLabel('ate').connection('person', 'meal').add()
    or
    schema.edgeLabel('ate').multiple().connection('person', 'meal').create()
  2. Multiple connections between vertex labels can be created for the same edge label:
    schema.edgeLabel('includedIn').connection('recipe','meal').add()
    schema.edgeLabel('includedIn').connection('meal','book').add()
    schema.edgeLabel('includedIn').connection('recipe','book').add()
    These schema statements will allow edges to be inserted that use the edge label includedIn between recipes and meals, meals and books, and recipe and books.
Order restrictions
  1. If cardinality (single or multiple) or properties() are specified, those options must precede the connection() option in the schema statement.
    Multiple steps:
    schema.edgeLabel('includedIn').multiple().create()
    schema.edgeLabel('includedIn').properties('amount').add()
    schema.edgeLabel('includedIn').connection('recipe','meal').add()
    schema.edgeLabel('includedIn').connection('meal','book').add()
    schema.edgeLabel('includedIn').connection('recipe','book').add()
    schema.edgeLabel('includedIn').connection('ingredient','recipe').add()
    Single step:
    schema.edgeLabel('includedIn').multiple().properties('amount').connection('recipe','meal').connection('recipe','book').connection('ingredient','recipe').create()
Edge labels with Time-To-Live (TTL)
  1. Edge labels can be defined with a Time-To-Live (TTL) value; once the specified time is reached, the value is deleted from the graph. Define an edge label with TTL:
    The name of the edge label is createDate is defined with a TTL of 60 seconds with ttl():
    schema.edgeLabel('createDate').ttl(60).create()

Example

The edge labels used for the DSE QuickStart example used throughout the documentation:
// ********
// EDGE LABELS
// ********
// SYNTAX:
//schema.edgeLabel('edgeLabel').
//    [ single() | multiple() ].
//    [ connection( outVertex, inVertex) ].
//    [ ttl ].
//    [ properties(property[, property]) ].
//    [ ifNotExists() ].
//    [ create() | add() | describe() | exists() ]
// DEFAULT IS MULTIPLE CARDINALITY
// ********

schema.edgeLabel('ate').multiple().create()
schema.edgeLabel('ate').properties('mealDate').add()
schema.edgeLabel('ate').connection('person', 'meal').add()
schema.edgeLabel('knows').multiple().create()
schema.edgeLabel('knows').properties('since').add()
schema.edgeLabel('knows').connection('person','person').add()
schema.edgeLabel('includes').multiple().create()
schema.edgeLabel('includes').properties('numServ').add()
schema.edgeLabel('includes').connection('meal','meal_item').add()
schema.edgeLabel('includedIn').multiple().create()
schema.edgeLabel('includedIn').properties('amount').add()
schema.edgeLabel('includedIn').connection('recipe','meal').add()
schema.edgeLabel('includedIn').connection('meal','book').add()
schema.edgeLabel('includedIn').connection('recipe','book').add()
schema.edgeLabel('includedIn').connection('ingredient','recipe').add()
schema.edgeLabel('created').multiple().create()
schema.edgeLabel('created').properties('createDate').add()
schema.edgeLabel('created').connection('person', 'recipe').add()
schema.edgeLabel('reviewed').multiple().create()
schema.edgeLabel('reviewed').properties('time','year','stars','comment').add()
schema.edgeLabel('reviewed').connection('person','recipe').add()
schema.edgeLabel('authored').multiple().create()
schema.edgeLabel('authored').connection('person', 'book').add()
schema.edgeLabel('contains').multiple().ttl(60800).create()
schema.edgeLabel('contains').properties('expireDate').add()
schema.edgeLabel('contains').connection('fridgeSensor','ingredient').add()
schema.edgeLabel('isStockedWith').multiple().ttl(60800).create()
schema.edgeLabel('isStockedWith').properties('expireDate').add()
schema.edgeLabel('isStockedWith').connection('store','ingredient').add()
schema.edgeLabel('isLocatedAt').multiple().create()
schema.edgeLabel('isLocatedAt').connection('home','location').add()
schema.edgeLabel('isLocatedAt').connection('store','location').add()
schema.edgeLabel('isLocatedAt').connection('fridgeSensor','home').add()