Monday 7 February 2022

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

No comments:

Post a Comment