Creating edge label 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, direction of the edge and associated property keys for the edge.
Vertex labels must be created prior to edge label creation, since edge label schema uses vertex labels.
The cardinality of an edge label is single cardinality by default, but multiple cardinality can be defined using clusterBy
to provide multiple rows in the CQL table that have the same partition key.
Edge label schema have four operations:
-
create an edge label with
create()
-
describe an edge label with
describe()
-
drop an edge label with
drop()
-
add or drop properties to existing edge labels with
addProperty()
ordropProperty()
Prior existence of an edge label can be checked using ifNotExists()
before creating an edge label.
An edge label can be created from an existing CQL table using fromExistingTable('tableName')
.
A tableName
that is different from the edge label can be also defined.
DataStax Graph uses a particular format for edge ids when edges are inserted.
DataStax Graph limits the number of vertex and edge labels to 200 per graph. |
Procedure
-
Simple edges
-
Create an edge label
authored
betweenperson
andbook
:schema.edgeLabel('authored'). ifNotExists(). from('person').to('book'). create()
This edge label is created if it doesn’t already exist, and has no edge properties.
-
Create an edge label
knows
betweenperson
andperson
with an edge propertysince
:schema.edgeLabel('knows'). ifNotExists(). from('person').to('person'). property('since', Date). create()
This edge label is created if it doesn’t already exist. Like in vertex label schema, the property must have a data type assigned.
-
Create an edge label
ate
with atableName
schema.edgeLabel('ate'). tableName('person_eating'). ifNotExists(). from('person').to('meal'). property('meal_date', Date). create()
The CQL table will be named
person_eating
, while the edge label isate
.An edge propertymeal_date
will be included in the edge label definition. -
Convert a CQL table into an edge label:
schema.edgeLabel('authored'). fromExistingTable('authored_table'). from('person'). mappingProperty('person_id'). mappingProperty('person_name'). to('book'). mappingProperty('book_id'). mappingProperty('book_name'). mappingProperty('isbn'). create()
This example additionally uses
mappingProperty
to designate the column names to use from the CQL table when creating the edge label. Note that while the column names are not required to match, the data type and the number of mapping properties must match the data type and the number of primary key columns on the vertex label in order. For example the vertex label propertyname
will map to the CQL table columnperson_name
-
Convert a CQL table into an edge label, specifying the partition and clustering keys:
schema.edgeLabel('authored'). from('person').to('book'). partitionBy(OUT, 'id', 'person_id'). partitionBy(OUT, 'name', 'person_name'). clusterBy(IN, 'id', 'book_id'). clusterBy(IN, 'name', 'book_name'). clusterBy(IN, 'isbn', 'isbn'). clusterBy(IN, 'year', 'publish_year', Desc). create()
The pattern for specifying the mapping columns is
partitionBy(direction, sourceProperty, targetProperty)
andclusterBy(direction, sourceProperty, targetProperty[, order])
. Thedirection
can only beOUT
forpartitionBy
, orIN
orOUT
forclusterBy
. ThesourceProperty
is the property name from the source vertex label CQL table, whereas thetargetProperty
is the property name in the edge label CQL table. The optionalorder
isAsc
, the default, orDesc
. -
Adding a property to an edge label
-
Add a property to an edge label:
schema.edgeLabel('authored'). addProperty('one', Int). addProperty('two', Int). alter()
This command adds two properties to all
authored
edge labels. -
Add a property to a specific edge label:
schema.edgeLabel('authored'). from('person'). to('book'). addProperty('one', Int). addProperty('two', Int). alter()
This command adds two properties to only the
authored
edge label betweenperson
andbook
.
Example
The edge labels used for the DSG QuickStart example used throughout the documentation:
// ********
// EDGE LABELS
// ********
// SYNTAX:
//schema.edgeLabel('edgeLabel').
// [ materializedView('indexName'). | secondaryIndex('indexName'). | searchIndex('indexName'). | inverse(). ]
// [ by('propertyName'). ]
// [ tableName('tableName'). ]
// [ ifNotExists(). ]
// from('vertexLabel').
// to('vertexLabel').
// [ partitionBy('propertyName', propertyType). [ ... ] ]
// [ clusterBy('propertyName', propertyType). [ ... ] ]
// [ property('propertyName', propertyType). ]
// [ create() | describe() | drop() |
// addProperty('propertyName', propertyType).alter() |
// dropProperty('propertyName', propertyType).alter() ]
// [fromExistingTable('tableName')
// from('vertexLabel'). [ mappingProperty('CQLPropertyName'). ]
// to('vertexLabel'). [ mappingProperty('CQLPropertyName'). ]]
// ********
// tag::createELs_person_authored_book[]
schema.edgeLabel('authored').
ifNotExists().
from('person').to('book').
create()
// end::createELs_person_authored_book[]
// tag::createELs_person_ate_meal[]
schema.edgeLabel('ate').
tableName('person_eating').
ifNotExists().
from('person').to('meal').
property('meal_date', Date).
create()
// end::createELs_person_ate_meal[]
// tag::createELs_person_knows_person[]
schema.edgeLabel('knows').
ifNotExists().
from('person').to('person').
property('since', Date).
create()
// end::createELs_person_knows_person[]
// tag::createELs_meal_includes_mealItem[]
schema.edgeLabel('includes').
ifNotExists().
from('meal').to('meal_item').
property('num_serv', Int).
create()
// end::createELs_meal_includes_mealItem[]
// tag::createELs_recipe_includes_ingredient[]
schema.edgeLabel('includes').
ifNotExists().
from('recipe').to('ingredient').
property('amount', Text).
create()
// end::createELs_recipe_includes_ingredient[]
// tag::createELs_recipe_included_in_meal[]
schema.edgeLabel('included_in').
ifNotExists().
from('recipe').to('meal').
property('amount', Text).
create()
// end::createELs_recipe_included_in_meal[]
// tag::createELs_recipe_included_in_book[]
schema.edgeLabel('included_in').
ifNotExists().
from('recipe').to('book').
create()
// end::createELs_recipe_included_in_book[]
// tag::createELs_person_created_recipe[]
schema.edgeLabel('created').
ifNotExists().
from('person').to('recipe').
property('create_date', Date).
create()
// end::createELs_person_created_recipe[]
// tag::createELs_person_reviewed_recipe[]
schema.edgeLabel('reviewed').
ifNotExists().
from('person').to('recipe').
property('time', Time).
property('year', Date).
property('stars', Int).
property('comment', Text).
create()
// end::createELs_person_reviewed_recipe[]
// tag::createELs_fridge_sensor_contains_ingredient[]
schema.edgeLabel('contains').
ifNotExists().
from('fridge_sensor').to('ingredient').
property('expire_date', Date).
create()
// end::createELs_fridge_sensor_contains_ingredient[]
// tag::createELs_store_is_stocked_with_ingredient[]
schema.edgeLabel('is_stocked_with').
ifNotExists().
from('store').to('ingredient').
property('expire_date', Date).
create()
// end::createELs_store_is_stocked_with_ingredient[]
// tag::createELs_home_is_located_at_location[]
schema.edgeLabel('is_located_at').
ifNotExists().
from('home').to('location').
create()
// end::createELs_home_is_located_at_location[]
// tag::createELs_store_isLocatedAt_location[]
schema.edgeLabel('is_located_at').
ifNotExists().
from('store').to('location').
create()
// end::createELs_store_isLocatedAt_location[]
//tag::createELs_fridge_sensor_is_located_at_home[]
schema.edgeLabel('is_located_at').
ifNotExists().
from('fridge_sensor').to('home').
create()
//end::createELs_fridge_sensor_is_located_at_home[]