一、java连接ArangoDB数据库
(1)maven配置
(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
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'
)