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.
2 Replies
spmallette
spmallette3mo ago
there's a number of ways you might try to solve that. how about just using coalesce() in the by():
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().as('a').out().as('b').select('a','b').by('name').by('age')
==>[a:marko,b:27]
==>[a:marko,b:32]
gremlin> g.V().as('a').out().as('b').select('a','b').by('name').by(coalesce(values('age'), constant(-1)))
==>[a:marko,b:-1]
==>[a:marko,b:27]
==>[a:marko,b:32]
==>[a:josh,b:-1]
==>[a:josh,b:-1]
==>[a:peter,b:-1]
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().as('a').out().as('b').select('a','b').by('name').by('age')
==>[a:marko,b:27]
==>[a:marko,b:32]
gremlin> g.V().as('a').out().as('b').select('a','b').by('name').by(coalesce(values('age'), constant(-1)))
==>[a:marko,b:-1]
==>[a:marko,b:27]
==>[a:marko,b:32]
==>[a:josh,b:-1]
==>[a:josh,b:-1]
==>[a:peter,b:-1]
the second one fails because you did select(...).elementMap(...) which isn't possible because elementMap() can't work on the Map that select() produces.
pieter
pieterOP3mo ago
Thanks, I'll try coalesce. This is what I ended up going with,
List<Map<String, Object>> result = graph.traversal().V().hasLabel("A").as("a")
.out("ab").as("b")
.select("a", "b")
.by(__.elementMap("prop1"))
.by(__.elementMap("prop2"))
.toList();
List<Map<String, Object>> result = graph.traversal().V().hasLabel("A").as("a")
.out("ab").as("b")
.select("a", "b")
.by(__.elementMap("prop1"))
.by(__.elementMap("prop2"))
.toList();
Thanks
Want results from more Discord servers?
Add your server