Monday, 7 February 2022

JanusGraph: Schema and data modelling

 


            JanusGraph: print vertex and edge labels, indexes, and property keys
            JanusGraph: Define edge labels
            JanusGraph: Define property keys
            JasunGraph: Relationship types
            JanusGraph: Define vertex labels
            JanusGraph: implicit schema generation
            JanusGraph: change or update schema elements
            JanusGraph: Static or unmodifiable vertexes
            JanusGraph: vertexes TTL (time to live)
            JanusGraph: Edge TTL
            JanusGraph: property TTL
Previous                                                 Next                                                 Home

Python: Concatenate int and string

There are various ways to concatenate int and string.

 

Approach 1: Converting int to string

str() function is used to convert int to string.

 

Example

msg = 'My age is ';
age = 32

str1 = msg + str(age)

 

Approach 2: Using % operator.

str2 = "%s%s" % (msg, age)

 

Approach 3: Using format function.

str3 = "{}{}".format(msg, age)

 

Approach 4: Using f-strings.

str4 = f'{msg}{age}'

Find the below working application.

 

string_int_concate.py

msg = 'My age is ';
age = 32

str1 = msg + str(age)
print(str1)

str2 = "%s%s" % (msg, age)
print(str2)

str3 = "{}{}".format(msg, age)
print(str3)

str4 = f'{msg}{age}'
print(str4)

Output

$python3 main.py
My age is 32
My age is 32
My age is 32
My age is 32





 

 

 

 

 

Previous                                                    Next                                                    Home

visual studio code: How to change the python version?

Step 1: View -> Command Palette…

 

 

Step 2: Search for the option 'Python: Select Interpreter', this option allow you to choose which version of python to use.





That’s it, you are done….:)

Previous                                                    Next                                                    Home

JanusGraph: Get outgoing and incoming vertices of the edge

'bothVertices()' method return both the outgoing and incoming vertices of this edge.

 

public default Iterator<Vertex> bothVertices()

The first vertex in the iterator is the outgoing vertex. The second vertex in the iterator is the incoming vertex.

 

Example

Iterator<Vertex> vertexIterator = janusGraphEdge.bothVertices();

Vertex outgoingVertex = vertexIterator.next();

Vertex incomingVertex = vertexIterator.next();

 

Find the below working application.

 

EdgeBothVertices.java

package com.sample.app.edges;

import java.util.Iterator;

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

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

		try (JanusGraph 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');

			JanusGraphEdge janusGraphEdge = ramVertex.addEdge("knows", krishnaVertex, "from", 2017, "colleague", "N");

			janusGraph.tx().commit();

			Iterator<Vertex> vertexIterator = janusGraphEdge.bothVertices();

			Vertex outgoingVertex = vertexIterator.next();
			Vertex incomingVertex = vertexIterator.next();

			System.out.println(outgoingVertex.property("name").value() + " knows " + incomingVertex.property("name").value());

		} finally {
			System.out.println("\nDone!!!");
		}

	}
}

Output

Ram knows Krishna

Done!!!



Previous                                                 Next                                                 Home

JanusGraph: get incoming vertex of the edge

‘inVertex’ method return the incoming vertex of the edge.

 

Example

JanusGraphVertex janusGraphVertex = janusGraphEdge.inVertex();

 

Find the below working application.

 

EdgeIncomingVertex.java

package com.sample.app.edges;

import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphEdge;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;

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

		try (JanusGraph 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');

			JanusGraphEdge janusGraphEdge = ramVertex.addEdge("knows", krishnaVertex, "from", 2017, "colleague", "N");

			janusGraph.tx().commit();

			JanusGraphVertex janusGraphVertex = janusGraphEdge.inVertex();
			System.out.println("Incoming vertex is : " + janusGraphVertex.property("name").value());

		} finally {
			System.out.println("\nDone!!!");
		}

	}
}

Output

Incoming vertex is : Krishna

Done!!!



Previous                                                 Next                                                 Home

Sunday, 6 February 2022

JanusGraph: Get outgoing vertex of this edge

‘outVertex’ method return the outgoing/tail vertex of this edge.

 

Example

JanusGraphVertex janusGraphVertex = janusGraphEdge.outVertex();

 

EdgeOutgoingVertex.java

package com.sample.app.edges;

import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphEdge;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;

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

		try (JanusGraph 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');

			JanusGraphEdge janusGraphEdge = ramVertex.addEdge("knows", krishnaVertex, "from", 2017, "colleague", "N");

			janusGraph.tx().commit();

			JanusGraphVertex janusGraphVertex = janusGraphEdge.outVertex();
			System.out.println("Outgoing vertex is : " + janusGraphVertex.property("name").value());

		} finally {
			System.out.println("\nDone!!!");
		}

	}
}

Output

Outgoing vertex is : Ram

Done!!!



 

Previous                                                 Next                                                 Home

JanusGraph: Get all the vertices associated with the edge

'vertices' method return all the vertices associated with this edge.

 

public Iterator<Vertex> vertices(final Direction direction)

Argument 'direction' is used to get the incoming vertex, outgoing vertex, or both vertices.

 

If the direction is Direction#BOTH then the iterator order is: Direction#OUT then Direction#IN.

 

Example

Iterator<Vertex> vertices = janusGraphEdge.vertices(Direction.BOTH);

 

Find the below working application.

 

GetEdgeVertices.java

package com.sample.app.edges;

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

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

		try (JanusGraph 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');

			JanusGraphEdge janusGraphEdge = ramVertex.addEdge("knows", krishnaVertex, "from", 2017, "colleague", "N");

			janusGraph.tx().commit();

			System.out.println("Vertices associated with the edge");
			Iterator<Vertex> vertices = janusGraphEdge.vertices(Direction.BOTH);
			while (vertices.hasNext()) {
				Vertex vertex = vertices.next();
				System.out.println("\t"+ vertex.property("name").value());
			}

		} finally {
			System.out.println("\nDone!!!");
		}

	}
}

 

Output

Vertices associated with the edge
	Ram
	Krishna

Done!!!

 

 

 

Previous                                                 Next                                                 Home