edgeLabel

How to create an edge label.

Synopsis

schema.edgeLabel('*edgeLabel*').
  [ materializedView('*indexName*'). | secondaryIndex('*indexName*'). | searchIndex('*indexName*'). | inverse(). ]
    [ by('*propertyName*'). ]
  [ tableName('*tableName*'). ]
  [fromExistingTable('*tableName*')
    from('*vertexLabel*'). [ mappingProperty('*CQLPropertyName*'). ]
    to('*vertexLabel*'). [ mappingProperty('*CQLPropertyName*'). ]]
  [ ifNotExists(). ]
  from('*vertexLabel*').
  to('*vertexLabel*').
  [ partitionBy('*propertyName*', *propertyType*). [ ... ] ]
  [ clusterBy('*propertyName*', *propertyType*). [ ... ] ]
  [ property('*propertyName*', *propertyType*). ]
  [ create() | describe() | drop() | ( addProperty | dropProperty )('*propertyName*', *propertyType*).alter() ]

Description

An edge label specifies a type of edge that can be stored in DataStax Graph (DSG). An edge label must have two defined vertex labels for the outgoing vertex from and the incoming vertex to, and can, in addition, have properties and indexes defined. An edge label has single cardinality by default, but can be defined with multiple cardinality using clustering keys. The partitionBy and clusteringBy are used to specify an edge id, identifying the partition storage location and order within the partition, respectively. Edges are unidirectional, and indexes are defined accordingly. The inverse() step will an index to simulate bidirectionality of edges.

  • materializedView, secondaryIndex, searchIndex, inverse

    Set an index for an edge label.

  • ifNotExists

    Check for lack of current existence with ifNotExists() before creating a new edge label.

  • tableName

    Specify a table name for the CQL table that holds the edge label data.

  • partitionBy

    Specify an edge label partition location. The value consists of one or more properties, with multiple properties forming a composite partition key. Which partition the data will be stored on is defined with this step.

  • clusteringBy

    Specify whether an edge label can have any clustering columns. The value consists of one or more properties that specify how data will be stored within a partition.

  • property

    Properties can be defined for vertices and edges. Allowed characters for the name are alphabetical or underscore.

  • create

    Create an edge label.

  • describe

    Describe an edge label.

  • drop

    Drop an edge label.

  • addProperty.alter, dropProperty.alter

    Add or drop a property by altering a previously created edge label.

  • fromExistingTable

    Convert an existing CQL table into an edge label. The CQL table columns can be specified with mappingProperty.

Examples

Create an edge label authored:

schema.edgeLabel('authored').
  ifNotExists().
  from('person').to('book').
  create()

This edge label is created if it doesn’t already exist.

Create an edge label knows with an edge property:

schema.edgeLabel('knows').
  ifNotExists().
  from('person').to('person').
  property('since', Date).
  create()

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.

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.

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.

Add a property to an edge label:

schema.edgeLabel('authored').
    addProperty('one', Int).
    addProperty('two', Int).
    alter()

Add a property to a specific edge label:

schema.edgeLabel('authored').
    from('person').
    to('book').
    addProperty('one', Int).
    addProperty('two', Int).
    alter()

A quick reference to indexes is included here, see indexes for more thorough information.

Create a materialized view index on an edge label:

schema.edgeLabel('reviewed').
  from('person').to('recipe').
  materializedView('person__reviewed__recipe_by_person_person_id_year').
  ifNotExists().
  partitionBy(OUT, 'person_id').
  clusterBy('year', Asc).
  clusterBy(IN, 'recipe_id', Asc).
  create()

Create a secondary index on an edge label:

schema.edgeLabel('is_stocked_with').
  from('store').to('ingredient').
  secondaryIndex('store_is_stocked_with_ingredient_by_store_store_id_expire_date').
  ifNotExists().
  partitionBy(OUT, 'store_id').
  clusterBy('expire_date', Asc).
  clusterBy(IN, 'ingred_id', Asc).
  create()

Create a search index on a vertex label:

schema.edgeLabel('reviewed').
    from('person').to('recipe').
    searchIndex().
    ifNotExists().
    by('comment').
    create()

Get the schema creation command for all edge labels using the describe() command:

schema.edgeLabels().describe()

Get the schema creation command for an edge label using the describe() command:

schema.edgeLabel('authored').describe()

Get the schema creation command for a specific edge label using the describe() command:

schema.edgeLabel('authored').from('person').to('book').describe()

Drop an edge label:

schema.edgeLabel('ate').
  ifExists().
  drop()

Drop a property from an edge label:

schema.edgeLabel('authored').
    dropProperty('one').
    dropProperty('two').
    alter()

Drop a property from a specific edge label:

schema.edgeLabel('authored').
    from('person').
    to('book').
    dropProperty('one').
    dropProperty('two').
    alter()

Drop an index for a vertex label:

schema.vertexLabel('person').
    materializedView('person_by_gender').
    ifExists().
    drop()

Drop an index for a vertex label:

schema.vertexLabel('store').
    searchIndex().
    dropProperty('name').
    alter()

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