Monday, 31 January 2022

JanusGraph: Get all the incoming adjacent vertices of a graph

Using 'vertices' method, you can get all the incoming 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.IN);
while (adjacentVertices.hasNext()) {
	Vertex vertex = adjacentVertices.next();
	System.out.println("\t" + vertex.property("name").value());
}

 

Find the below working application.

 

AdjacentIncomingVertices.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 AdjacentIncomingVertices {
	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("Incoming adjacent vertices of Ram are:");
			Iterator<Vertex> adjacentVertices = ramVertex.vertices(Direction.IN);
			while (adjacentVertices.hasNext()) {
				Vertex vertex = adjacentVertices.next();
				System.out.println("\t" + vertex.property("name").value());
			}

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

	}
}

 

Output

Incoming adjacent vertices of Ram are:
	Rahim
Done!!!

 


  

Previous                                                 Next                                                 Home

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

JanusGraph: Get all the adjacent vertices

Using 'vertices' method, you can get all the 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.BOTH);
while (adjacentVertices.hasNext()) {
	Vertex vertex = adjacentVertices.next();
	System.out.println("\t" + vertex.property("name").value());
}

 

Find the below working application.

 

AdjacentVertices.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 AdjacentVertices {
	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.BOTH);
			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
	Rahim
Done!!!

 

 

  

Previous                                                 Next                                                 Home

JanusGraph: Get all the outgoing edges of vertex

By supplying Direction.OUT argument to the edges method, we can get all the outgoing 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.OUT);
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.

 

OutgoingEdges.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 OutgoingEdges {
	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.OUT);
			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
Done!!!

 

 

  

Previous                                                 Next                                                 Home

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

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