Having fun with ArangoDB
Besides performance, strong consistency and scalability we also pick NoSQL DBs based on the Design of your Data this is one of the cool things about ArangoDB because it has multi-models.
ArangoDB is a distributed NoSQL database. It has a flexible data model for documents, graphs, and key-values this is also called multi-model db. ArangoDB has convenient SQL-like query language or JavaScript extensions.
In this post i will cover the Document Model and show some have code for such task i will use gradle and Java 8. ArangoDB have drivers for lots of languages you check it out here.
I'm using ArangoDB in Ubuntu Linux, so to install you just do $ sudo apt-get install arangodb. If you are using another OS or Docker for instance is fine you check it out here as well.
Build.gradle
So first of all lets see the build.gradle file, here we are just adding ArangoDB Java Driver dependency. So it's very straightforward. Once you do this you can run $ gradle build and $ gradle eclipse to generate the eclipse project. Now let's see some Java code.
Java Driver
Now we will create a very simple DB and query a document using the Java Driver.
First we connect on the server running on localhost on port 8529. Next step is create a Database called db1. Moving forward there is a collection being created called myCollection in this collection there are several documents being add, as you can see us the Simpsons Family. To finish there is query and a for loop to show all results.
You can then run $ gradle run . Full project is available in my GitHub.
That`s it :-)
ArangoDB is a distributed NoSQL database. It has a flexible data model for documents, graphs, and key-values this is also called multi-model db. ArangoDB has convenient SQL-like query language or JavaScript extensions.
In this post i will cover the Document Model and show some have code for such task i will use gradle and Java 8. ArangoDB have drivers for lots of languages you check it out here.
I'm using ArangoDB in Ubuntu Linux, so to install you just do $ sudo apt-get install arangodb. If you are using another OS or Docker for instance is fine you check it out here as well.
Build.gradle
So first of all lets see the build.gradle file, here we are just adding ArangoDB Java Driver dependency. So it's very straightforward. Once you do this you can run $ gradle build and $ gradle eclipse to generate the eclipse project. Now let's see some Java code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
apply plugin: 'eclipse' | |
apply plugin: 'application' | |
mainClassName = "com.github.diegopacheco.sandbox.java.arrangodb.MainApp" | |
sourceCompatibility = 1.8 | |
targetCompatibility = 1.8 | |
repositories { | |
mavenLocal() | |
mavenCentral() | |
maven { | |
url "https://oss.sonatype.org/content/groups/public/" | |
} | |
maven { | |
url 'http://repo.spring.io/milestone' | |
} | |
} | |
eclipse { | |
classpath { | |
downloadSources=true | |
} | |
} | |
dependencies { | |
compile([ | |
'com.arangodb:arangodb-java-driver:2.7.0' | |
]) | |
} |
Now we will create a very simple DB and query a document using the Java Driver.
First we connect on the server running on localhost on port 8529. Next step is create a Database called db1. Moving forward there is a collection being created called myCollection in this collection there are several documents being add, as you can see us the Simpsons Family. To finish there is query and a for loop to show all results.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.diegopacheco.sandbox.java.arrangodb; | |
import java.util.Iterator; | |
import java.util.Map; | |
import com.arangodb.ArangoConfigure; | |
import com.arangodb.ArangoDriver; | |
import com.arangodb.ArangoHost; | |
import com.arangodb.DocumentCursor; | |
import com.arangodb.entity.CollectionEntity; | |
import com.arangodb.entity.DocumentEntity; | |
import com.arangodb.util.MapBuilder; | |
public class MainApp { | |
public static void main(String[] args) throws Throwable { | |
ArangoConfigure configure = new ArangoConfigure(); | |
configure.setArangoHost(new ArangoHost("127.0.0.1", 8529)); | |
configure.init(); | |
ArangoDriver arangoDriver = new ArangoDriver(configure); | |
try{ | |
arangoDriver.createDatabase("db1"); | |
}catch(Exception e){ | |
System.out.println("DB created already"); | |
} | |
arangoDriver.setDefaultDatabase("db1"); | |
CollectionEntity myArangoCollection = arangoDriver.createCollection("myCollection"); | |
System.out.println(myArangoCollection); | |
try{ | |
MyObject myObject = new MyObject("Homer", 38); | |
DocumentEntity<MyObject> myDocument = arangoDriver.createDocument("myCollection", myObject); | |
System.out.println(myDocument); | |
arangoDriver.createDocument("myCollection", new MyObject("Marge", 36)); | |
arangoDriver.createDocument("myCollection", new MyObject("Bart", 10)); | |
arangoDriver.createDocument("myCollection", new MyObject("Lisa", 8)); | |
arangoDriver.createDocument("myCollection", new MyObject("Maggie", 2)); | |
}catch(Exception e){ | |
System.out.println("Duplicated not adding again..."); | |
} | |
String query = "FOR t IN myCollection FILTER t.age >= @age SORT t.age RETURN t"; | |
Map<String, Object> bindVars = new MapBuilder().put("age", 3).get(); | |
DocumentCursor<MyObject> documentCursor = arangoDriver.executeDocumentQuery( | |
query, bindVars, arangoDriver.getDefaultAqlQueryOptions(), MyObject.class); | |
Iterator<DocumentEntity<MyObject>> iterator = documentCursor.iterator(); | |
while (iterator.hasNext()) { | |
DocumentEntity<MyObject> documentEntity = iterator.next(); | |
MyObject obj = documentEntity.getEntity(); | |
System.out.println(obj.getName()); | |
} | |
} | |
} |
You can then run $ gradle run . Full project is available in my GitHub.
That`s it :-)