Creating edge label schema
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()
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.CAUTION: DataStax Graph limits the number of vertex
and edge labels to 200 per graph.
Prerequisites
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 namedperson_eating
, while the edge label isate
.An edge propertymeal_date
will be included in the edge label definition.
Converting CQL table to edge labels
-
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 usesmappingProperty
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 ispartitionBy(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 allauthored
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 theauthored
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'). ]] // ******** // START-createELs_person_authored_book schema.edgeLabel('authored'). ifNotExists(). from('person').to('book'). create() // END-createELs_person_authored_book // START-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 // START-createELs_person_knows_person schema.edgeLabel('knows'). ifNotExists(). from('person').to('person'). property('since', Date). create() // END-createELs_person_knows_person // START-createELs_meal_includes_mealItem schema.edgeLabel('includes'). ifNotExists(). from('meal').to('meal_item'). property('num_serv', Int). create() // END-createELs_meal_includes_mealItem // START-createELs_recipe_includes_ingredient schema.edgeLabel('includes'). ifNotExists(). from('recipe').to('ingredient'). property('amount', Text). create() // END-createELs_recipe_includes_ingredient // START-createELs_recipe_included_in_meal schema.edgeLabel('included_in'). ifNotExists(). from('recipe').to('meal'). property('amount', Text). create() // END-createELs_recipe_included_in_meal // START-createELs_recipe_included_in_book schema.edgeLabel('included_in'). ifNotExists(). from('recipe').to('book'). create() // END-createELs_recipe_included_in_book // START-createELs_person_created_recipe schema.edgeLabel('created'). ifNotExists(). from('person').to('recipe'). property('create_date', Date). create() // END-createELs_person_created_recipe // START-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 // START-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 // START-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 // START-createELs_home_is_located_at_location schema.edgeLabel('is_located_at'). ifNotExists(). from('home').to('location'). create() // END-createELs_home_is_located_at_location // START-createELs_store_isLocatedAt_location schema.edgeLabel('is_located_at'). ifNotExists(). from('store').to('location'). create() // END-createELs_store_isLocatedAt_location //START-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