|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 ArangoDB GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.arangodb.example; |
| 18 | + |
| 19 | +import java.util.Iterator; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import com.arangodb.ArangoConfigure; |
| 23 | +import com.arangodb.ArangoDriver; |
| 24 | +import com.arangodb.ArangoException; |
| 25 | +import com.arangodb.DocumentCursor; |
| 26 | +import com.arangodb.entity.BaseDocument; |
| 27 | +import com.arangodb.entity.CollectionEntity; |
| 28 | +import com.arangodb.entity.DocumentEntity; |
| 29 | +import com.arangodb.util.MapBuilder; |
| 30 | + |
| 31 | +/** |
| 32 | + * My first ArangoDB project |
| 33 | + */ |
| 34 | +public class FirstProject { |
| 35 | + |
| 36 | + public static void main(String[] args) { |
| 37 | + |
| 38 | + // |
| 39 | + // You can find the ArangoDB Web interface here: |
| 40 | + // http://127.0.0.1:8529/_db/mydb/ |
| 41 | + // |
| 42 | + |
| 43 | + // Lets configure and open a connection to start ArangoDB. |
| 44 | + ArangoConfigure configure = new ArangoConfigure(); |
| 45 | + configure.init(); |
| 46 | + ArangoDriver arangoDriver = new ArangoDriver(configure); |
| 47 | + // The default connection is to http://127.0.0.1:8529. |
| 48 | + |
| 49 | + // Lets configure and open a connection to start ArangoDB. |
| 50 | + String dbName = "mydb"; |
| 51 | + try { |
| 52 | + arangoDriver.createDatabase(dbName); |
| 53 | + System.out.println("Database created: " + dbName); |
| 54 | + } catch (Exception e) { |
| 55 | + System.out.println("Failed to create database " + dbName + "; " + e.getMessage()); |
| 56 | + } |
| 57 | + |
| 58 | + // You can set the new database as default database for the driver: |
| 59 | + arangoDriver.setDefaultDatabase(dbName); |
| 60 | + |
| 61 | + String collectionName = "firstCollection"; |
| 62 | + try { |
| 63 | + CollectionEntity myArangoCollection = arangoDriver.createCollection(collectionName); |
| 64 | + System.out.println("Collection created: " + myArangoCollection.getName()); |
| 65 | + } catch (Exception e) { |
| 66 | + System.out.println("Failed to create colleciton " + collectionName + "; " + e.getMessage()); |
| 67 | + } |
| 68 | + |
| 69 | + // create a document |
| 70 | + BaseDocument myObject = new BaseDocument(); |
| 71 | + myObject.setDocumentKey("myKey"); |
| 72 | + myObject.addAttribute("a", "Foo"); |
| 73 | + myObject.addAttribute("b", 42); |
| 74 | + try { |
| 75 | + arangoDriver.createDocument(collectionName, myObject); |
| 76 | + System.out.println("Document created"); |
| 77 | + } catch (ArangoException e) { |
| 78 | + System.out.println("Failed to create document. " + e.getMessage()); |
| 79 | + } |
| 80 | + |
| 81 | + // read a document |
| 82 | + DocumentEntity<BaseDocument> myDocument = null; |
| 83 | + BaseDocument myObject2 = null; |
| 84 | + try { |
| 85 | + myDocument = arangoDriver.getDocument(collectionName, "myKey", BaseDocument.class); |
| 86 | + myObject2 = myDocument.getEntity(); |
| 87 | + System.out.println("Key: " + myObject2.getDocumentKey()); |
| 88 | + System.out.println("Attribute 'a': " + myObject2.getProperties().get("a")); |
| 89 | + System.out.println("Attribute 'b': " + myObject2.getProperties().get("b")); |
| 90 | + System.out.println("Attribute 'c': " + myObject2.getProperties().get("c")); |
| 91 | + } catch (ArangoException e) { |
| 92 | + System.out.println("Failed to get document. " + e.getMessage()); |
| 93 | + } |
| 94 | + |
| 95 | + // update a document |
| 96 | + try { |
| 97 | + myObject2.addAttribute("c", "Bar"); |
| 98 | + arangoDriver.updateDocument(myDocument.getDocumentHandle(), myObject2); |
| 99 | + } catch (ArangoException e) { |
| 100 | + System.out.println("Failed to update document. " + e.getMessage()); |
| 101 | + } |
| 102 | + |
| 103 | + // read document again |
| 104 | + try { |
| 105 | + myDocument = arangoDriver.getDocument(collectionName, "myKey", BaseDocument.class); |
| 106 | + System.out.println("Key: " + myObject2.getDocumentKey()); |
| 107 | + System.out.println("Attribute 'a': " + myObject2.getProperties().get("a")); |
| 108 | + System.out.println("Attribute 'b': " + myObject2.getProperties().get("b")); |
| 109 | + System.out.println("Attribute 'c': " + myObject2.getProperties().get("c")); |
| 110 | + } catch (ArangoException e) { |
| 111 | + System.out.println("Failed to get document. " + e.getMessage()); |
| 112 | + } |
| 113 | + |
| 114 | + // delete document |
| 115 | + try { |
| 116 | + arangoDriver.deleteDocument(myDocument.getDocumentHandle()); |
| 117 | + } catch (ArangoException e) { |
| 118 | + System.out.println("Failed to delete document. " + e.getMessage()); |
| 119 | + } |
| 120 | + |
| 121 | + // create some example entries |
| 122 | + try { |
| 123 | + for (Integer i = 0; i < 10; i++) { |
| 124 | + BaseDocument baseDocument = new BaseDocument(); |
| 125 | + baseDocument.setDocumentKey(i.toString()); |
| 126 | + baseDocument.addAttribute("name", "Homer"); |
| 127 | + baseDocument.addAttribute("b", i + 42); |
| 128 | + arangoDriver.createDocument(collectionName, baseDocument); |
| 129 | + } |
| 130 | + } catch (ArangoException e) { |
| 131 | + System.out.println("Failed to create document. " + e.getMessage()); |
| 132 | + } |
| 133 | + |
| 134 | + // Get all documents with the name "Homer" from collection |
| 135 | + // "firstCollection" and iterate over the result: |
| 136 | + try { |
| 137 | + String query = "FOR t IN firstCollection FILTER t.name == @name RETURN t"; |
| 138 | + Map<String, Object> bindVars = new MapBuilder().put("name", "Homer").get(); |
| 139 | + DocumentCursor<BaseDocument> cursor = arangoDriver.executeDocumentQuery(query, bindVars, null, |
| 140 | + BaseDocument.class); |
| 141 | + |
| 142 | + Iterator<BaseDocument> iterator = cursor.entityIterator(); |
| 143 | + while (iterator.hasNext()) { |
| 144 | + BaseDocument aDocument = iterator.next(); |
| 145 | + System.out.println("Key: " + aDocument.getDocumentKey()); |
| 146 | + } |
| 147 | + } catch (ArangoException e) { |
| 148 | + System.out.println("Failed to execute query. " + e.getMessage()); |
| 149 | + } |
| 150 | + |
| 151 | + // Now we will delete the document created before: |
| 152 | + try { |
| 153 | + String query = "FOR t IN firstCollection FILTER t.name == @name " |
| 154 | + + "REMOVE t IN firstCollection LET removed = OLD RETURN removed"; |
| 155 | + Map<String, Object> bindVars = new MapBuilder().put("name", "Homer").get(); |
| 156 | + DocumentCursor<BaseDocument> cursor = arangoDriver.executeDocumentQuery(query, bindVars, null, |
| 157 | + BaseDocument.class); |
| 158 | + |
| 159 | + Iterator<BaseDocument> iterator = cursor.entityIterator(); |
| 160 | + while (iterator.hasNext()) { |
| 161 | + BaseDocument aDocument = iterator.next(); |
| 162 | + System.out.println("Removed document: " + aDocument.getDocumentKey()); |
| 163 | + } |
| 164 | + |
| 165 | + } catch (ArangoException e) { |
| 166 | + System.out.println("Failed to execute query. " + e.getMessage()); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments