ArangoDB Java Driver: Graph Data Manipulation & Queries
With ArangoDB 2.2 the new graph API was released featuring multi collection graphs (see blog). With the new version (2.2.1) of arangodb-java-driver the new graph API is supported. In the following you can find a small example of creating a graph with Java.
For the import via maven and configuring the driver, please read the Basics and Driver Setup. For the following we assume, that arangodbDriver
is a configured instance of the driver.
So let’s start the whole thing…
In the database we need a graph containing collections for the vertices and edges (defined in edge definitions).
// Edge definitions of the graph
List<EdgeDefinitionEntity> edgeDefinitions = new ArrayList<EdgeDefinitionEntity>();
// We start with one edge definition:
EdgeDefinitionEntity edgeDefHasWritten = new EdgeDefinitionEntity();
// Define the edge collection...
edgeDefHasWritten.setCollection("HasWritten");
// ... and the vertex collection(s) where an edge starts...
List<String> from = new ArrayList<String>();
from.add("Person");
edgeDefHasWritten.setFrom(from);
// ... and ends.
List<String> to = new ArrayList<String>();
to.add("Publication");
edgeDefHasWritten.setTo(to);
// add the edge definition to the list
edgeDefinitions.add(edgeDefHasWritten);
// We do not need any orphan collections, so this is just an empty list
List<String> orphanCollections = new ArrayList<String>();
// Create the graph:
GraphEntity graphAcademical = arangoDriver.createGraph("Academical", edgeDefinitions, orphanCollections, true);
Now we have a graph Academical
where edges are defined from vertex collection Person
to vertex collection Publication
and stored in edge collection HasWritten
. Collections will be created automatically if they do not already exist. Let’s add another edge definition to the graph:
EdgeDefinitionEntity edgeDefHasCited = new EdgeDefinitionEntity();
edgeDefHasCited.setCollection("HasCited");
from.clear();
from.add("Publication");
edgeDefHasCited.setFrom(from);
to.clear();
to.add("Publication");
edgeDefHasCited.setTo(to);
// add the new definition to the existing graph:
arangoDriver.graphCreateEdgeDefinition(graphAcademical.getName(), edgeDefHasCited);
In this second part only the collection HasCited
is created, the collection Publication
already exists and was used automatically.
Let us play a little with our graph. A first step would be, to fill it with vertices and edges. Therefore we should define, which information a vertex contains. So let’s define some classes for the different vertices:
class Person {
String name;
String title;
public Person(String name, String title) {
this.name = name;
this.title = title;
}
// getter and setter
...
}
class Publication {
String title;
String isbn;
int pages;
public Publication(String title, String isbn, int pages) {
this.title = title;
this.isbn = isbn;
this.pages = pages;
}
// getter and setter
...
}
Fill the graph with vertices…
DocumentEntity<Person> person1 = arangoDriver.graphCreateVertex("Academical", "Person", new Person("Bob", "Dr"), true);
DocumentEntity<Person> person2 = arangoDriver.graphCreateVertex("Academical", "Person", new Person("Floyd", "master of arts"), true);
DocumentEntity<Publication> publication1 = arangoDriver.graphCreateVertex("Academical", "Publication", new Publication("Surgery for dummies", "1-234-1", 42), true);
DocumentEntity<Publication> publication2 = arangoDriver.graphCreateVertex("Academical", "Publication", new Publication("Relaxing while working", "5-678-x", 815), true);
DocumentEntity<Publication> publication3 = arangoDriver.graphCreateVertex("Academical", "Publication", new Publication("Infrasound in art and science", "7-081-5", 60), true);
… and edges:
arangoDriver.graphCreateEdge("Academical", "HasWritten", null, person1.getDocumentHandle(), publication1.getDocumentHandle());
arangoDriver.graphCreateEdge("Academical", "HasWritten", null, person2.getDocumentHandle(), publication2.getDocumentHandle());
arangoDriver.graphCreateEdge("Academical", "HasWritten", null, person2.getDocumentHandle(), publication3.getDocumentHandle());
arangoDriver.graphCreateEdge("Academical", "HasCited", null, publication1.getDocumentHandle(), publication3.getDocumentHandle());
arangoDriver.graphCreateEdge("Academical", "HasCited", null, publication1.getDocumentHandle(), publication2.getDocumentHandle());
Click here for part 2.
Get the latest tutorials, blog posts and news: