Wednesday, 9 February 2022

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

No comments:

Post a Comment