ArangoDB教程:Java 10分钟
这是一个关于如何用Java Driver使用ArangoDB的简短教程。在不到10分钟的时间内,您可以学习如何在Maven和Gradle项目中使用ArangoDB Java驱动程序。
项目配置
要使用ArangoDB Java驱动程序,您需要将两个库导入到您的项目中:
- arangodb-java-driver: 司机本人
- jackson-dataformat-velocypack: 一种数据格式后端实现,支持对Jackson数据绑定API的VelocyPack支持
在Maven项目中,您需要将以下依赖项添加到pom.xml
中:
<dependencies> <dependency> <groupId>com.arangodb</groupId> <artifactId>arangodb-java-driver</artifactId> <version>...</version> </dependency> <dependency> <groupId>com.arangodb</groupId> <artifactId>jackson-dataformat-velocypack</artifactId> <version>...</version> </dependency> </dependencies>
在Gradle项目中,您将在build.gradle
中添加以下内容:
dependencies { implementation 'com.arangodb:arangodb-java-driver:...' implementation 'com.arangodb:jackson-dataformat-velocypack:...' }
连接
配置和打开连接以启动ArangoDB。
ArangoDB arangoDB = new ArangoDB.Builder() .serializer(new ArangoJack()) .build();
提示: 原始连接为 http://127.0.0.1:8529.
创建数据库
我们来创建一个新的数据库:
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()); }
结果应该是:
Database created: mydb
创建一个集合
现在让我们来创建我们的第一个集合:
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()); } }
结果应该是:
Collection created: firstCollection
创建文档
现在我们在集合中创建一个文档。任何对象都可以作为文档添加到数据库中,并作为对象从数据库中检索。
这个例子中,我们使用驱动程序提供的BaseDocument
类。文档的属性存储在映射中,作为键<String>/
值<Object>
对:
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()); }
结果应该是:
Document created
您应该了解的一些细节代码:
setKey()
设置新对象的键值addAttribute()
将一个新的键/值对放在对象中
阅读文档
阅读创建的文档:
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()); }
结果应该是:
Key: myKey Attribute a: Foo Attribute b: 42/pre>
您应该了解的一些细节代码:
getDocument()
将存储的文档数据返回到给定的JavaBean (BaseDocument
)
阅读VelocyPack 格式文档
您还可以将文件阅读为Jackson JsonNode
:
try { ObjectNode myDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey", ObjectNode.class); System.out.println("Key: " + myDocument.get("_key").textValue()); System.out.println("Attribute a: " + myDocument.get("a").textValue()); System.out.println("Attribute b: " + myDocument.get("b").intValue()); } catch (ArangoDBException e) { System.err.println("Failed to get document: myKey; " + e.getMessage()); }
结果应该是:
Key: myKey Attribute a: Foo Attribute b: 42/pre>
您应该了解的一些细节代码:
getDocument()
将存储的文档数据返回到 VelocyPack 格式 (VPackSlice
)
更新文档
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()); }
再次阅读文件
我们再次阅读文件:
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()); }
结果应该是:
Key: myKey Attribute a: Foo Attribute b: 42 Attribute c: Bar
删除文档
让我们来删除一个文档:
try { arangoDB.db(dbName).collection(collectionName).deleteDocument("myKey"); } catch (ArangoDBException e) { System.err.println("Failed to delete document. " + e.getMessage()); }
执行AQL查询
首先我们需要在集合firstCollection
中创建一些名称为Homer
的文档:
First we need to create some documents with the name Homer
in collection firstCollection
:
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
的文档,并且遍历存储结果:
try { String query = "FOR t IN firstCollection FILTER t.name == @name RETURN t"; MapbindVars = Collections.singletonMap("name", "Homer"); ArangoCursor 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()); }
结果应该是:
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删除文档
现在我们将删除之前创建的文档:
try { String query = "FOR t IN firstCollection FILTER t.name == @name " + "REMOVE t IN firstCollection LET removed = OLD RETURN removed"; MapbindVars = Collections.singletonMap("name", "Homer"); ArangoCursor 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()); }
结果应该是:
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的信息。
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.