当前位置:   article > 正文

使用Java驱动操作ArangoDB_java arangodb

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>
  • 1
  • 2
  • 3
  • 4
  • 5

完整的 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

下面写了一个简单的 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();
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

另外,在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();
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/706338
推荐阅读
相关标签
  

闽ICP备14008679号