Monday, 7 February 2022

JanusGraph: Define vertex labels

You can define vertex labels using ‘janusGraphManagement.makeVertexLabel()’ method. Vertex labels must be unique in the graph.

 

Example

VertexLabelMaker vertexLabelMaker = janusGraphManagement.makeVertexLabel("software");
VertexLabel vertexLabel = vertexLabelMaker.make();

 

Find the below working application.

 

ModelVertexLabel.java

 

package com.sample.app.schema;

import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.VertexLabel;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.janusgraph.core.schema.VertexLabelMaker;

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

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

			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();

			VertexLabelMaker vertexLabelMaker = janusGraphManagement.makeVertexLabel("software");
			VertexLabel vertexLabel = vertexLabelMaker.make();

			janusGraph.tx().commit();

			String vertexLabels = janusGraphManagement.printVertexLabels();

			System.out.println(vertexLabels);
		} finally {
			System.out.println("\nDone!!!");
		}

	}
}

 

Output

------------------------------------------------------------------------------------------------
Vertex Label Name              | Partitioned | Static                                             |
---------------------------------------------------------------------------------------------------
software                       | false       | false                                              |
---------------------------------------------------------------------------------------------------


Done!!!

 

Is vertex labels mandatory?

Vertex labels are optional.

 

 

Previous                                                 Next                                                 Home

JasunGraph: Relationship types

In JanusGraph, both edge labels and property keys are considered as relationship types. As per the documentation, RelationType names must be unique in a graph database, that means edge labels and property keys may not have the same name.

 

Below snippet is used to get relationship type by name.

RelationType relationType = janusGraphManagement.getRelationType("favoritePlaces");

 

Find the below working application.

 

GetRelationshipTypes.java

package com.sample.app.schema;

import org.janusgraph.core.Cardinality;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.RelationType;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.janusgraph.core.schema.PropertyKeyMaker;

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

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

			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();

			PropertyKeyMaker propertyKeyMaker = janusGraphManagement.makePropertyKey("favoritePlaces");
			propertyKeyMaker.cardinality(Cardinality.SET);
			propertyKeyMaker.dataType(String.class);

			propertyKeyMaker.make();

			janusGraph.tx().commit();

			RelationType relationType = janusGraphManagement.getRelationType("favoritePlaces");

			if (relationType.isPropertyKey()) {
				System.out.println("Given relationship is a property key");
			}

			if (relationType.isEdgeLabel()) {
				System.out.println("Given relationship is an edge label");
			}

		} finally {
			System.out.println("\nDone!!!");
		}

	}
}

 

Output

Given relationship is a property key

Done!!!

 

 

 

 

 

Previous                                                 Next                                                 Home

JanusGraph: Define property keys

Vertexes and edges in janus graph has zero or more properties Property is a simple <key, value> pair.

 

for example, the property name='Krishna' has the key name and value 'Krishna'.

 

Property key name must be unique within a graph. As per the documentation, spaces and special characters should be avoided in property key name.

 

Can a property takes more than one value?

Yes, you can attach multiple values to a property key. Default cardinality is set to SINGLE.

 

Can I constrain a property to specific type?

Yes, you can constrain datatype. At the time of writing this post, JanusGraph support below datatypes.

 

Data type

Description

String

Specifies a character sequence

Character

Specifies single character

Boolean

Specifies true or false

Byte

Specifies byte value

Short

Specifies short value

Integer

Specifies integer value

Long

Specifies long value

Float

Specifies 4 byte floating point number

Double

Specifies 8 byte floating point number

Date

Specific instant in time (java.util.Date)

Geoshape

Specifies Geographic shape like point, circle or box

UUID

Specifies Universally unique identifier (java.util.UUID)

 

Property key cardinality

By specifying the Cardinality, we can set the cardinality of the values associated with given key for a particular element.

 

Below table summarizes the possible key cardinalities.

 

Cardinality

Description

SINGLE

At most one value is associated with the key

LIST

Multiple values and duplicate values may be associated with the given key.

SET

Multiple but distinct values may be associated with the given key.

 

Example

PropertyKeyMaker propertyKeyMaker = janusGraphManagement.makePropertyKey("hobbies");
propertyKeyMaker.cardinality(Cardinality.SET);
propertyKeyMaker.dataType(String.class);

PropertyKey propertyKey = propertyKeyMaker.make();

 

Find the below working application.

 

ModelPropertyKeys.java

 

package com.sample.app.schema;

import org.janusgraph.core.Cardinality;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.PropertyKey;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.janusgraph.core.schema.PropertyKeyMaker;

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

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

			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();

			PropertyKeyMaker propertyKeyMaker = janusGraphManagement.makePropertyKey("hobbies");
			propertyKeyMaker.cardinality(Cardinality.SET);
			propertyKeyMaker.dataType(String.class);

			PropertyKey propertyKey = propertyKeyMaker.make();

			String propertyKeys = janusGraphManagement.printPropertyKeys();

			System.out.println(propertyKeys);
		} finally {
			System.out.println("\nDone!!!");
		}

	}
}

Output

------------------------------------------------------------------------------------------------
Property Key Name              | Cardinality | Data Type                                          |
---------------------------------------------------------------------------------------------------
hobbies                        | SET         | class java.lang.String                             |
---------------------------------------------------------------------------------------------------


Done!!!




Previous                                                 Next                                                 Home

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

JanusGraph: print vertex and edge labels, indexes, and property keys.

Below snippet is used to print vertex and edge labels, indexes, and property keys.

JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
String schema = janusGraphManagement.printSchema();
System.out.println(schema);

Find the below working application.

 

PrintSchema.java

package com.sample.app.schema;

import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.schema.JanusGraphManagement;

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

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

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

	}
}

Output

------------------------------------------------------------------------------------------------
Vertex Label Name              | Partitioned | Static                                             |
---------------------------------------------------------------------------------------------------
person                         | false       | false                                              |
employee                       | false       | false                                              |
---------------------------------------------------------------------------------------------------
Edge Label Name                | Directed    | Unidirected | Multiplicity                         |
---------------------------------------------------------------------------------------------------
knows                          | true        | false       | MULTI                                |
---------------------------------------------------------------------------------------------------
Property Key Name              | Cardinality | Data Type                                          |
---------------------------------------------------------------------------------------------------
name                           | SINGLE      | class java.lang.String                             |
age                            | SINGLE      | class java.lang.Integer                            |
gender                         | SINGLE      | class java.lang.Character                          |
from                           | SINGLE      | class java.lang.Integer                            |
colleague                      | SINGLE      | class java.lang.String                             |
hobbies                        | SET         | class java.lang.String                             |
football                       | SINGLE      | class java.lang.String                             |
experience                     | SINGLE      | class java.lang.Double                             |
ABC Corp                       | SINGLE      | class java.lang.Double                             |
Delta Inc                      | SINGLE      | class java.lang.Double                             |
since                          | SINGLE      | class java.lang.Integer                            |
---------------------------------------------------------------------------------------------------
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