Monday 31 January 2022

JanusGraph: Get all the edges of this vertex

Using 'edges' method we can get all the edges (incoming and outgoing) of this 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.BOTH);
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.

 

GetAllEdges.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.JanusGraphEdge;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;

public class GetAllEdges {
	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.BOTH);
			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

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

 

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment