ArangoDB教程:Java 10分钟
这是一个关于如何用Java Sync Driver使用ArangoDB的简短教程。在不到10分钟的时间内,您将学会如何用Java 操作ArangoDB。有关驱动程序的功能和性能的更多细节,请查看相应的博客文章。
如果您使用的是旧版本的ArangoDB,请查看为旧版本设计的Java教程。
安装Java驱动程序
本教程将在Eclipse中介绍Java驱动程序的用法。首先,通过maven
将Java驱动程序添加到您的项目中:
1 2 3 4 5 6 7 8 |
<dependencies> <dependency> <groupId>com.arangodb</groupId> <artifactId>arangodb-java-driver</artifactId> <version>4.2.2</version> </dependency> .... </dependencies> |
使用eclipse,您需要执行以下步骤:
- 首先
file
, 然后new
然后点击other
- 选择
Maven Project
- 选择工作区位置
- 选择默认的原型
- 最后选择一个Group id (
mydb
) 和一个Artifact id (firstproject
) 然后点击完成 - 现在打开
pom.xml
, 在标签dependencies
然后点击添加 - 现在我们填写groupID (
com.arangodb
), artifactID(arangodb-java-driver
) and version(4.2.2
)
快速开始
创建一个名为 FirstProject
的文件并写入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package mydb.firstproject; import java.util.List; import java.util.Map; import java.util.HashMap; import com.arangodb.ArangoCollection; import com.arangodb.ArangoCursor; import com.arangodb.ArangoDB; import com.arangodb.ArangoDBException; import com.arangodb.entity.BaseDocument; import com.arangodb.entity.CollectionEntity; import com.arangodb.model.AqlQueryOptions; import com.arangodb.util.MapBuilder; import com.arangodb.velocypack.VPackSlice; import com.arangodb.velocypack.exception.VPackException; public class FirstProject { public static void main(final String[] args) { } } |
在eclipse中,您需要创建一个名为 FirstProject
的新类,并将代码复制到其中。在本教程的过程中,将使用代码中给出的每个 import
。

连接
配置和打开连接以启动ArangoDB。
1 |
ArangoDB arangoDB = new ArangoDB.Builder().build(); |

创建数据库
我们来创建一个新的数据库:
1 2 3 4 5 6 7 |
String dbName = "mydb"; try { arangoDB.createDatabase(dbName); System.out.println("Database created: " + dbName); } catch (ArangoDBException e) { System.err.println("Failed to create database: " + dbName + "; " + e.getMessage()); } |

结果是应该是:
1 |
Database created: mydb |
创建一个集合
现在让我们来创建我们的第一个集合:
1 2 3 4 5 6 7 |
String collectionName = "firstCollection"; try { CollectionEntity myArangoCollection = arangoDB.db(dbName).createCollection(collectionName); System.out.println("Collection created: " + myArangoCollection.getName()); } catch (ArangoDBException e) { System.err.println("Failed to create collection: " + collectionName + "; " + e.getMessage()); } |

结果是应该是:
1 |
Collection created: firstCollection |
您应该了解的一些细节代码:
createCollection()
创建集合firstCollection
是集合的名字
创建文档
现在我们在集合中创建一个文档。任何对象都可以作为文档添加到数据库中,并作为对象从数据库中检索。
这个例子中,我们使用驱动程序提供的BaseDocument
类。文档的属性存储在映射中,作为键<String>/
值<Object>
对:
1 2 3 4 5 6 7 8 9 10 |
BaseDocument myObject = new BaseDocument(); myObject.setKey("myKey"); myObject.addAttribute("a", "Foo"); myObject.addAttribute("b", 42); try { arangoDB.db(dbName).collection(collectionName).insertDocument(myObject); System.out.println("Document created"); } catch (ArangoDBException e) { System.err.println("Failed to create document. " + e.getMessage()); } |

结果应该是:
1 |
Document created |
您应该了解的一些细节代码:
setKey()
设置新对象的键值addAttribute()
将一个新的键/值对放在对象中- 每个属性作为单个键/值对存储在文档根目录中
阅读文档
阅读创建的文档:
1 2 3 4 5 6 7 8 9 |
try { BaseDocument myDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey", BaseDocument.class); System.out.println("Key: " + myDocument.getKey()); System.out.println("Attribute a: " + myDocument.getAttribute("a")); System.out.println("Attribute b: " + myDocument.getAttribute("b")); } catch (ArangoDBException e) { System.err.println("Failed to get document: myKey; " + e.getMessage()); } |

结果应该是:
1 2 3 |
Key: myKey Attribute a: Foo Attribute b: 42 |
您应该了解的一些细节代码:
getDocument()
将存储的文档数据返回到给定的JavaBean (BaseDocument
)
阅读VelocyPack 格式文档
您也可以阅读VelocyPack 格式文档:
1 2 3 4 5 6 7 8 9 |
try { VPackSlice myDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey", VPackSlice.class); System.out.println("Key: " + myDocument.get("_key").getAsString()); System.out.println("Attribute a: " + myDocument.get("a").getAsString()); System.out.println("Attribute b: " + myDocument.get("b").getAsInt()); } catch (ArangoDBException | VPackException e) { System.err.println("Failed to get document: myKey; " + e.getMessage()); } |

结果应该是:
1 2 3 |
Key: myKey Attribute a: Foo Attribute b: 42 |
您应该了解的一些细节代码:
getDocument()
将存储的文档数据返回到 VelocyPack 格式 (VPackSlice
)
更新文档
1 2 3 4 5 6 |
myObject.addAttribute("c", "Bar"); try { arangoDB.db(dbName).collection(collectionName).updateDocument("myKey", myObject); } catch (ArangoDBException e) { System.err.println("Failed to update document. " + e.getMessage()); } |

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

结果应该是:
1 2 3 4 |
Key: myKey Attribute a: Foo Attribute b: 42 Attribute c: Bar |
删除文档
让我们来删除一个文档:
1 2 3 4 5 |
try { arangoDB.db(dbName).collection(collectionName).deleteDocument("myKey"); } catch (ArangoDBException e) { System.err.println("Failed to delete document. " + e.getMessage()); } |

执行AQL查询
首先我们需要在集合firstCollection
中创建一些名称为Homer
的文档:
1 2 3 4 5 6 7 |
ArangoCollection collection = arangoDB.db(dbName).collection(collectionName); for (int i = 0; i < 10; i++) { BaseDocument value = new BaseDocument(); value.setKey(String.valueOf(i)); value.addAttribute("name", "Homer"); collection.insertDocument(value); } |

从集合firstCollection
中获取所有名称为Homer
的文档,并且遍历存储结果:
1 2 3 4 5 6 7 8 9 10 11 |
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()); } |

结果应该是:
1 2 3 4 5 6 7 8 9 10 |
Key: 1 Key: 0 Key: 5 Key: 3 Key: 4 Key: 9 Key: 2 Key: 7 Key: 8 Key: 6 |
您应该了解的一些细节代码:
- AQL 查询语法使用占位符
@name
其必须被绑定到一个值 query()
执行定义的查询并返回一个具有给定类的ArangoCursor
(这里:BaseDocument
)- 顺序不能保证
使用AQL删除文档
现在我们将删除之前创建的文档:
1 2 3 4 5 6 7 8 9 10 11 12 |
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()); } |

结果应该是:
1 2 3 4 5 6 7 8 9 10 |
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 |
学习更多
- 查看AQL 文档,进一步学习我们的查询语言。
- 想更多地了解我们的数据库吗点击这里!
- 阅读关于Collections的更多信息。
- 在我们的文档中浏览更多关于Documents的信息。
- 相关更多示例,您可以浏览ArangoDB 菜谱。
This page has been translated from the original English version with great caution.
However, it might still contain mistakes. Please refer to the English version for binding information.