How to use the database of graph, document and key / value models flexibly? –Java 10 minute tutorial

Time:2020-10-15

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 (arangodb on GitHub) in Java. For more details on the functionality and performance of the driver, check out the corresponding blog posts.
*Please note: this tutorial is written for arangodb version 3.1 and may not support older versions.
If you are using an older version of arangodb, check out the Java tutorials designed for the older version. *

Install Java driver
This tutorial introduces the use of Java drivers in eclipse. First, throughmavenAdd a Java driver to your project:

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>

To use eclipse, you need to perform the following steps:

firstfile, and thennewThen clickother
choiceMaven Project
Select workspace location
Select the default prototype
Finally, select a group ID(mydb)And an artifact ID(firstProject)Then click finish
Now open itpom.xml, in the labeldependenciesThen click Add
Now let’s fill in the groupid(com.arangodb), artifactID(arangodb-java-driver) and version(4.0.0)

Note: make sure that arangodb 3.1 or later is installed and running.

Quick start

Create aFirstProjectAnd write to:

   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 aFirstProjectAnd copy the code to it. Throughout the course of this tutorial, you will use each of theimport

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

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.

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

Create 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());
    }

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

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());
  }
  

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

The result should be:

1 Collection created: firstCollection

Some detailed codes you should know:

  • createCollection()Create collection

  • firstCollectionIt’s 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 theBaseDocumentClass. The properties of the document are stored in the map asKey < string > / value < Object >yes:

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());

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

The results should be:

1 Document created

Some detailed codes you should know:

  • setKey()Set the key value of the new object

  • addAttribute()Place a new key / value pair in the object

  • Each attribute is stored in the document root as a single key / value pair

Read the 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());
   }

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

The results should be:

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

Some detailed codes you should know:

getDocument()Returns the stored document data to the givenJavaBean (BaseDocument)

Read velocypack format document

You can also read the velocypack format documentation:

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());
}

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

Some detailed codes you should know:

getDocument()Returns the 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());
}

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

Read the document again

We 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());
  }

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

The results 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());
}

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

Execute AQL query

First of all, we need to assemble in thefirstCollectionCreate someHomerDocument for:

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);
 }

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

From setfirstCollectionAll the names areHomerAnd 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());
}

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

The results should be:

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

Some detailed codes you should know:

  • AQL query syntax uses placeholders@nameIt must be bound to a value

  • query()Executes the defined query and returns aArangoCursor(here:BaseDocument)

  • Sequence is not guaranteed

Deleting documents using AQL

Now we’re going to delete the previously created document:

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());
}

How to use the database of graph, document and key / value models flexibly? --Java 10 minute tutorial

The results 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

  • Check the AQL documentation to learn more about our query language.

  • Want to learn more about our database? Click here!

  • Read more about collections.

  • Browse more about documents in our documents.

  • For more examples, you can browse the arangodb menu.

Recommended Today

Blog based on beego, go blog

Go Blog A beego based development, can quickly create personal blog, CMS system Include functions see Official website of go bloggo-blog.cn Demo siteleechan.online Update log time function January 23, 2020 New top post function February 2, 2020 New custom navigation function February 4, 2020 New site announcement function February 6, 2020 New link module February […]