How to use Java operation to integrate the database of Graph, Document, Key/Value? Java 10-minute tutorial

Keywords: Javascript Attribute Java Database Eclipse

This is a short tutorial on how to use ArangoDB with Java Sync Driver 4.1. In less than 10 minutes, you will learn how to operate ArangoDB in Java. For more details about the functions and performance of the driver, please check out the corresponding blog articles.
* Please note that this tutorial is written for ArangoDB version 3.1 and may not support the old version.
If you are using an older version of ArangoDB, check out the Java tutorial designed for the older version. *

Installing Java Driver
This tutorial will introduce the use of Java drivers in Eclipse. First, add the Java driver to your project through maven:

1
2<dependencies>
3  <dependency>
4    <groupId>com.arangodb</groupId>
5    <artifactId>arangodb-java-driver</artifactId>
6    <version>4.1.0</version>
7  </dependency>
8    ....
</dependencies>

With eclipse, you need to perform the following steps:

First file, then new, and then click other
Select Maven Project
Selecting Workspace Location
Choose the default prototype
Finally, select a Group id (mydb) and an Artifact ID (first project) and click Finish.
Now open pom.xml, tag dependencies, and click Add
Now let's fill in group ID (com. arangodb), artifactID (arangodb-java-driver) and version (4.0.0)

Note: Make sure ArangoDB 3.1 or later is installed and running.

Quick start

Create a file named FirstProject and write:

   1 
   2 package mydb.firstproject;
   3  
   4 import java.util.Map;
   5  
   6 import com.arangodb.ArangoCollection;
   7 import com.arangodb.ArangoCursor;
   8 import com.arangodb.ArangoDB;
   9 import com.arangodb.ArangoDBException;
  10 import com.arangodb.entity.BaseDocument;
  11 import com.arangodb.entity.CollectionEntity;
  12 import com.arangodb.util.MapBuilder;
  13 import com.arangodb.velocypack.VPackSlice;
  14 import com.arangodb.velocypack.exception.VPackException;
  15   
  16 public class FirstProject {
  17   public static void main(final String[] args) {
  18   
  19   }
    }
    
    

In eclipse, you need to create a new class called FirstProject and copy the code into it. In the course of this tutorial, each import given in the code will be used.

Connect

Configure and open the connection to start ArangoDB.

1  ArangoDB arangoDB = new ArangoDB.Builder().build();

Tip: The original connection is http://127.0.0.1:8529.

Create a database

Let's create a new database:

1
2   String dbName = "mydb";
3    try {
4     arangoDB.createDatabase(dbName);
5     System.out.println("Database created: " + dbName);
6  
  } catch (ArangoDBException e) {
7      System.err.println("Failed to create database: " + dbName + "; " + e.getMessage());
    }

The result should be:

Database created: mydb


Create a collection

Now let's create our first collection:

1
2 String collectionName = "firstCollection";
3 try {
4  CollectionEntity myArangoCollection = arangoDB.db(dbName).createCollection(collectionName);
5  System.out.println("Collection created: " + myArangoCollection.getName());
6 } catch (ArangoDBException e) {
7  System.err.println("Failed to create collection: " + collectionName + "; " + e.getMessage());
  }
  

The result should be:

1 Collection created: firstCollection

Some details you should know about the code:

  • createCollection() Creates a collection

  • First Collection is the name of the collection

create documents

Now we create a document in the collection. Any object can be added to the database as a document and retrieved from the database as an object.

In this example, we use the BaseDocument class provided by the driver. Document attributes are stored in the mapping as keys < String >/ values < Object > pairs:

1
2 BaseDocument myObject = new BaseDocument();
3 myObject.setKey("myKey");
4 myObject.addAttribute("a", "Foo");
5 myObject.addAttribute("b", 42);
6 try {
7   arangoDB.db(dbName).collection(collectionName).insertDocument(myObject);
8   System.out.println("Document created");
9 } catch (ArangoDBException e) {
10  System.err.println("Failed to create document. " + e.getMessage());

The result should be:

1 Document created

Some details you should know about the code:

Reading document

Read the created document:

  1
  2 try {
  3  BaseDocument myDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey",
  4      BaseDocument.class);
  5  System.out.println("Key: " + myDocument.getKey());
  6  System.out.println("Attribute a: " + myDocument.getAttribute("a"));
  7  System.out.println("Attribute b: " + myDocument.getAttribute("b"));
  8 } catch (ArangoDBException e) {
  9   System.err.println("Failed to get document: myKey; " + e.getMessage());
   }

The result should be:

1
2 Key: myKey
3 Attribute a: Foo
  Attribute b: 42

Some details you should know about the code:

getDocument() returns stored document data to a given JavaBean (BaseDocument)

Read VelocyPack Format Documents

You can also read VelocyPack format documents:

1
2 try {
3   VPackSlice myDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey",
4    VPackSlice.class);
5  System.out.println("Key: " + myDocument.get("_key").getAsString());
6  System.out.println("Attribute a: " + myDocument.get("a").getAsString());
7  System.out.println("Attribute b: " + myDocument.get("b").getAsInt());
8 } catch (ArangoDBException | VPackException e) {
9   System.err.println("Failed to get document: myKey; " + e.getMessage());
}

Some details you should know about the code:

getDocument() returns stored document data to VelocyPack format (VPackSlice)

Update document

1
2 myObject.addAttribute("c", "Bar");
3 try {
4  arangoDB.db(dbName).collection(collectionName).updateDocument("myKey", myObject);
5 } catch (ArangoDBException e) {
6  System.err.println("Failed to update document. " + e.getMessage());
}

Read the document again

Let's read the document again:

1
2 try {
3   BaseDocument myUpdatedDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey",
4      BaseDocument.class);
5   System.out.println("Key: " + myUpdatedDocument.getKey());
6   System.out.println("Attribute a: " + myUpdatedDocument.getAttribute("a"));
7   System.out.println("Attribute b: " + myUpdatedDocument.getAttribute("b"));
8   System.out.println("Attribute c: " + myUpdatedDocument.getAttribute("c"));
9 } catch (ArangoDBException e) {
10  System.err.println("Failed to get document: myKey; " + e.getMessage());
  }

The result should be:

1
2 Key: myKey
3 Attribute a: Foo
4 Attribute b: 42
 Attribute c: Bar 

remove document

Let's delete a document:

1 
2 try {
3   arangoDB.db(dbName).collection(collectionName).deleteDocument("myKey");
4 } catch (ArangoDBException e) {
5   System.err.println("Failed to delete document. " + e.getMessage());
}

Execute AQL queries

First we need to create some documents named Homer in the collection first Collection:

1
2 ArangoCollection collection = arangoDB.db(dbName).collection(collectionName);
3 for (int i = 0; i < 10; i++) {
4   BaseDocument value = new BaseDocument();
5  value.setKey(String.valueOf(i));
6  value.addAttribute("name", "Homer");
7  collection.insertDocument(value);
 }

Get all documents named Homer from the collection first Collection and traverse the stored results:

 
try {
  String query = "FOR t IN firstCollection FILTER t.name == @name RETURN t";
  Map<String, Object> bindVars = new MapBuilder().put("name", "Homer").get();
  ArangoCursor<BaseDocument> cursor = arangoDB.db(dbName).query(query, bindVars, null,
      BaseDocument.class);
  cursor.forEachRemaining(aDocument -> {
    System.out.println("Key: " + aDocument.getKey());
  });
} catch (ArangoDBException e) {
  System.err.println("Failed to execute query. " + e.getMessage());
}

The result should be:

Key: 1
Key: 0
Key: 5
Key: 3
Key: 4
Key: 9
Key: 2
Key: 7
Key: 8
Key: 6 

Some details you should know about the code:

Delete documents using AQL

Now we will delete the document we created before:

try {
  String query = "FOR t IN firstCollection FILTER t.name == @name "
          + "REMOVE t IN firstCollection LET removed = OLD RETURN removed";
  Map<String, Object> bindVars = new MapBuilder().put("name", "Homer").get();
  ArangoCursor<BaseDocument> cursor = arangoDB.db(dbName).query(query, bindVars, null,
      BaseDocument.class);
  cursor.forEachRemaining(aDocument -> {
    System.out.println("Removed document " + aDocument.getKey());
  });
} catch (ArangoDBException e) {
  System.err.println("Failed to execute query. " + e.getMessage());
}

The result should be:

Removed document: 1
Removed document: 0
Removed document: 5
Removed document: 3
Removed document: 4
Removed document: 9
Removed document: 2
Removed document: 7
Removed document: 8
Removed document: 6

Learn more

Posted by karlitosphere on Wed, 12 Dec 2018 04:03:05 -0800