home shape

Arangodb Java Driver and Graphs – Part 2

After defining a graph and filling it with some vertices and edges (see part 1), the time has come to retrieve information out of the graph.

Please take a look at the defined graph operations of ArangoDB. These will be the base for our next examples. (Yes, there may be other ways to get the results, this post does not claim completeness!)

We will start with some easy stuff and then smoothly advance in complexity.

Question: “How many edges are defined within the graph?”

Answer:

First we have to define an AQL-query:

String query = "for i in GRAPH_EDGES('Academical', null) return i";

In our example we only have “plain” edges, that means no additional information was stored with the edges. Thus we expect a set of com.arangodb.entity.PlainEdgeEntity as result.

CursorEntity<PlainEdgeEntity> result = arangoDriver.executeQuery(query, null, PlainEdgeEntity.class, true, 10);

result now contains maximum 10 edges (defined in the last parameter). With the 3rd parameter set to true, the number of results of the query is calculated. So this is the number of edges:

System.out.println("The graph has " + result.getCount() + " edges.");

Question: “How do I get the number of publications of a given person?”

Answer:

We use the same function, but provide some options and a vertex example. In this case we take person2 as our person of interest (in our example: “Floyd”).

String query = "for i in GRAPH_EDGES(@graphName, @vertexId, {direction: 'outbound', edgeCollectionRestriction: 'HasWritten'}) return i";
Map<String, Object> bindVars = new MapBuilder().put("graphName", "Academical").put("vertexId", person2.getDocumentHandle()).get();
CursorEntity<PlainEdgeEntity> result = arangoDriver.executeQuery(query, bindVars, PlainEdgeEntity.class, true, 10);
System.out.println(person2.getEntity().getName() + " has written " + result.getCount() + " publications.");

As before, the first parameter of the AQL-function GRAPH_EDGES is the name of the database. The second is the example vertex. We use one specific vertex (“Floyd”), so in our case it’s sufficient to provide the ID of this vertex. Least the options are defined: We only want to get the outgoing edges of our vertex (defined with direction) and all edges have to be stored in the collection defined with edgeCollectionRestriction.

That vertex example was easy. What about a more unspecific example? Maybe you want to get all publications of persons which have a “master of arts” title. Let’s add another person with a publication to the database.

DocumentEntity<Person> person3 = arangoDriver.graphCreateVertex("Academical", "Person", new Person("Zoot", "master of arts"), true);
DocumentEntity<Publication> publication4 = arangoDriver.graphCreateVertex("Academical", "Publication", new Publication("Ship arriving too late", "7-081-5", 60), true);
arangoDriver.graphCreateEdge("Academical", "HasWritten", null, person3.getDocumentHandle(), publication4.getDocumentHandle());

Now there are two ways of creating a vertex example (in this case): Use an object of the class Person or create a Map. Let’s do both.

First the object: As the class Person only consists of non primitive datatypes (two attributes of type String) create an object with name set to null

Person exampleVertexObject = new Person(null, "master of arts");

Object will be serialized, so the first parameter (name) will not be used as filter criteria.

Second the map:

Map<String, Object> exampleVertexMap = new MapBuilder().put("title", "master of arts").get();

The results of the following two queries are equal:

String queryObject = "for i in GRAPH_EDGES(@graphName, @vertexExample, {direction: 'outbound', edgeCollectionRestriction: 'HasWritten'}) return i";
Map<String, Object> bindVars = new MapBuilder().put("graphName", "Academical").put("vertexExample", exampleVertexObject).get();

String queryMap = "for i in GRAPH_EDGES(@graphName, @vertexExample, {direction: 'outbound', edgeCollectionRestriction: 'HasWritten'}) return i";
Map<String, Object> bindVars = new MapBuilder().put("graphName", "Academical").put("vertexExample", exampleVertexMap).get();

Question: How do I get the titles of all publications of a specific person?

Answer:

As result we expect a set of Publication. We know that persons and publications are connected via edges in “HasWritten”. So they are direct neighbors. A possible query might look like the following:

String query = "for i in GRAPH_NEIGHBORS('Academical', {_id:@startVertexId}, {endVertexCollectionRestriction : 'Publication'}) return i.vertex";
Map<String, Object> bindVars = new MapBuilder().put("startVertexId", person2.getDocumentHandle()).get();
CursorEntity<Publication> result = arangoDriver.executeQuery(query, bindVars, Publication.class, true, 20);
    System.out.println(person2.getEntity().getName() + " has written the following publications:");
    Iterator iterator = result.iterator();
    while(iterator.hasNext()) {
        Publication publication = (Publication) iterator.next();
        System.out.println("- " + publication.getTitle());
    }

Julie Ferrario

3 Comments

  1. Jaiganesh Vazhkudai on March 19, 2016 at 11:36 pm

    Why is the graph view of the collection not showing the edge label? (For example ‘Knows’) this doesn’t happen in Java driver for me.. i am using 2.7.2

    • Wilfried Gösgens on April 12, 2016 at 10:13 am

      for the graph viewer you have to configure the which attribute is to be displayed as the label.

      Click the tiny gear in the upper right and there you can configure the Edge and Vertex labels.

    • Wilfried Gösgens on April 12, 2016 at 10:35 am

      In the graph viewer you have to choose the attribute that will be displayed as label.

      Click on the gear in the upper right, then you will be able to choose the attribute that becomes displayed as label for edges and vertices.

Leave a Comment





Get the latest tutorials, blog posts and news: