ArangoDB教程(三)-使用java操作ArangoDB

package com.haha.ArangoDBTest;

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 ArangoDBTest {

	public static void main(String[] args)
	{
		//配置和打开连接以启动ArangoDB
		ArangoDB arangoDB = new ArangoDB.Builder().host("10.41.137.175", 8529).user("root").password("root").build();
		
		try 
		{
			//创建数据库
			String dbName = "TestDb";
			arangoDB.createDatabase(dbName);
			
			//创建Collection集合
			String collectionName = "TestCollection";
			CollectionEntity myArangoCollection = arangoDB.db(dbName).createCollection(collectionName);
			
			//创建document文档
			BaseDocument myObject = new BaseDocument();
			myObject.setKey("myKey");
			myObject.addAttribute("name", "张三");
			myObject.addAttribute("age", 42);
			arangoDB.db(dbName).collection(collectionName).insertDocument(myObject);
			
			//查询document文档
			BaseDocument myDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey",BaseDocument.class);
			System.out.println("Key: " + myDocument.getKey());
			System.out.println("Attribute name: " + myDocument.getAttribute("name"));
			System.out.println("Attribute age: " + myDocument.getAttribute("age"));
			
			//更新document文档
			myObject.addAttribute("sex", "男");
			arangoDB.db(dbName).collection(collectionName).updateDocument("myKey", myObject);
			
			//删除document文档
			//arangoDB.db(dbName).collection(collectionName).deleteDocument("myKey");
			
			//执行AQL语句
//			String query = "FOR t IN TestCollection FILTER t.name == @name "
//					+ "REMOVE t IN TestCollection LET removed = OLD RETURN removed";
//			Map<String, Object> bindVars = new MapBuilder().put("name", "张三").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)
		{
			e.printStackTrace();
		}
	}
}

pom.xml

<dependency>
    <groupId>com.arangodb</groupId>
    <artifactId>arangodb-java-driver</artifactId>
    <version>4.5.2</version>
</dependency>
发布了184 篇原创文章 · 获赞 121 · 访问量 47万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览