Skip to content

Commit af5ff3d

Browse files
author
a-brandt
committed
added document examples
1 parent 3cf8b9c commit af5ff3d

File tree

8 files changed

+792
-0
lines changed

8 files changed

+792
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.arangodb.example.document;
2+
3+
import com.arangodb.ArangoConfigure;
4+
import com.arangodb.ArangoDriver;
5+
import com.arangodb.ArangoException;
6+
import com.arangodb.entity.CollectionEntity;
7+
8+
public class BaseExample {
9+
10+
protected static ArangoDriver createDatabase(String name) {
11+
12+
ArangoConfigure configure = new ArangoConfigure();
13+
// configure.setUser("myUser");
14+
// configure.setPassword("password");
15+
// configuration file: src/test/resources/arangodb.properties
16+
configure.init();
17+
18+
ArangoDriver arangoDriver = new ArangoDriver(configure);
19+
20+
deleteDatabase(arangoDriver, name);
21+
22+
try {
23+
arangoDriver.createDatabase(name);
24+
System.out.println("Database created: " + name);
25+
} catch (Exception e) {
26+
System.out.println("Failed to create database " + name + "; " + e.getMessage());
27+
}
28+
29+
arangoDriver.setDefaultDatabase(name);
30+
31+
return arangoDriver;
32+
}
33+
34+
protected static void deleteDatabase(ArangoDriver arangoDriver, String name) {
35+
try {
36+
arangoDriver.deleteDatabase(name);
37+
} catch (Exception e) {
38+
}
39+
}
40+
41+
protected static void createCollection(ArangoDriver arangoDriver, String name) {
42+
try {
43+
CollectionEntity createCollection = arangoDriver.createCollection(name);
44+
System.out.println(
45+
"created collection '" + createCollection.getName() + "' with id '" + createCollection.getId() + "'");
46+
} catch (ArangoException e) {
47+
System.err.println(e.getMessage());
48+
}
49+
}
50+
51+
protected static void printEntity(Object object) {
52+
if (object == null) {
53+
System.out.println("Document not found");
54+
} else {
55+
System.out.println(object);
56+
}
57+
}
58+
59+
protected static void printHeadline(String name) {
60+
System.out.println("---------------------------------------------");
61+
System.out.println(name);
62+
System.out.println("---------------------------------------------");
63+
}
64+
65+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.arangodb.example.document;
2+
3+
import com.arangodb.ArangoDriver;
4+
import com.arangodb.ArangoException;
5+
import com.arangodb.entity.CollectionEntity;
6+
import com.arangodb.entity.CollectionsEntity;
7+
8+
public class CollectionExample extends BaseExample {
9+
10+
private static final String DATABASE_NAME = "CreateCollectionExample";
11+
12+
public static void main(String[] args) {
13+
//
14+
// You can find the ArangoDB Web interface here:
15+
// http://127.0.0.1:8529/
16+
//
17+
// change the log level to "debug" in /src/test/resource/logback.xml to
18+
// see the HTTP communication
19+
20+
ArangoDriver arangoDriver = createDatabase(DATABASE_NAME);
21+
22+
//
23+
printHeadline("create a collection");
24+
//
25+
try {
26+
CollectionEntity entity = arangoDriver.createCollection("collection1");
27+
printCollectionEntity(entity);
28+
// System.out.println(entity);
29+
} catch (ArangoException e) {
30+
System.err.println(e.getMessage());
31+
}
32+
33+
createCollection(arangoDriver, "collection2");
34+
createCollection(arangoDriver, "collection3");
35+
36+
//
37+
printHeadline("get list of all collections");
38+
//
39+
try {
40+
CollectionsEntity collectionsEntity = arangoDriver.getCollections();
41+
for (CollectionEntity entity : collectionsEntity.getCollections()) {
42+
printCollectionEntity(entity);
43+
// System.out.println(entity);
44+
}
45+
46+
} catch (ArangoException e) {
47+
System.err.println(e.getMessage());
48+
}
49+
50+
//
51+
printHeadline("get one collection");
52+
//
53+
try {
54+
CollectionEntity entity = arangoDriver.getCollection("collection2");
55+
printCollectionEntity(entity);
56+
// System.out.println(entity);
57+
} catch (ArangoException e) {
58+
System.err.println(e.getMessage());
59+
}
60+
61+
//
62+
printHeadline("rename collection");
63+
//
64+
try {
65+
CollectionEntity entity = arangoDriver.renameCollection("collection2", "collection4");
66+
printCollectionEntity(entity);
67+
// System.out.println(entity);
68+
} catch (ArangoException e) {
69+
System.err.println(e.getMessage());
70+
}
71+
72+
//
73+
printHeadline("truncate collection");
74+
//
75+
try {
76+
arangoDriver.truncateCollection("collection4");
77+
} catch (ArangoException e) {
78+
System.err.println(e.getMessage());
79+
}
80+
81+
//
82+
printHeadline("delete collection");
83+
//
84+
try {
85+
CollectionEntity entity = arangoDriver.deleteCollection("collection4");
86+
printCollectionEntity(entity);
87+
// System.out.println(entity);
88+
} catch (ArangoException e) {
89+
System.err.println(e.getMessage());
90+
}
91+
92+
}
93+
94+
private static void printCollectionEntity(CollectionEntity collection) {
95+
if (collection == null) {
96+
System.out.println("Collection not found");
97+
} else if (collection.getName() == null) {
98+
// collection is deleted
99+
System.out.println("Collection '" + collection.getName() + "' with id '" + collection.getId() + "'");
100+
} else {
101+
System.out.println(
102+
"Collection '" + collection.getName() + "' (" + (collection.getIsSystem() ? "system" : "normal")
103+
+ " collection) with id '" + collection.getId() + "'");
104+
}
105+
}
106+
107+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.arangodb.example.document;
2+
3+
import com.arangodb.ArangoConfigure;
4+
import com.arangodb.ArangoDriver;
5+
6+
public class CreateAndDeleteDatabaseExample extends BaseExample {
7+
8+
private static final String DATABASE_NAME = "CreateDatabaseExample";
9+
10+
public static void main(String[] args) {
11+
//
12+
// You can find the ArangoDB Web interface here:
13+
// http://127.0.0.1:8529/
14+
//
15+
// change the log level to "debug" in /src/test/resource/logback.xml to
16+
// see the HTTP communication
17+
18+
//
19+
printHeadline("create a driver");
20+
//
21+
22+
ArangoConfigure configure = new ArangoConfigure();
23+
// configure.setUser("myUser");
24+
// configure.setPassword("password");
25+
configure.init();
26+
27+
ArangoDriver arangoDriver = new ArangoDriver(configure);
28+
29+
//
30+
printHeadline("create a database");
31+
//
32+
try {
33+
arangoDriver.createDatabase(DATABASE_NAME);
34+
System.out.println("Database created: " + DATABASE_NAME);
35+
} catch (Exception e) {
36+
System.out.println("Failed to create database " + DATABASE_NAME + "; " + e.getMessage());
37+
}
38+
// set a default database for the connection
39+
arangoDriver.setDefaultDatabase(DATABASE_NAME);
40+
41+
// do something ...
42+
43+
//
44+
printHeadline("create a driver with default database");
45+
//
46+
ArangoConfigure configure2 = new ArangoConfigure();
47+
configure2.init();
48+
ArangoDriver arangoDriver2 = new ArangoDriver(configure, DATABASE_NAME);
49+
50+
// do something ...
51+
52+
//
53+
printHeadline("delete database");
54+
//
55+
try {
56+
arangoDriver.deleteDatabase(DATABASE_NAME);
57+
} catch (Exception e) {
58+
System.err.println(e.getMessage());
59+
}
60+
61+
}
62+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package com.arangodb.example.document;
2+
3+
import java.util.HashMap;
4+
5+
import com.arangodb.ArangoDriver;
6+
import com.arangodb.ArangoException;
7+
import com.arangodb.entity.BaseDocument;
8+
import com.arangodb.entity.DocumentEntity;
9+
10+
public class CreateDocumentExample extends BaseExample {
11+
12+
private static final String DATABASE_NAME = "CreateDocument";
13+
14+
private static final String COLLECTION_NAME = "CreateDocument";
15+
16+
private static final String KEY1 = "key1";
17+
18+
private static final String KEY2 = "key2";
19+
20+
private static final String KEY3 = "key3";
21+
22+
private static final String KEY4 = "key4";
23+
24+
public static void main(String[] args) {
25+
//
26+
// You can find the ArangoDB Web interface here:
27+
// http://127.0.0.1:8529/
28+
//
29+
// change the log level to "debug" in /src/test/resource/logback.xml to
30+
// see the HTTP communication
31+
32+
ArangoDriver arangoDriver = createDatabase(DATABASE_NAME);
33+
createCollection(arangoDriver, COLLECTION_NAME);
34+
35+
//
36+
printHeadline("create documents");
37+
//
38+
39+
System.out.println("1. create a document by a BaseDocument object:");
40+
BaseDocument myBaseDocument = new BaseDocument();
41+
myBaseDocument.setDocumentKey(KEY1);
42+
// attributes are stored in a HashMap
43+
myBaseDocument.addAttribute("name", "Alice");
44+
myBaseDocument.addAttribute("gender", "female");
45+
myBaseDocument.addAttribute("age", 18);
46+
47+
try {
48+
DocumentEntity<BaseDocument> entity = arangoDriver.createDocument(COLLECTION_NAME, myBaseDocument);
49+
// or DocumentEntity<BaseDocument> entity =
50+
// arangoDriver.createDocument(COLLECTION_NAME, KEY1,
51+
// myBaseDocument);
52+
53+
// the DocumentEntity contains the key, document handle and revision
54+
System.out.println("Key: " + entity.getDocumentKey());
55+
System.out.println("Id: " + entity.getDocumentHandle());
56+
System.out.println("Revision: " + entity.getDocumentRevision());
57+
58+
BaseDocument baseDocument = entity.getEntity();
59+
// the BaseDocument contains the key, document handle and revision
60+
System.out.println("Key: " + baseDocument.getDocumentKey());
61+
System.out.println("Id: " + baseDocument.getDocumentHandle());
62+
System.out.println("Revision: " + baseDocument.getDocumentRevision());
63+
// get the attributes
64+
System.out.println("Attribute 'name': " + baseDocument.getProperties().get("name"));
65+
System.out.println("Attribute 'gender': " + baseDocument.getProperties().get("gender"));
66+
System.out.println("Attribute 'age': " + baseDocument.getProperties().get("age"));
67+
68+
// printEntity(entity);
69+
} catch (ArangoException e) {
70+
System.out.println("Failed to create document. " + e.getMessage());
71+
}
72+
73+
System.out.println("2. create a document by a HashMap object:");
74+
HashMap<String, Object> myHashMap = new HashMap<String, Object>();
75+
myHashMap.put("_key", KEY2);
76+
// attributes are stored in a HashMap
77+
myHashMap.put("name", "Alice");
78+
myHashMap.put("gender", "female");
79+
myHashMap.put("age", 18);
80+
81+
try {
82+
DocumentEntity<HashMap<String, Object>> entity = arangoDriver.createDocument(COLLECTION_NAME, myHashMap);
83+
// or DocumentEntity<HashMap<String, Object>> entity =
84+
// arangoDriver.createDocument(COLLECTION_NAME, KEY2, myHashMap);
85+
86+
// the DocumentEntity contains the key, document handle and revision
87+
System.out.println("Key: " + entity.getDocumentKey());
88+
System.out.println("Id: " + entity.getDocumentHandle());
89+
System.out.println("Revision: " + entity.getDocumentRevision());
90+
91+
HashMap<String, Object> hashMap = entity.getEntity();
92+
// the HashMap contains the key, document handle and revision
93+
System.out.println("Key: " + hashMap.get("_key"));
94+
System.out.println("Id: " + hashMap.get("_id"));
95+
System.out.println("Revision: " + hashMap.get("_rev"));
96+
// get the attributes
97+
System.out.println("Attribute 'name': " + hashMap.get("name"));
98+
System.out.println("Attribute 'gender': " + hashMap.get("gender"));
99+
System.out.println("Attribute 'age': " + hashMap.get("age"));
100+
101+
// printEntity(entity);
102+
} catch (ArangoException e) {
103+
System.out.println("Failed to create document. " + e.getMessage());
104+
}
105+
106+
System.out.println("3. create a document by an object:");
107+
SimplePerson mySimplePerson = new SimplePerson("Angela", "female", 42);
108+
try {
109+
DocumentEntity<SimplePerson> entity = arangoDriver.createDocument(COLLECTION_NAME, KEY3, mySimplePerson);
110+
111+
// the DocumentEntity contains the key, document handle and revision
112+
System.out.println("Key: " + entity.getDocumentKey());
113+
System.out.println("Id: " + entity.getDocumentHandle());
114+
System.out.println("Revision: " + entity.getDocumentRevision());
115+
116+
SimplePerson simplePerson = entity.getEntity();
117+
// get the attributes
118+
System.out.println("Attribute 'name': " + simplePerson.getName());
119+
System.out.println("Attribute 'gender': " + simplePerson.getGender());
120+
System.out.println("Attribute 'age': " + simplePerson.getAge());
121+
122+
// printEntity(entity);
123+
} catch (ArangoException e) {
124+
System.out.println("Failed to create document. " + e.getMessage());
125+
}
126+
127+
System.out.println("4. create a document by an object with document attributes:");
128+
DocumentPerson myDocumentPerson = new DocumentPerson("Peter", "male", 24);
129+
try {
130+
DocumentEntity<DocumentPerson> entity = arangoDriver.createDocument(COLLECTION_NAME, KEY4,
131+
myDocumentPerson);
132+
133+
// the DocumentEntity contains the key, document handle and revision
134+
System.out.println("Key: " + entity.getDocumentKey());
135+
System.out.println("Id: " + entity.getDocumentHandle());
136+
System.out.println("Revision: " + entity.getDocumentRevision());
137+
138+
DocumentPerson documentPerson = entity.getEntity();
139+
// the DocumentPerson contains the key, document handle and revision
140+
System.out.println("Key: " + documentPerson.getDocumentKey());
141+
System.out.println("Id: " + documentPerson.getDocumentHandle());
142+
System.out.println("Revision: " + documentPerson.getDocumentRevision());
143+
// get the attributes
144+
System.out.println("Attribute 'name': " + documentPerson.getName());
145+
System.out.println("Attribute 'gender': " + documentPerson.getGender());
146+
System.out.println("Attribute 'age': " + documentPerson.getAge());
147+
148+
// printEntity(entity);
149+
} catch (ArangoException e) {
150+
System.out.println("Failed to create document. " + e.getMessage());
151+
}
152+
153+
}
154+
155+
}

0 commit comments

Comments
 (0)