Monday 7 February 2022

JanusGraph: Define edge labels

Edge labels are used to define semantics of the relationship between two vertexes. Edge label name must be unique within the graph.

 

For example, the edge labelled knows between two vertexes v1 and v2 encodes a relationship between two persons.

 

You can set the multiplicity of edges between vertices for a given label while defining edge label. Below table summarizes different possible multiplicity options.

 

Multiplicity

Description

MULTI

Allows multiple edges of the same label between any pair of vertices. This is used to define multigraphs.

SIMPLE

Allows at most one edge of such label between any pair of vertices.

MANY2ONE

There can only be a single out-edge of this label for a given vertex but multiple in-edges. (i.e. out-unique)

ONE2MANY

There can only be a single in-edge of this label for a given vertex but multiple out-edges (i.e. in-unique)

ONE2ONE

There can be only a single in and out-edge of this label for a given vertex (i.e. unique in both directions).


Example

EdgeLabelMaker edgeLabelMaker = janusGraphManagement.makeEdgeLabel("knows");
edgeLabelMaker.multiplicity(Multiplicity.SIMPLE);
edgeLabelMaker.make();

Above snippet defines a label ‘knows’.

 

Find the below working application.

 

ModelEdgeLabel.java

package com.sample.app.schema;

import org.janusgraph.core.EdgeLabel;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.Multiplicity;
import org.janusgraph.core.schema.EdgeLabelMaker;
import org.janusgraph.core.schema.JanusGraphManagement;

public class ModelEdgeLabel {
	public static void main(String args[]) {

		try (JanusGraph janusGraph = JanusGraphFactory.open("/Users/Shared/janus.properties")) {

			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();

			EdgeLabelMaker edgeLabelMaker = janusGraphManagement.makeEdgeLabel("knows");
			edgeLabelMaker.multiplicity(Multiplicity.SIMPLE);
			EdgeLabel edgeLabel = edgeLabelMaker.make();

			String schema = janusGraphManagement.printSchema();
			System.out.println(schema);
		} finally {
			System.out.println("\nDone!!!");
		}

	}
}

Output

------------------------------------------------------------------------------------------------
Vertex Label Name              | Partitioned | Static                                             |
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
Edge Label Name                | Directed    | Unidirected | Multiplicity                         |
---------------------------------------------------------------------------------------------------
knows                          | true        | false       | SIMPLE                               |
---------------------------------------------------------------------------------------------------
Property Key Name              | Cardinality | Data Type                                          |
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
Graph Index (Vertex)           | Type        | Unique    | Backing        | Key:           Status |
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
Graph Index (Edge)             | Type        | Unique    | Backing        | Key:           Status |
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
Relation Index (VCI)           | Type        | Direction | Sort Key       | Order    |     Status |
---------------------------------------------------------------------------------------------------


Done!!!

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment