Monday, 31 January 2022

JanusGraph: Get all the incoming edges of a vertex

By supplying Direction.IN argument to the edges method, we can get all the incoming edges of a vertex.

 

public Iterator<Edge> edges(final Direction direction, final String... edgeLabels);

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

 

Example

Iterator<Edge> edges = ramVertex.edges(Direction.IN);
while (edges.hasNext()) {
	Edge edge = edges.next();
	System.out.println(edge.outVertex().property("name").value() + "--" + edge.label() + "-->" + edge.inVertex().property("name").value());
}

 

Find the below working application.

 

IncomingEdges.java
package com.sample.app.edges;

import java.util.Iterator;

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

public class IncomingEdges {
	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();

			Iterator<Edge> edges = ramVertex.edges(Direction.IN);
			while (edges.hasNext()) {
				Edge edge = edges.next();
				System.out.println(edge.outVertex().property("name").value() + "--" + edge.label() + "-->"
						+ edge.inVertex().property("name").value());
			}

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

	}
}

 

Output

Rahim--knows-->Ram
Done!!!

 

 

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment