Skip to content

corneil/arangodb-java-driver

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ArangoDB-Logo

arangodb-java-driver

2.7: Build Status 3.0: Build Status 3.1: Build Status 4.1: Build Status 4.2: Build Status master: Build Status

Maven Central

Supported versions

arangodb-java-driverArangoDBnetwork protocolJava version
4.2.x3.0.x, 3.1.x, 3.2.xVelocyStream, HTTP1.6+
4.1.x3.1.x, 3.2.xVelocyStream1.6+
3.1.x3.1.x, 3.2.xHTTP1.6+
3.0.x3.0.xHTTP1.6+
2.7.42.7.x, 2.8.xHTTP1.6+

Note: VelocyStream is only supported in ArangoDB 3.1 and above.

Maven

To add the driver to your project with maven, add the following code to your pom.xml (please use a driver with a version number compatible to your ArangoDB server's version):

ArangoDB 3.x.x

<dependencies> <dependency> <groupId>com.arangodb</groupId> <artifactId>arangodb-java-driver</artifactId> <version>4.2.0</version> </dependency> </dependencies>

If you want to test with a snapshot version (e.g. 4.2.0-SNAPSHOT), add the staging repository of oss.sonatype.org to your pom.xml:

<repositories> <repository> <id>arangodb-snapshots</id> <url>https://oss.sonatype.org/content/groups/staging</url> </repository> </repositories>

Compile java driver

mvn clean install -DskipTests=true -Dgpg.skip=true -Dmaven.javadoc.skip=true -B 

Table of Contents

Driver setup

Setup with default configuration, this automatically loads a properties file arangodb.properties if exists in the classpath:

 // this instance is thread-safe ArangoDB arangoDB = new ArangoDB.Builder().build();

The driver is configured with some default values:

property-keydescriptiondefault value
arangodb.hostsArangoDB hosts127.0.0.1:8529
arangodb.timeoutsocket connect timeout(millisecond)0
arangodb.userBasic Authentication User
arangodb.passwordBasic Authentication Password
arangodb.useSsluse SSL connectionfalse
arangodb.chunksizeVelocyStream Chunk content-size(bytes)30000
arangodb.connections.maxmax number of connections1 VST, 20 HTTP
arangodb.protocolused network protocolVST

To customize the configuration the parameters can be changed in the code...

 ArangoDB arangoDB = new ArangoDB.Builder().host("192.168.182.50", 8888).build(); 

... or with a custom properties file (my.properties)

 InputStream in = MyClass.class.getResourceAsStream("my.properties"); ArangoDB arangoDB = new ArangoDB.Builder().loadProperties(in).build(); 

Example for arangodb.properties:

 arangodb.hosts=127.0.0.1:8529,127.0.0.1:8529 arangodb.user=root arangodb.password=

Network protocol

The drivers default used network protocol is the binary protocol VelocyStream which offers the best performance within the driver. To use HTTP, you have to set the configuration useProtocol to Protocol.HTTP_JSON for HTTP with Json content or Protocol.HTTP_VPACK for HTTP with VelocyPack content.

 ArangoDB arangoDB = new ArangoDB.Builder().useProtocol(Protocol.VST).build(); 

In addition to set the configuration for HTTP you have to add the apache httpclient to your classpath.

<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.1</version> </dependency>

Note: If you are using ArangoDB 3.0.x you have to set the protocol to Protocol.HTTP_JSON because it is the only one supported.

SSL

To use SSL, you have to set the configuration useSsl to true and set a SSLContext. (see example code)

 ArangoDB arangoDB = new ArangoDB.Builder().useSsl(true).sslContext(sc).build(); 

Connection Pooling

The driver supports connection pooling for VelocyStream with a default of 1 and HTTP with a default of 20 maximum connections. To change this value use the method maxConnections(Integer) in ArangoDB.Builder.

 ArangoDB arangoDB = new ArangoDB.Builder().maxConnections(8).build();

configure VelocyPack serialization

Since version 4.1.11 you can extend the VelocyPack serialization by registering additional VPackModules on ArangoDB.Builder.

Java 8 types

Added support for:

  • java.time.Instant
  • java.time.LocalDate
  • java.time.LocalDateTime
  • java.util.Optional;
  • java.util.OptionalDouble;
  • java.util.OptionalInt;
  • java.util.OptionalLong;
<dependencies> <dependency> <groupId>com.arangodb</groupId> <artifactId>velocypack-module-jdk8</artifactId> <version>1.0.2</version> </dependency> </dependencies>
ArangoDB arangoDB = new ArangoDB.Builder().registerModule(new VPackJdk8Module()).build();

Scala types

Added support for:

  • scala.Option
  • scala.collection.immutable.List
  • scala.collection.immutable.Map
<dependencies> <dependency> <groupId>com.arangodb</groupId> <artifactId>velocypack-module-scala</artifactId> <version>1.0.0</version> </dependency> </dependencies>
val arangoDB: ArangoDB = new ArangoDB.Builder().registerModule(new VPackScalaModule).build

Joda-Time

Added support for:

  • org.joda.time.DateTime;
  • org.joda.time.Instant;
  • org.joda.time.LocalDate;
  • org.joda.time.LocalDateTime;
<dependencies> <dependency> <groupId>com.arangodb</groupId> <artifactId>velocypack-module-joda</artifactId> <version>1.0.0</version> </dependency> </dependencies>
ArangoDB arangoDB = new ArangoDB.Builder().registerModule(new VPackJodaModule()).build();

custom serializer

 ArangoDB arangoDB = new ArangoDB.Builder().registerModule(new VPackModule() { @Override public <C extends VPackSetupContext<C>> void setup(final C context) { context.registerDeserializer(MyObject.class, new VPackDeserializer<MyObject>() { @Override public MyObject deserialize(VPackSlice parent,VPackSlice vpack, VPackDeserializationContext context) throws VPackException { MyObject obj = new MyObject(); obj.setName(vpack.get("name").getAsString()); return obj; } }); context.registerSerializer(MyObject.class, new VPackSerializer<MyObject>() { @Override public void serialize(VPackBuilder builder,String attribute,MyObject value, VPackSerializationContext context) throws VPackException { builder.add(attribute, ValueType.OBJECT); builder.add("name", value.getName()); builder.close(); } }); } }).build();

Manipulating databases

create database

 // create database  arangoDB.createDatabase("myDatabase"); 

drop database

 // drop database  arangoDB.db("myDatabase").drop(); 

Manipulating collections

create collection

 // create collection arangoDB.db("myDatabase").createCollection("myCollection", null); 

drop collection

 // delete collection  arangoDB.db("myDatabase").collection("myCollection").drop(); 

truncate collection

 arangoDB.db("myDatabase").collection("myCollection").truncate();

Basic Document operations

Every document operations works with POJOs (e.g. MyObject), VelocyPack (VPackSlice) and Json (String).

For the next examples we use a small object:

 public class MyObject { private String key; private String name; private int age; public MyObject(String name, int age) { this(); this.name = name; this.age = age; } public MyObject() { super(); } /*  * + getter and setter  */ } 

insert document

 MyObject myObject = new MyObject("Homer", 38); arangoDB.db("myDatabase").collection("myCollection").insertDocument(myObject); 

When creating a document, the attributes of the object will be stored as key-value pair E.g. in the previous example the object was stored as follows:

 "name" : "Homer" "age" : "38"

delete document

 arangoDB.db("myDatabase").collection("myCollection").deleteDocument(myObject.getKey()); 

update document

 arangoDB.db("myDatabase").collection("myCollection").updateDocument(myObject.getKey(), myUpdatedObject); 

replace document

 arangoDB.db("myDatabase").collection("myCollection").replaceDocument(myObject.getKey(), myObject2); 

read document as JavaBean

 MyObject document = arangoDB.db("myDatabase").collection("myCollection").getDocument(myObject.getKey(), MyObject.class); document.getName(); document.getAge(); 

read document as VelocyPack

 VPackSlice document = arangoDB.db("myDatabase").collection("myCollection").getDocument(myObject.getKey(), VPackSlice.class); document.get("name").getAsString(); document.get("age").getAsInt(); 

read document as Json

 String json = arangoDB.db("myDatabase").collection("myCollection").getDocument(myObject.getKey(), String.class); 

read document by key

 arangoDB.db("myDatabase").collection("myCollection").getDocument("myKey", MyObject.class); 

read document by id

 arangoDB.db("myDatabase").getDocument("myCollection/myKey", MyObject.class); 

Multi Document operations

insert documents

 Collection<MyObject> documents = new ArrayList<>; documents.add(myObject1); documents.add(myObject2); documents.add(myObject3); arangoDB.db("myDatabase").collection("myCollection").insertDocuments(documents); 

delete documents

 Collection<String> keys = new ArrayList<>; keys.add(myObject1.getKey()); keys.add(myObject2.getKey()); keys.add(myObject3.getKey()); arangoDB.db("myDatabase").collection("myCollection").deleteDocuments(keys); 

update documents

 Collection<MyObject> documents = new ArrayList<>; documents.add(myObject1); documents.add(myObject2); documents.add(myObject3); arangoDB.db("myDatabase").collection("myCollection").updateDocuments(documents); 

replace documents

 Collection<MyObject> documents = new ArrayList<>; documents.add(myObject1); documents.add(myObject2); documents.add(myObject3); arangoDB.db("myDatabase").collection("myCollection").replaceDocuments(documents); 

AQL

Executing an AQL statement

Every AQL operations works with POJOs (e.g. MyObject), VelocyPack (VPackSlice) and Json (String).

E.g. get all Simpsons aged 3 or older in ascending order:

 arangoDB.createDatabase("myDatabase"); ArangoDatabase db = arangoDB.db("myDatabase"); db.createCollection("myCollection"); ArangoCollection collection = db.collection("myCollection"); collection.insertDocument(new MyObject("Homer", 38)); collection.insertDocument(new MyObject("Marge", 36)); collection.insertDocument(new MyObject("Bart", 10)); collection.insertDocument(new MyObject("Lisa", 8)); collection.insertDocument(new MyObject("Maggie", 2)); Map<String, Object> bindVars = new HashMap<>(); bindVars.put("age", 3); ArangoCursor<MyObject> cursor = db.query(query, bindVars, null, MyObject.class); for(; cursor.hasNext;) { MyObject obj = cursor.next(); System.out.println(obj.getName()); }

or return the AQL result as VelocyPack:

 ArangoCursor<VPackSlice> cursor = db.query(query, bindVars, null, VPackSlice.class); for(; cursor.hasNext;) { VPackSlice obj = cursor.next(); System.out.println(obj.get("name").getAsString()); }

Note: The parameter type in query() has to match the result of the query, otherwise you get an VPackParserException. E.g. you set type to BaseDocument or a POJO and the query result is an array or simple type, you get an VPackParserException caused by VPackValueTypeException: Expecting type OBJECT.

Graphs

The driver supports the graph api.

Some of the basic graph operations are described in the following:

##add graph A graph consists of vertices and edges (stored in collections). Which collections are used within a graph is defined via edge definitions. A graph can contain more than one edge definition, at least one is needed.

 Collection<EdgeDefinition> edgeDefinitions = new ArrayList<>(); EdgeDefinition edgeDefinition = new EdgeDefinition(); // define the edgeCollection to store the edges edgeDefinition.collection("myEdgeCollection"); // define a set of collections where an edge is going out... edgeDefinition.from("myCollection1", "myCollection2"); // repeat this for the collections where an edge is going into  edgeDefinition.to("myCollection1", "myCollection3"); edgeDefinitions.add(edgeDefinition); // A graph can contain additional vertex collections, defined in the set of orphan collections GraphCreateOptions options = new GraphCreateOptions(); options.orphanCollections("myCollection4", "myCollection5"); // now it's possible to create a graph arangoDB.db("myDatabase").createGraph("myGraph", edgeDefinitions, options); 

delete graph

A graph can be deleted by its name

 arangoDB.db("myDatabase").graph("myGraph").drop();

add vertex

Vertices are stored in the vertex collections defined above.

 MyObject myObject1 = new MyObject("Homer", 38); MyObject myObject2 = new MyObject("Marge", 36); arangoDB.db("myDatabase").graph("myGraph").vertexCollection("collection1").insertVertex(myObject1, null); arangoDB.db("myDatabase").graph("myGraph").vertexCollection("collection3").insertVertex(myObject2, null); 

add edge

Now an edge can be created to set a relation between vertices

 arangoDB.db("myDatabase").graph("myGraph").edgeCollection("myEdgeCollection").insertEdge(myEdgeObject, null); 

Foxx

call a service

 Request request = new Request("mydb", RequestType.GET, "/my/foxx/service") Response response = arangoDB.execute(request); 

User management

If you are using [authentication] (https://docs.arangodb.com/Manual/GettingStarted/Authentication.html) you can manage users with the driver.

add user

 //username, password arangoDB.createUser("myUser", "myPassword");

delete user

 arangoDB.deleteUser("myUser");

list users

 Collection<UserResult> users = arangoDB.getUsers(); for(UserResult user : users) { System.out.println(user.getUser()) }

grant user access

 arangoDB.db("myDatabase").grantAccess("myUser");

revoke user access

 arangoDB.db("myDatabase").revokeAccess("myUser");

Serialization

JavaBeans

The driver can serialize/deserialize JavaBeans. They need at least a constructor without parameter.

 public class MyObject { private String name; private Gender gender; private int age; public MyObject() { super(); } } 

internal fields

To use Arango-internal fields (like _id, _key, _rev, _from, _to) in your JavaBeans, use the annotation DocumentField.

 public class MyObject { @DocumentField(Type.KEY) private String key; private String name; private Gender gender; private int age; public MyObject() { super(); } } 

serialized fieldnames

To use a different serialized name for a field, use the annotation SerializedName.

 public class MyObject { @SerializedName("title") private String name; private Gender gender; private int age; public MyObject() { super(); } } 

ignore fields

To ignore fields at serialization/deserialization, use the annotation Expose

 public class MyObject { @Expose private String name; @Expose(serialize = true, deserialize = false) private Gender gender; private int age; public MyObject() { super(); } } 

custom serializer

 ArangoDB arangoDB = new ArangoDB.Builder().registerModule(new VPackModule() { @Override public <C extends VPackSetupContext<C>> void setup(final C context) { context.registerDeserializer(MyObject.class, new VPackDeserializer<MyObject>() { @Override public MyObject deserialize(VPackSlice parent,VPackSlice vpack, VPackDeserializationContext context) throws VPackException { MyObject obj = new MyObject(); obj.setName(vpack.get("name").getAsString()); return obj; } }); context.registerSerializer(MyObject.class, new VPackSerializer<MyObject>() { @Override public void serialize(VPackBuilder builder,String attribute,MyObject value, VPackSerializationContext context) throws VPackException { builder.add(attribute, ValueType.OBJECT); builder.add("name", value.getName()); builder.close(); } }); } }).build();

manually serialization

To de-/serialize from and to VelocyPack before or after a database call, use the ArangoUtil from the method util() in ArangoDB, ArangoDatabase, ArangoCollection, ArangoGraph, ArangoEdgeCollectionor ArangoVertexCollection.

 ArangoDB arangoDB = new ArangoDB.Builder(); VPackSlice vpack = arangoDB.util().serialize(myObj);
 ArangoDB arangoDB = new ArangoDB.Builder(); MyObject myObj = arangoDB.util().deserialize(vpack, MyObject.class);

Learn more

About

ArangoDB Java driver

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.8%
  • Shell 0.2%