Handling results

Graph queries return a GraphResultSet, which is essentially an iterable of GraphNode:

GraphStatement graphStatement = DseGraph.statementFromTraversal(DseGraph.traversal().V());
GraphResultSet rs = dseSession.executeGraph(graphStatement);

// Iterating:
for (GraphNode n : rs) {
    System.out.println(n);
}

// Get the first result only (or if you know there is exactly one):
GraphNode n = rs.one();

GraphNode wraps the responses returned by the server. You can coerce the result to a specific type using the asXxx() methods:

GraphStatement graphStatement = DseGraph.statementFromTraversal(
        DseGraph.traversal().
        V().
        count());
GraphNode n = dseSession.executeGraph(graphStatement).one();
System.out.printf("The graph has %s vertices%n", n.asInt());

If the result is an array or an object (non-leaf node), you can iterate its child elements:

if (n.isArray()) {
    for (int i = 0; i < n.size(); i++) {
        GraphNode child = n.get(i);
        System.out.printf("Element at position %d: %s%n", i, child);
    }
}

if (n.isObject()) {
    Iterator<String> fieldNames = n.fieldNames();
    while (fieldNames.hasNext()) {
        String fieldName = fieldNames.next();
        System.out.printf("Element at key %s: %s%n", fieldName, n.get(fieldName));
    }
}

The driver also exposes general-purpose methods to handle results in the form of Maps and Lists:

GraphStatement graphStatement = DseGraph.statementFromTraversal(
        DseGraph.traversal().
        V().
        valueMap());
GraphNode n = dseSession.executeGraph(graphStatement).one();
Map<String, Object> values = n.asMap();

Graph structural types

The driver has client-side representations for Vertex, Edge, Path, VertexProperty, and Property.

These are accessible via the corresponding GraphNode#asXXXX() methods:


GraphTraversalSource g = DseGraph.traversal();

GraphNode n = dseSession.executeGraph(DseGraph.statementFromTraversal(
        g.V().hasLabel("test_vertex")
    )).one();
Vertex vertex = n.asVertex();

n = dseSession.executeGraph(DseGraph.statementFromTraversal(
        g.V().hasLabel("test_vertex").
        outE()
    )).one();
Edge edge = n.asEdge();

n = dseSession.executeGraph(DseGraph.statementFromTraversal(
        g.V().hasLabel("test_vertex").
        outE().
        inV().
        path()
    )).one();

Path path = n.asPath();

n = dseSession.executeGraph(DseGraph.statementFromTraversal(
        g.V().hasLabel("test_vertex").
        properties("name")
    )).one();
// .properties() returns a list of properties, so we get the first one and transform it as a VertexProperty
VertexProperty vertexProperty = n.ge(0).asVertexProperty();

Data types compatibility matrix

DSE Graph exposes several data types when defining a schema for a graph.

Those data types server-side translate into specific Java classes when the data is returned from the server.

Here is the exhaustive list of possible DSE Graph data types, and their corresponding classes in the DSE Java driver:

DSE Graph Java Driver
bigint Long
int Integer
double Double
float Float
uuid UUID
bigdecimal BigDecimal
duration java.time.Duration
inet InetAddress
timestamp java.time.Instant
time java.time.LocalTime
date java.time.LocalDate
smallint Short
varint BigInteger
polygon Polygon
point Point
linestring LineString
blob byte[]

Deserializing complex data types

The driver exposes methods to deserialize data into more complex data types, as long as the server-side data type associated corresponds. Doing so requires using the GraphNode#as(Class<T> clazz) method:

GraphTraversalSource g = DseGraph.traversal();

GraphNode n = dseSession.executeGraph(DseGraph.statementFromTraversal(
        g.V().hasLabel("test_vertex")
    )).one();
Vertex vertex = n.asVertex();
UUID uuidProp = vertex.getProperty("uuidProp").getValue().as(UUID.class);

Java 8 Time types

The DSE Java driver is compatible with Java from version 6. The driver is able to automatically determine whether the application it’s used with is running on such a legacy Java version, and will be able to deserialize objects differently accordingly.

If using the driver with Java version 8 or higher, java.time types will be usable when retrieving the data from a Traversal (see the types matrix above).

If the driver is used with an inferior version of Java, other classes will be usable when retrieving the data sent by the server. For Java versions < 8, the Duration() and Time() DSE Graph types will be exposed as Strings. Timestamp() will be exposed as a java.util.Date, and Date() as a com.datastax.driver.core.LocalDate.

A word on Properties

The vertex properties exposed by the driver (VertexProperty) respect the same behaviour as in Apache TinkerPop. First, a VertexProperty is a property, with a simple value. But in addition to that, a VertexProperty can be the parent of a sublist of Property. If that’s the case, the sub properties of VertexProperty are called meta-properties.

Moreover, in a vertex the same property key can be associated to multiple vertex properties. In this case, the vertex is said to have “multi-properties”.

Here is the syntax for dealing with vertex properties with the DataStax Java driver graph types:

GraphTraversalSource g = DseGraph.traversal();

GraphNode n = dseSession.executeGraph(DseGraph.statementFromTraversal(
        g.V().hasLabel("test_vertex_props")
    )).one();
Vertex vertex = n.asVertex();

// there can be more than one VertexProperty with the key "multi_with_meta_props"
Iterator<VertexProperty> vertexProps = vertex.getProperties("multi_with_meta_props");

VertexProperty vertexProp1 = vertexProps.next();
// the value of the vertex property
int vertexProp1Value = vertexProp1.getValue().asInt();
// the meta-properties of the vertex property
Iterator<Property> metaProps1 = vertexProp1.getProperties();
Property metaProp11 = metaProps1.next();
double metaPropValue11 = metaProp11.getValue().asDouble(); 
Property metaProp12 = metaProps1.next();
double metaPropValue12 = metaProp12.getValue().asDouble(); 

// **multi-properties**.
VertexProperty vertexProp2 = vertexProps.next();
[...]

More on how to create and query multi-valued properties and meta-properties is in the Apache Tinkerpop documentation.