This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
Monday, 7 February 2022
JanusGraph: Schema and data modelling
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
visual studio code: How to change the python version?
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!!!
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!!!
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!!!
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!!!