java key value 数据类型_怎么灵活使用Graph, Document, Key/Value 三种混合模型的数据库?--Java 10分钟教程...

这是一个关于如何用Java Sync Driver 4.1使用ArangoDB的简短教程。在不到10分钟的时间内,您将学会如何用Java 操作ArangoDB (ArangoDB on Github)。有关驱动程序的功能和性能的更多细节,请查看相应的博客文章。

*请注意: 本教程是针对 ArangoDB 3.1版本编写的,有可能不支持旧版本。

如果您使用的是旧版本的ArangoDB,请查看为旧版本设计的Java教程。*

安装Java驱动程序

本教程将在Eclipse中介绍Java驱动程序的用法。首先,通过maven将Java驱动程序添加到您的项目中:

1

2

3

4 com.arangodb

5 arangodb-java-driver

6 4.1.0

7

8 ....

使用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.0.0)

注意: 确保安装并运行ArangoDB 3.1或更高版本。

快速开始

创建一个名为 FirstProject 的文件并写入:

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 }

}

在eclipse中,您需要创建一个名为 FirstProject的新类,并将代码复制到其中。在本教程的过程中,将使用代码中给出的每个 import。

f38657dc162b30f48d562973f5c35060.png

连接

配置和打开连接以启动ArangoDB。

1 ArangoDB arangoDB = new ArangoDB.Builder().build();

bfa021e5c1970356804d907b94892222.png

创建数据库

我们来创建一个新的数据库:

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

}

84217887cd96bdbd3566b80ab39799a5.png

结果是应该是:

Database created: mydb

创建一个集合

现在让我们来创建我们的第一个集合:

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

}

f62ef678f9ad003a2952f3522a082188.png

结果是应该是:

1 Collection created: firstCollection

您应该了解的一些细节代码:

createCollection()创建集合

firstCollection 是集合的名字

创建文档

现在我们在集合中创建一个文档。任何对象都可以作为文档添加到数据库中,并作为对象从数据库中检索。

这个例子中,我们使用驱动程序提供的BaseDocument类。文档的属性存储在映射中,作为键/值 对:

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

43954b347eaa2256c8f14f7b937178d5.png

结果应该是:

1 Document created

您应该了解的一些细节代码:

setKey() 设置新对象的键值

addAttribute() 将一个新的键/值对放在对象中

每个属性作为单个键/值对存储在文档根目录中

阅读文档

阅读创建的文档:

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

}

abff52080d70fc0dedb7a6a72292d960.png

结果应该是:

1

2 Key: myKey

3 Attribute a: Foo

Attribute b: 42

您应该了解的一些细节代码:

getDocument() 将存储的文档数据返回到给定的JavaBean (BaseDocument)

阅读VelocyPack 格式文档

您也可以阅读VelocyPack 格式文档:

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

}

8d2c8898114415e903d4e650ac6b9040.png

您应该了解的一些细节代码:

getDocument() 将存储的文档数据返回到 VelocyPack 格式 (VPackSlice)

更新文档

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

}

0a77ba63603e3f2acc806f5c9de93960.png

再次阅读文件

我们再次阅读文件:

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

}

0ce3f2c3cae5545a9ac17e1b95b0b19b.png

结果应该是:

1

2 Key: myKey

3 Attribute a: Foo

4 Attribute b: 42

Attribute c: Bar

删除文档

让我们来删除一个文档:

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

}

c57545de621f06cb4c15cb2d10bceb16.png

执行AQL查询

首先我们需要在集合firstCollection中创建一些名称为Homer的文档:

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

}

602883348706eda499a5897408a4b42c.png

从集合firstCollection中获取所有名称为Homer 的文档,并且遍历存储结果:

try {

String query = "FOR t IN firstCollection FILTER t.name == @name RETURN t";

Map bindVars = new MapBuilder().put("name", "Homer").get();

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

}

9ad2e31cb47adee5fb57882f3333d9f0.png

结果应该是:

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";

Map bindVars = new MapBuilder().put("name", "Homer").get();

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

}

6a688521b768fa2a35e0236d1a5c3d17.png

结果应该是:

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 文档,进一步学习我们的查询语言。

想更多地了解我们的数据库吗点击这里!

在我们的文档中浏览更多关于Documents的信息。

相关更多示例,您可以浏览ArangoDB 菜谱。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值