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