Wednesday, 9 February 2022

JanusGraph: property TTL

Just like how we defined TTL(time to live)  on edge and vertex level, we can define TTL on property level.

 

You can define ttl at property level using ‘janusGraphManagement.setTTL’ method.

PropertyKey propertyKey = janusGraphManagement.makePropertyKey("tempKey").dataType(String.class).make();
janusGraphManagement.setTTL(propertyKey, Duration.ofSeconds(10));
janusGraphManagement.commit();

 

Find the below working application.

 

PropertyTTL.java
package com.sample.app.schema;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;
import org.janusgraph.core.PropertyKey;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.janusgraph.diskstorage.BackendException;

public class PropertyTTL {

	public static void main(String args[]) throws InterruptedException, BackendException {
		JanusGraph janusGraph = null;

		try {
			janusGraph = JanusGraphFactory.open("/Users/Shared/janus.properties");
			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();

			PropertyKey propertyKey = janusGraphManagement.makePropertyKey("tempKey").dataType(String.class).make();
			janusGraphManagement.setTTL(propertyKey, Duration.ofSeconds(10));
			janusGraphManagement.commit();

			JanusGraphVertex janusGraphVertex = janusGraph.addVertex("login");

			// Add properties to the vertex
			janusGraphVertex.property("tempKey", "AGGF&!^@%ASASSF");
			janusGraph.tx().commit();

			Object id = janusGraphVertex.id();
			JanusGraphVertex janusGraphVertex1 = (JanusGraphVertex) janusGraph.vertices(id).next();
			VertexProperty<String> vertexProperty = janusGraphVertex1.property("tempKey");
			System.out.println("tempKey : " + vertexProperty.value());

			System.out.println("\nGoing to sleep for 15 seconds");
			TimeUnit.SECONDS.sleep(15);
			janusGraph.tx().commit();

			janusGraphVertex1 = (JanusGraphVertex) janusGraph.vertices(id).next();
			vertexProperty = janusGraphVertex1.property("tempKey");

			if (vertexProperty == null) {
				System.out.println("\nNo property found with tempKey");
			} else {
				System.out.println("\ntempKey : " + vertexProperty.value());
			}

		} finally {
			// drop the graph. This is an irreversible operation that will delete all graph
			// and index data.
			JanusGraphFactory.drop(janusGraph);
			System.out.println("Done!!!");
		}

	}

}

 

Output

 

tempKey : AGGF&!^@%ASASSF

Going to sleep for 15 seconds
Done!!!
Exception in thread "main" java.lang.IllegalStateException: The property does not exist as it has no key, value, or associated element
	at org.apache.tinkerpop.gremlin.structure.Property$Exceptions.propertyDoesNotExist(Property.java:147)
	at org.apache.tinkerpop.gremlin.structure.util.empty.EmptyVertexProperty.value(EmptyVertexProperty.java:74)
	at com.sample.app.schema.PropertyTTL.main(PropertyTTL.java:43)

 

 

 

Previous                                                 Next                                                 Home

JanusGraph: Edge TTL

In JanusGraph, you can configure Edge and vertex labels with a time-to-live (TTL). Edges and vertices with TTL labels will be automatically removed from the graph when the configured TTL has passed after their initial creation.

 

Follow below step-by-step procedure to build TTL vertex.

 

Step 1: Define TTL edge label.

JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
org.janusgraph.core.EdgeLabel ttl10 = janusGraphManagement.makeEdgeLabel("permissions-ttl-10").make();
janusGraphManagement.setTTL(ttl10, Duration.ofSeconds(10));
janusGraphManagement.commit();

Step 2: Define edge with this label.

JanusGraphEdge janusGraphEdge = roleVertex.addEdge("permissions-ttl-10", krishnaVertex);

Find the below working application.

 

EdgeTTL.java

package com.sample.app.edges;

import java.time.Duration;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.apache.tinkerpop.gremlin.structure.Edge;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphEdge;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.janusgraph.diskstorage.BackendException;

public class EdgeTTL {

	public static void main(String args[]) throws InterruptedException, BackendException {
		JanusGraph janusGraph = null;

		try {
			janusGraph = JanusGraphFactory.open("/Users/Shared/janus.properties");
			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
			org.janusgraph.core.EdgeLabel ttl10 = janusGraphManagement.makeEdgeLabel("permissions-ttl-10").make();
			janusGraphManagement.setTTL(ttl10, Duration.ofSeconds(10));
			janusGraphManagement.commit();

			// Create a person vertex
			JanusGraphVertex krishnaVertex = janusGraph.addVertex("person");
			krishnaVertex.property("name", "Krishna");
			krishnaVertex.property("age", 31);
			krishnaVertex.property("gender", 'M');

			// Create a person vertex
			JanusGraphVertex roleVertex = janusGraph.addVertex("role");
			roleVertex.property("name", "temp-admin");

			JanusGraphEdge janusGraphEdge = roleVertex.addEdge("permissions-ttl-10", krishnaVertex);
			janusGraphEdge.property("name", "permissions-ttl-10");

			janusGraph.tx().commit();

			Object id = janusGraphEdge.id();

			JanusGraphEdge janusGraphEdge1 = (JanusGraphEdge) janusGraph.edges(id).next();
			System.out.println("Found the edge : " + janusGraphEdge1.property("name").value());

			System.out.println("\nGoing to sleep for 15 seconds");
			TimeUnit.SECONDS.sleep(15);

			janusGraph.tx().commit();

			Iterator<Edge> edgeIterator = janusGraph.edges(id);

			if (edgeIterator.hasNext()) {
				janusGraphEdge1 = (JanusGraphEdge) edgeIterator.next();
				System.out.println("\nIs edge removed : " + janusGraphEdge1.isRemoved());
			} else {
				System.out.println("\nNo edge found with the id " + id);
			}

		} finally {
			// drop the graph. This is an irreversible operation that will delete all graph
			// and index data.
			JanusGraphFactory.drop(janusGraph);
			System.out.println("Done!!!");
		}

	}

}

Output

Found the edge : permissions-ttl-10

Going to sleep for 15 seconds

No edge found with the id 1lb-394-t1-36g
Done!!!

Note

a.   modifying an edge resets the TTL for that edge

b.   You can even update the TTL of an edge label, but it takes some time to reflect.

 




Previous                                                 Next                                                 Home

JanusGraph: vertexes TTL (time to live)

In JanusGraph, you can configure Edge and vertex labels with a time-to-live (TTL). Edges and vertices with TTL labels will be automatically removed from the graph when the configured TTL has passed after their initial creation.

 

Follow below step-by-step procedure to build TTL vertex.

 

Step 1: Define a static vertex label and set TTL to it.

JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
VertexLabel ttl30 = janusGraphManagement.makeVertexLabel("ttl-30").setStatic().make();
janusGraphManagement.setTTL(ttl30, Duration.ofSeconds(30));
janusGraphManagement.commit();

 

Step 2: Define a vertex TTL using the label ttl-30.

JanusGraphVertex janusGraphVertex = janusGraph.addVertex("ttl-30");

This vertex will be removed post 30 seconds.

 

Find the below working application.

 

VertexTTL.java

package com.sample.app.vertex;

import java.time.Duration;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.janusgraph.diskstorage.BackendException;

public class VertexTTL {

	public static void main(String args[]) throws InterruptedException, BackendException {
		JanusGraph janusGraph = null;
		
		try {
			janusGraph = JanusGraphFactory.open("/Users/Shared/janus.properties");
			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
			org.janusgraph.core.VertexLabel ttl30 = janusGraphManagement.makeVertexLabel("ttl-30").setStatic().make();
			janusGraphManagement.setTTL(ttl30, Duration.ofSeconds(30));
			janusGraphManagement.commit();

			JanusGraphVertex janusGraphVertex = janusGraph.addVertex("ttl-30");

			// Add properties to the vertex
			janusGraphVertex.property("name", "lifetime-30seconds");
			janusGraph.tx().commit();

			Object id = janusGraphVertex.id();
			JanusGraphVertex janusGraphVertex1 = (JanusGraphVertex) janusGraph.vertices(id).next();
			System.out.println("Found the vertex : " + janusGraphVertex1.property("name").value());

			System.out.println("\nGoing to sleep for 35 seconds");
			TimeUnit.SECONDS.sleep(35);
			
			janusGraph.tx().commit();

			Iterator<Vertex> vertexIterator = janusGraph.vertices(id);

			if (vertexIterator.hasNext()) {
				JanusGraphVertex vertex = (JanusGraphVertex) vertexIterator.next();
				System.out.println("\nIs vertex removed : " + vertex.isRemoved());
			} else {
				System.out.println("\nNo vertex found with the id " + id);
			}

		} finally {
			// drop the graph. This is an irreversible operation that will delete all graph and index data.
			JanusGraphFactory.drop(janusGraph);
			System.out.println("Done!!!");
		}

	}

}

Output

Found the vertex : lifetime-30seconds

Going to sleep for 35 seconds

No vertex found with the id 4228
Done!!!

Note

a.   The configured TTL applies to the vertex, its properties, and all incident edges to ensure that the entire vertex is removed from the graph.

b.   Vertex TTL only applies to static vertex labels.




 

 

 

Previous                                                 Next                                                 Home

Monday, 7 February 2022

JanusGraph: Static or unmodifiable vertexes

You can define unmodifiable vertexes using static labels. Vertices of static label cannot be modified outside of the transaction in which they were created.

 

How to define static vertex?

Step 1: Define static label.

JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
janusGraphManagement.makeVertexLabel("publish").setStatic().make();
janusGraphManagement.commit();

 

Step 2: Define a vertex using static label.

JanusGraphVertex janusGraphVertex = janusGraph.addVertex("publish");

Find the below working application.

 

StaticVertex.java

package com.sample.app.vertex;

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

public class StaticVertex {

	public static void main(String args[]) {

		try (JanusGraph janusGraph = JanusGraphFactory.open("/Users/Shared/janus.properties")) {
			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();
			janusGraphManagement.makeVertexLabel("publish").setStatic().make();
			janusGraphManagement.commit();

			JanusGraphVertex janusGraphVertex = janusGraph.addVertex("publish");

			// Add properties to the vertex
			janusGraphVertex.property("bookName", "All over the world");
			janusGraphVertex.property("publishedDate", 1643202767l);

			janusGraph.tx().commit();

			System.out.println("Trying to update static vertex outside of the transaction");

			try {
				janusGraphVertex.property("a", 1);
			} catch (Exception e) {
				e.printStackTrace();
			}

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

	}

}

Output

Trying to update static vertex outside of the transaction
org.janusgraph.core.SchemaViolationException: Cannot modify unmodifiable vertex: v[4172]
	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.verifyWriteAccess(StandardJanusGraphTx.java:352)
	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.addProperty(StandardJanusGraphTx.java:848)
	at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.addProperty(StandardJanusGraphTx.java:842)
	at org.janusgraph.graphdb.vertices.AbstractVertex.property(AbstractVertex.java:180)
	at org.janusgraph.graphdb.vertices.AbstractVertex.property(AbstractVertex.java:160)
	at org.janusgraph.core.JanusGraphVertex.property(JanusGraphVertex.java:72)
	at com.sample.app.vertex.StaticVertex.main(StaticVertex.java:28)
Done!!!



 

Previous                                                 Next                                                 Home

JanusGraph: change or update schema elements

Once edge label, property key, or vertex label defined and committed to the graph, their definition can't be changed. But you can change the name of schema elements.

 

Using 'changeName' method of JanusGraphManagement interface, you can change the name of schema element.

 

Signature

void changeName(JanusGraphSchemaElement element, String newName)

 

Example

vertexLabel = janusGraphManagement.getVertexLabel("spreadsheet");

janusGraphManagement.changeName(vertexLabel, "application_software");

 

Find the below working application.

 

ChangeSchemaElementName.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 ChangeSchemaElementName {
	public static void main(String args[]) {

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

			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();

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

			janusGraph.tx().commit();

			vertexLabel = janusGraphManagement.getVertexLabel("spreadsheet");
			janusGraphManagement.changeName(vertexLabel, "application_software");

			janusGraph.tx().commit();

			String vertexLabels = janusGraphManagement.printVertexLabels();

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

	}
}

 

Output

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


Done!!!

 

Will the schema name changes reflect immediately?

No, not exactly.  When schema name changes are notified to all the JanusGraph instances via the storage backend, it will take a while for the schema changes to take effect.

 

 

 

 

Previous                                                 Next                                                 Home

JanusGraph: implicit schema generation

If you do not explicitly model the schema for labels, properties, JanusGraph implicitly define the schema when it first encounter the edge label, vertex label and properties.

 

a.   implicitly created edge labels have multiplicity MULTI and

b.   implicitly created property keys have cardinality SINGLE.

 

Can I control automatic schema generation?

Yes, you can control by implementing and registering your own DefaultSchemaMaker.

 

As per the documentation, It is highly recommended to explicitly define all the schema elements and disable automatic schema creation by setting schema.default=none in the JanusGraph graph configuration.

 

Previous                                                 Next                                                 Home