-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathFirstProject.java
136 lines (119 loc) · 5.95 KB
/
FirstProject.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package example;
import com.arangodb.ArangoCollection;
import com.arangodb.ArangoCursor;
import com.arangodb.ArangoDB;
import com.arangodb.ArangoDBException;
import com.arangodb.config.ArangoConfigProperties;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.CollectionEntity;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
public class FirstProject {
public static void main(final String[] args) {
final ArangoDB arangoDB = new ArangoDB.Builder().loadProperties(ArangoConfigProperties.fromFile()).user("root").build();
// create database
final String dbName = "mydb";
try {
arangoDB.createDatabase(dbName);
System.out.println("Database created: " + dbName);
} catch (final ArangoDBException e) {
System.err.println("Failed to create database: " + dbName + "; " + e.getMessage());
}
// create collection
final String collectionName = "firstCollection";
try {
final CollectionEntity myArangoCollection = arangoDB.db(dbName).createCollection(collectionName);
System.out.println("Collection created: " + myArangoCollection.getName());
} catch (final ArangoDBException e) {
System.err.println("Failed to create collection: " + collectionName + "; " + e.getMessage());
}
// creating a document
final BaseDocument myObject = new BaseDocument(UUID.randomUUID().toString());
myObject.setKey("myKey");
myObject.addAttribute("a", "Foo");
myObject.addAttribute("b", 42);
try {
arangoDB.db(dbName).collection(collectionName).insertDocument(myObject);
System.out.println("Document created");
} catch (final ArangoDBException e) {
System.err.println("Failed to create document. " + e.getMessage());
}
// read a document
try {
final BaseDocument myDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey",
BaseDocument.class);
System.out.println("Key: " + myDocument.getKey());
System.out.println("Attribute a: " + myDocument.getAttribute("a"));
System.out.println("Attribute b: " + myDocument.getAttribute("b"));
} catch (final ArangoDBException e) {
System.err.println("Failed to get document: myKey; " + e.getMessage());
}
// read a document as JsonNode
try {
final JsonNode myDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey",
JsonNode.class);
System.out.println("Key: " + myDocument.get("_key").textValue());
System.out.println("Attribute a: " + myDocument.get("a").textValue());
System.out.println("Attribute b: " + myDocument.get("b").textValue());
} catch (final ArangoDBException e) {
System.err.println("Failed to get document: myKey; " + e.getMessage());
}
// update a document
myObject.addAttribute("c", "Bar");
try {
arangoDB.db(dbName).collection(collectionName).updateDocument("myKey", myObject);
} catch (final ArangoDBException e) {
System.err.println("Failed to update document. " + e.getMessage());
}
// read the document again
try {
final BaseDocument myUpdatedDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey",
BaseDocument.class);
System.out.println("Key: " + myUpdatedDocument.getKey());
System.out.println("Attribute a: " + myUpdatedDocument.getAttribute("a"));
System.out.println("Attribute b: " + myUpdatedDocument.getAttribute("b"));
System.out.println("Attribute c: " + myUpdatedDocument.getAttribute("c"));
} catch (final ArangoDBException e) {
System.err.println("Failed to get document: myKey; " + e.getMessage());
}
// delete a document
try {
arangoDB.db(dbName).collection(collectionName).deleteDocument("myKey");
} catch (final ArangoDBException e) {
System.err.println("Failed to delete document. " + e.getMessage());
}
// create some documents for the next step
final ArangoCollection collection = arangoDB.db(dbName).collection(collectionName);
for (int i = 0; i < 10; i++) {
final BaseDocument value = new BaseDocument(UUID.randomUUID().toString());
value.setKey(String.valueOf(i));
value.addAttribute("name", "Homer");
collection.insertDocument(value);
}
// execute AQL queries
try {
final String query = "FOR t IN firstCollection FILTER t.name == @name RETURN t";
final Map<String, Object> bindVars = Collections.singletonMap("name", "Homer");
final ArangoCursor<BaseDocument> cursor = arangoDB.db(dbName).query(query, BaseDocument.class, bindVars);
while (cursor.hasNext()) {
System.out.println("Key: " + cursor.next().getKey());
}
} catch (final ArangoDBException e) {
System.err.println("Failed to execute query. " + e.getMessage());
}
// delete a document with AQL
try {
final String query = "FOR t IN firstCollection FILTER t.name == @name "
+ "REMOVE t IN firstCollection LET removed = OLD RETURN removed";
final Map<String, Object> bindVars = Collections.singletonMap("name", "Homer");
final ArangoCursor<BaseDocument> cursor = arangoDB.db(dbName).query(query, BaseDocument.class, bindVars);
while (cursor.hasNext()) {
System.out.println("Removed document " + cursor.next().getKey());
}
} catch (final ArangoDBException e) {
System.err.println("Failed to execute query. " + e.getMessage());
}
}
}