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() or dropProperty()

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 between person and book:

    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 between person and person with an edge property since:

    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 a tableName

    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 is ate.An edge property meal_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 property name will map to the CQL table column person_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) and clusterBy(direction, sourceProperty, targetProperty[, order]). The direction can only be OUT for partitionBy, or IN or OUT for clusterBy. The sourceProperty is the property name from the source vertex label CQL table, whereas the targetPropertyis the property name in the edge label CQL table. The optional order is Asc, the default, or Desc.

  • 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 authorededge 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 authorededge label between person and book.

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[]

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