pieter
pieter
ATApache TinkerPop
Created by pieter on 11/16/2024 in #questions
Using java/gremlin inside python with Jpype!
I recently experimented with using Jpype to give the python world at my day job access to Sqlg. It seems a very easy and powerful way to give python code full access to the any java api. In my case I am making SqgGraph available to python. It is about 5 lines of setup code and voila, the python code has the same functionality as native java. Does anyone use Jpype, anything caveats I should know about?
1 replies
ATApache TinkerPop
Created by pieter on 10/18/2024 in #questions
select T.id + optional properties
I am trying to work out how to select vertex id and some optional properties. select().by does not work as it filters out not productive properties. Here is the sample graph I am testing this on.
TinkerGraph graph = TinkerGraph.open();
Vertex a = graph.addVertex(T.label, "A", "prop1", "aaaa");
Vertex b1 = graph.addVertex(T.label, "B", "prop2", "bbbb");
Vertex b2 = graph.addVertex(T.label, "B");
a.addEdge("ab", b1);
a.addEdge("ab", b2);

List<Map<String, Vertex>> result1 = graph.traversal().V().hasLabel("A").as("a")
.out("ab").as("b")
.<Vertex>select("a", "b").by("prop1").by("prop2")
.toList();
for (Map<String, Vertex> objectObjectMap : result1) {
System.out.println(objectObjectMap);
}
List<Map<Object, Object>> result2 = graph.traversal().V().hasLabel("A").as("a")
.out("ab").as("b")
.select("a", "b")
.elementMap(T.id.getAccessor(), "prop1", "prop2")
.toList();
for (Map<Object, Object> objectObjectMap : result2) {
System.out.println(objectObjectMap);
}
TinkerGraph graph = TinkerGraph.open();
Vertex a = graph.addVertex(T.label, "A", "prop1", "aaaa");
Vertex b1 = graph.addVertex(T.label, "B", "prop2", "bbbb");
Vertex b2 = graph.addVertex(T.label, "B");
a.addEdge("ab", b1);
a.addEdge("ab", b2);

List<Map<String, Vertex>> result1 = graph.traversal().V().hasLabel("A").as("a")
.out("ab").as("b")
.<Vertex>select("a", "b").by("prop1").by("prop2")
.toList();
for (Map<String, Vertex> objectObjectMap : result1) {
System.out.println(objectObjectMap);
}
List<Map<Object, Object>> result2 = graph.traversal().V().hasLabel("A").as("a")
.out("ab").as("b")
.select("a", "b")
.elementMap(T.id.getAccessor(), "prop1", "prop2")
.toList();
for (Map<Object, Object> objectObjectMap : result2) {
System.out.println(objectObjectMap);
}
The first gremlin produces one row as it filters out the 'B' with no 'prop2'
{a=aaaa, b=bbbb}
{a=aaaa, b=bbbb}
The second gremlin crashes with
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class org.apache.tinkerpop.gremlin.structure.Elemen
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class org.apache.tinkerpop.gremlin.structure.Elemen
I am looking to retrieve A.id, prop1, B.id, prop2 where prop1 and prop2 to are optional fields.
5 replies
ATApache TinkerPop
Created by pieter on 11/17/2023 in #questions
search for vertices where multiple properties
I need to search for vertices where multiple properties are a certain value. Here is what I am able to come up with.
try (TinkerGraph graph = TinkerGraph.open()) {
Vertex person1 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Smith", "id", 1);
Vertex person2 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Do", "id", 2);
Vertex person3 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Ho", "id", 3);
Vertex person4 = graph.addVertex(T.label, "Person", "name", "Jo", "surname", "Do", "id", 4);

List<Vertex> persons = graph.traversal().V().hasLabel("Person")
.has("name", P.within("John", "Jo"))
.has("surname", P.within("Smith", "Do"))
.toList();

System.out.println(persons.stream().map(v -> "[" + v.value("name") + " " + v.value("surname") + "]").reduce((a,b) -> a + "," + b).orElse(""));

persons = graph.traversal().V().hasLabel("Person").as("person")
.values("name").concat(__.select("person").values("surname"))
.filter(t -> Arrays.asList("JohnSmith", "JoDo").contains(t.get()))
.<Vertex>select("person")
.toList();
System.out.println(persons.stream().map(v -> "[" + v.value("name") + " " + v.value("surname") + "]").reduce((a,b) -> a + "," + b).orElse(""));
}
try (TinkerGraph graph = TinkerGraph.open()) {
Vertex person1 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Smith", "id", 1);
Vertex person2 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Do", "id", 2);
Vertex person3 = graph.addVertex(T.label, "Person", "name", "John", "surname", "Ho", "id", 3);
Vertex person4 = graph.addVertex(T.label, "Person", "name", "Jo", "surname", "Do", "id", 4);

List<Vertex> persons = graph.traversal().V().hasLabel("Person")
.has("name", P.within("John", "Jo"))
.has("surname", P.within("Smith", "Do"))
.toList();

System.out.println(persons.stream().map(v -> "[" + v.value("name") + " " + v.value("surname") + "]").reduce((a,b) -> a + "," + b).orElse(""));

persons = graph.traversal().V().hasLabel("Person").as("person")
.values("name").concat(__.select("person").values("surname"))
.filter(t -> Arrays.asList("JohnSmith", "JoDo").contains(t.get()))
.<Vertex>select("person")
.toList();
System.out.println(persons.stream().map(v -> "[" + v.value("name") + " " + v.value("surname") + "]").reduce((a,b) -> a + "," + b).orElse(""));
}
This will print out,
[John Smith],[John Do],[Jo Do]
[John Smith],[Jo Do]
[John Smith],[John Do],[Jo Do]
[John Smith],[Jo Do]
The second query is the results I am looking for, but is there a better way to do this? Using concat and filter is hard to optimize.
6 replies