How to add a bunch of vertices and edges in a single traversal and return a subset of those using DSE Graph:

358 views Asked by At

Goal: I am trying to add a bunch of vertices and edges in a single traversal and return a subset of those.

It might be that the fluent API is still in Beta or that I haven't understood the API properly:

I think there is a problem with .select() and GraphResultSet one() and all() but here is what I have tried:

GraphTraversal<Vertex,Vertex> traversal = g.addV("Entity").property("uuid","testuuid").as("a")
            .addV("Entity").property("uuid","testuuid3").as("b");
            GraphStatement graphStatement =  DseGraph.statementFromTraversal(traversal.select("a","b"));
            GraphResultSet grs = dseSession.executeGraph(graphStatement.setGraphName("graph"));

I am creating two vertices a,b and I select both of them.. if I select only one grs.one().as(Vector.class) works fine... but now that I select both a and b ... grs.one() and grs.all() return both results in a list [] and in an obj {} .. iterator() doesn't work properly since grs.all().count() is 1.. so I can't get what I need .. assuming that grs.all() returns an iterator for two things and grs.one() returns the first thing.

grs.all() returns something like that:

[{a=DefaultVertex{id={~label=Entity, community_id=903802240, member_id=515}, label=Entity, properties=com.google.common.collect.Iterators$3@3670f00}, b=DefaultVertex{id={~label=Entity, community_id=903802240, member_id=516}, label=Entity, properties=com.google.common.collect.Iterators$3@452e26d0}}]

grs.one() returns something like that:

{a=DefaultVertex{id={~label=Entity, community_id=1220980480, member_id=514}, label=Entity, properties=com.google.common.collect.Iterators$3@10b892d5}, b=DefaultVertex{id={~label=Entity, community_id=1220980480, member_id=515}, label=Entity, properties=com.google.common.collect.Iterators$3@3d3f761a}}

Edit (trying to iterate over multiple things in the same row (see comments):

   GraphNode ga = grs.all().iterator().next(); // this contains both a and b 
   System.out.println(ga.getClass()); // this is ObjectGraphNode :/

I am using:

<dependency>
   <groupId>com.datastax.cassandra</groupId>
   <artifactId>dse-driver</artifactId>
   <version>1.1.1-beta1</version>
</dependency>
<dependency>
   <groupId>com.datastax.cassandra</groupId>
   <artifactId>java-dse-graph</artifactId>
   <version>1.0.0-beta1</version>
</dependency>

Any help is appreciated!

1

There are 1 answers

25
Daniel Kuppitz On BEST ANSWER

I'm not sure what your expectation is, but select("a","b") is supposed to return both vertices in a single "row". Both results, for all() and one(), look good to me.

Here's a wild guess; you're probably looking for something like this:

g.addV("Entity").property("uuid","testuuid").union(identity(),
  addV("Entity").property("uuid","testuuid3"))

This traversal would create 2 vertices and return them in 2 separate rows.

UPDATE

After figuring out, in the comments below, that the use-case was to iterate over the map entries returned in a GraphNode (which is a wrapper for any object returned by the DSE Graph API), the answer should be one of the following.

If you only need the first vertex:

executeGraph(statementFromTraversal(
  g.addV("Entity").property("uuid","testuuid").sideEffect(
    addV("Entity").property("uuid","testuuid3"))).one().asVertex()

If you only need the second vertex:

executeGraph(statementFromTraversal(
  g.addV("Entity").property("uuid","testuuid").
    addV("Entity").property("uuid","testuuid3")).one().asVertex()

If you need all / multiple vertices:

GraphNode n = executeGraph(statementFromTraversal(
  g.addV("Entity").property("uuid","testuuid").as("a").
    addV("Entity").property("uuid","testuuid3").as("b").select("a","b")).one()
n.get("a").asVertex() // work w/ vertex a
n.get("b").asVertex() // work w/ vertex b