使用Java驱动操作ArangoDB
Posted kongxx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Java驱动操作ArangoDB相关的知识,希望对你有一定的参考价值。
前面说过怎样使用 ArangoDB 的 Web,Shell 和 Restful API 来操作数据库,今天看一下怎样使用Java语言来操作ArangoDB数据库。
首先创建一个Maven工程,添加 ArangoDB 的 Java 驱动库
<dependency>
<groupId>com.arangodb</groupId>
<artifactId>arangodb-java-driver</artifactId>
<version>6.5.0</version>
</dependency>
完整的 pom.xml 文件内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.arangodbstudy</groupId>
<artifactId>arangodbstudy</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>arangodbstudy</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.arangodb</groupId>
<artifactId>arangodb-java-driver</artifactId>
<version>6.5.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
下面写了一个简单的 Java 类,其中包含了一些常用的操作,如:创建/关闭连接,数据库操作,集合操作和文档操作等。
package my.arangodbstudy;
import com.arangodb.ArangoCollection;
import com.arangodb.ArangoDB;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.CollectionEntity;
import com.arangodb.entity.DocumentCreateEntity;
import java.util.Collection;
public class SimpleTest
private static final String DB_HOST = "localhost";
private static final int DB_PORT = 8529;
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "<password>";
private static final String DB_NAME = "mydb";
private static final String COLLECTION_NAME = "users";
public static void main(String[] args)
// 构造ArangoDB实例
ArangoDB arangoDB = new ArangoDB.Builder()
.host(DB_HOST, DB_PORT)
.user(DB_USERNAME)
.password(DB_PASSWORD)
.build();
// 判断数据库存在,如果存在删除
if (arangoDB.db(DB_NAME).exists())
arangoDB.db(DB_NAME).drop();
// 创建数据库
arangoDB.createDatabase(DB_NAME);
System.out.println("arangodb databases: " + arangoDB.getDatabases());
// 获取刚才创建的数据库
ArangoDatabase db = arangoDB.db(DB_NAME);
// 迭代打印数据库中的集合
Collection<CollectionEntity> collectionEntities = db.getCollections();
for (CollectionEntity collectionEntity: collectionEntities)
System.out.println(collectionEntity.getName());
// 创建集合
CollectionEntity collectionEntity = db.createCollection(COLLECTION_NAME);
System.out.println("collection name: " + collectionEntity.getName());
// 获取创建的集合
ArangoCollection collection = db.collection(COLLECTION_NAME);
// 创建文档对象
BaseDocument document = new BaseDocument();
document.addAttribute("name", "user");
document.addAttribute("age", 10);
document.addAttribute("sex", 1);
// 写入数据
DocumentCreateEntity documentCreateEntity = collection.insertDocument(document);
System.out.println("collection count: " + collection.count().getCount());
// 查询
document = collection.getDocument(documentCreateEntity.getKey(), BaseDocument.class);
System.out.println("document: " + document);
// 更新文档
document.updateAttribute("sex", 0);
collection.updateDocument(documentCreateEntity.getKey(), document);
document = collection.getDocument(documentCreateEntity.getKey(), BaseDocument.class);
System.out.println("document: " + document);
// 删除文档
collection.deleteDocument(documentCreateEntity.getKey());
document = collection.getDocument(documentCreateEntity.getKey(), BaseDocument.class);
System.out.println("document: " + document);
// 删除集合
collection.drop();
// 删除数据库
db.drop();
// shutdown ArangoDB
arangoDB.shutdown();
另外,在ArangoDB中,也可以使用AQL来执行一些查询操作获取需要的集合,比如:
package my.arangodbstudy;
import com.arangodb.ArangoCollection;
import com.arangodb.ArangoCursor;
import com.arangodb.ArangoDB;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.CollectionEntity;
import com.arangodb.entity.DocumentCreateEntity;
import com.arangodb.util.MapBuilder;
import java.util.Collection;
import java.util.Map;
public class SimpleAQLTest
private static final String DB_HOST = "localhost";
private static final int DB_PORT = 8529;
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "<password>";
private static final String DB_NAME = "mydb";
private static final String COLLECTION_NAME = "users";
public static void main(String[] args)
// 构造ArangoDB实例
ArangoDB arangoDB = new ArangoDB.Builder()
.host(DB_HOST, DB_PORT)
.user(DB_USERNAME)
.password(DB_PASSWORD)
.build();
// 判断数据库存在,如果存在删除
if (arangoDB.db(DB_NAME).exists())
arangoDB.db(DB_NAME).drop();
// 创建数据库
arangoDB.createDatabase(DB_NAME);
System.out.println("arangodb databases: " + arangoDB.getDatabases());
// 获取刚才创建的数据库
ArangoDatabase db = arangoDB.db(DB_NAME);
// 创建集合
CollectionEntity collectionEntity = db.createCollection(COLLECTION_NAME);
System.out.println("collection name: " + collectionEntity.getName());
// 获取创建的集合
ArangoCollection collection = db.collection(COLLECTION_NAME);
// 创建文档对象并写入
for (int i = 0; i < 10; i++)
BaseDocument document = new BaseDocument();
document.addAttribute("name", "user_" + i);
document.addAttribute("age", 10 + i);
document.addAttribute("sex", 1);
collection.insertDocument(document);
System.out.println("collection count: " + collection.count().getCount());
// 使用AQL查询文档
String query = "FOR user IN " + COLLECTION_NAME +
" FILTER user.name == @name || user.age >= @age " +
" RETURN user";
Map<String, Object> params = new MapBuilder()
.put("name", "user_0")
.put("age", 15)
.get();
ArangoCursor<BaseDocument> cursor = db.query(query, params, null, BaseDocument.class);
cursor.forEachRemaining(document ->
System.out.println("document: " + document);
);
// 删除集合
collection.drop();
// 删除数据库
db.drop();
// shutdown ArangoDB
arangoDB.shutdown();
以上是关于使用Java驱动操作ArangoDB的主要内容,如果未能解决你的问题,请参考以下文章