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);
}


The first gremlin produces one row as it filters out the 'B' with no 'prop2'
{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


I am looking to retrieve A.id, prop1, B.id, prop2 where prop1 and prop2 to are optional fields.
Was this page helpful?