ArangoDB——(使用后端语言操作)

一、java连接ArangoDB数据库

(1)maven配置

<dependency>

<groupId>com.arangodb</groupId>

<artifactId>arangodb-java-driver</artifactId>

<version>4.1.10</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

</dependency>

(2)Java代码

import com.arangodb.ArangoDB;

import com.arangodb.ArangoDatabase;

import com.arangodb.ArangoCollection;

import com.arangodb.entity.BaseDocument;

import org.junit.Test;

public class ArangoDBTest {

//连接和mongo差不多,本地地址

ArangoDB arangoDB = new ArangoDB.Builder().host("127.0.0.1", 8529).user("root").password("root").build();

//数据库

ArangoDatabase db = arangoDB.db("AQLTest");

//集合

ArangoCollection coll =db.collection("test");

@Test

public void insertDocument() {

BaseDocument document = new BaseDocument();

document.addAttribute("id",01);

document.addAttribute("name","Damo");

document.addAttribute("tag","10");

coll.insertDocument(document);

}

@Test

public void findrole() throws Exception {

try {

ArangoDB arango = new ArangoDB.Builder().host("127.0.0.1", 8529)

.user("root").password("root").build();

ArangoDatabase mydb = arango.db("myAQL");

String queryCmmd = "for doc in @@collection return doc";

AqlQueryOptions options = new AqlQueryOptions();

options.ttl(1000000);// 持续时间

Map map = new HashMap();

map.put("@collection", "test");

ArangoCursor<BaseDocument> cursor = mydb.query(queryCmmd, map,

options, BaseDocument.class);

int ii = 0;

while (cursor.hasNext()) {

ii++;

BaseDocument object = cursor.next();

// String name =object.getAttribute("name").toString(); //输出

System.out.println(object.toString());

System.out.println(ii);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

二、python连接ArangoDB

#可参考 https://github.com/joowani/python-arango

#python-arango-4.0及以上按下面方式使用

from arango import ArangoClient

# Initialize the client for ArangoDB.

#指定协议、IP、端口得到一个client

client = ArangoClient(protocol='http', host='localhost', port=8529)

# Connect to "_system" database as root user.

#指定数据库名_system 用户名 密码 得到数据库 sys_db

sys_db = client.db('_system', username='root', password='passwd')

# Create a new database named "test".

#创建一个test数据库

sys_db.create_database('test')

# Connect to "test" database as root user.

#得到test数据库实例

db = client.db('test', username='root', password='passwd')

# Create a new collection named "students".

#创建students documetn collection

students = db.create_collection('students')

# Add a hash index to the collection.

#添加Hash index (索引)

students.add_hash_index(fields=['name'], unique=True)

# Insert new documents into the collection.

#collection中插入数据

students.insert({'name': 'jane', 'age': 39})

students.insert({'name': 'josh', 'age': 18})

students.insert({'name': 'judy', 'age': 21})

# Execute an AQL query and iterate through the result cursor.

#获得查询游标

cursor = db.aql.execute('FOR doc IN students RETURN doc')

#通过document函数得到student_names

student_names = [document['name'] for document in cursor]

#Here is another example with graphs:

from arango import ArangoClient

# Initialize the client for ArangoDB.

#指定协议、IP、端口得到一个client

client = ArangoClient(protocol='http', host='localhost', port=8529)

# Connect to "test" database as root user.

#指定数据库名test 用户名 密码 得到数据库 db

db = client.db('test', username='root', password='passwd')

# Create a new graph named "school".

#创建graph

graph = db.create_graph('school')

# Create vertex collections for the graph.

#创建document类型的collection

students = graph.create_vertex_collection('students')

lectures = graph.create_vertex_collection('lectures')

# Create an edge definition (relation) for the graph.

#创建edge类型的collection

register = graph.create_edge_definition(

edge_collection='register',

from_vertex_collections=['students'],

to_vertex_collections=['lectures']

)

#插入数据到document collection

# Insert vertex documents into "students" (from) vertex collection.

students.insert({'_key': '01', 'full_name': 'Anna Smith'})

students.insert({'_key': '02', 'full_name': 'Jake Clark'})

students.insert({'_key': '03', 'full_name': 'Lisa Jones'})

#插入数据到document collection

# Insert vertex documents into "lectures" (to) vertex collection.

lectures.insert({'_key': 'MAT101', 'title': 'Calculus'})

lectures.insert({'_key': 'STA101', 'title': 'Statistics'})

lectures.insert({'_key': 'CSC101', 'title': 'Algorithms'})

#插入数据到edge collection

# Insert edge documents into "register" edge collection.

register.insert({'_from': 'students/01', '_to': 'lectures/MAT101'})

register.insert({'_from': 'students/01', '_to': 'lectures/STA101'})

register.insert({'_from': 'students/01', '_to': 'lectures/CSC101'})

register.insert({'_from': 'students/02', '_to': 'lectures/MAT101'})

register.insert({'_from': 'students/02', '_to': 'lectures/STA101'})

register.insert({'_from': 'students/03', '_to': 'lectures/CSC101'})

# Traverse the graph in outbound direction, breadth-first.

result = graph.traverse(

start_vertex='students/01',

direction='outbound',

strategy='breadthfirst'

)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程大虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值