Monday, 31 January 2022

JanusGraph: Get all the outgoing adjacent vertices of a graph

Using 'vertices' method, you can get all the outgoing adjacent vertices.

 

public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels)

Gets an Iterator of adjacent vertices. Argument direction specifies the adjacency direction of the vertices to retrieve off this vertex. Direction can be IN, OUT, BOTH. Argument 'edgeLabels' specifies the labels of the edges associated with the vertices to retrieve. If no labels are provided, then get all edges.

 

Example

Iterator<Vertex> adjacentVertices = ramVertex.vertices(Direction.OUT);
while (adjacentVertices.hasNext()) {
	Vertex vertex = adjacentVertices.next();
	System.out.println("\t" + vertex.property("name").value());
}

 

Find the below working application.

 

AdjacentOutgoingVertices.java

package com.sample.app.vertex;

import java.util.Iterator;

import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;

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

		JanusGraph janusGraph = null;

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

			// 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 ramVertex = janusGraph.addVertex("person");
			ramVertex.property("name", "Ram");
			ramVertex.property("age", 34);
			ramVertex.property("gender", 'M');

			// Create a person vertex
			JanusGraphVertex rahimVertex = janusGraph.addVertex("person");
			rahimVertex.property("name", "Rahim");
			rahimVertex.property("age", 41);
			rahimVertex.property("gender", 'M');

			ramVertex.addEdge("knows", krishnaVertex, "from", 2017, "colleague", "N");
			rahimVertex.addEdge("knows", ramVertex, "from", 2019, "colleague", "Y");

			janusGraph.tx().commit();

			System.out.println("Adjacent vertices of Ram are:");
			Iterator<Vertex> adjacentVertices = ramVertex.vertices(Direction.OUT);
			while (adjacentVertices.hasNext()) {
				Vertex vertex = adjacentVertices.next();
				System.out.println("\t" + vertex.property("name").value());
			}

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

	}
}

 

Output

Adjacent vertices of Ram are:
	Krishna
Done!!!

 

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment