|
| 1 | +/* |
| 2 | + * Copyright (C) 2012 tamtam180 |
| 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.HashMap; |
| 20 | +import java.util.Iterator; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import com.arangodb.ArangoConfigure; |
| 25 | +import com.arangodb.ArangoDriver; |
| 26 | +import com.arangodb.ArangoException; |
| 27 | +import com.arangodb.CursorResult; |
| 28 | +import com.arangodb.util.AqlQueryOptions; |
| 29 | + |
| 30 | +/** |
| 31 | + * AQL example with new cursor implementation |
| 32 | + * |
| 33 | + * @author tamtam180 - kirscheless at gmail.com |
| 34 | + * @author a-brandt |
| 35 | + * |
| 36 | + */ |
| 37 | +public class ExecuteDocumentQueryWithSpecialReturnTypesExample { |
| 38 | + |
| 39 | + private static final String COLLECTION_NAME = "example_collection1"; |
| 40 | + |
| 41 | + public static class Person { |
| 42 | + public String name; |
| 43 | + public String gender; |
| 44 | + public int age; |
| 45 | + } |
| 46 | + |
| 47 | + public static void main(String[] args) { |
| 48 | + |
| 49 | + // Initialize configure |
| 50 | + ArangoConfigure configure = new ArangoConfigure(); |
| 51 | + configure.init(); |
| 52 | + |
| 53 | + // Create Driver (this instance is thread-safe) |
| 54 | + ArangoDriver driver = new ArangoDriver(configure); |
| 55 | + |
| 56 | + // create test collection |
| 57 | + try { |
| 58 | + driver.createCollection(COLLECTION_NAME); |
| 59 | + } catch (ArangoException e) { |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + // remove all elements of test collection |
| 64 | + driver.truncateCollection(COLLECTION_NAME); |
| 65 | + |
| 66 | + // create some persons |
| 67 | + for (int i = 0; i < 1000; i++) { |
| 68 | + Person value = new Person(); |
| 69 | + value.name = "TestUser" + i; |
| 70 | + switch (i % 3) { |
| 71 | + case 0: |
| 72 | + value.gender = "MAN"; |
| 73 | + break; |
| 74 | + case 1: |
| 75 | + value.gender = "WOMAN"; |
| 76 | + break; |
| 77 | + case 2: |
| 78 | + value.gender = "OTHER"; |
| 79 | + break; |
| 80 | + } |
| 81 | + value.age = (int) (Math.random() * 100) + 10; |
| 82 | + driver.createDocument(COLLECTION_NAME, value, true, null); |
| 83 | + } |
| 84 | + |
| 85 | + // bind @gender to WOMAN |
| 86 | + HashMap<String, Object> bindVars = new HashMap<String, Object>(); |
| 87 | + bindVars.put("gender", "WOMAN"); |
| 88 | + |
| 89 | + // query (count = true, batchSize = 5) |
| 90 | + AqlQueryOptions aqlQueryOptions = new AqlQueryOptions().setCount(true).setBatchSize(5); |
| 91 | + |
| 92 | + // map |
| 93 | + System.out.println("get AQL query results in a map"); |
| 94 | + |
| 95 | + String queryString = "FOR t IN " + COLLECTION_NAME |
| 96 | + + " FILTER t.age >= 20 && t.age < 30 && t.gender == @gender RETURN t"; |
| 97 | + @SuppressWarnings("rawtypes") |
| 98 | + CursorResult<Map> cursor = driver.executeAqlQuery(queryString, bindVars, aqlQueryOptions, Map.class); |
| 99 | + @SuppressWarnings("rawtypes") |
| 100 | + Iterator<Map> iterator = cursor.iterator(); |
| 101 | + while (iterator.hasNext()) { |
| 102 | + Map<?, ?> map = iterator.next(); |
| 103 | + System.out.printf("%15s (%5s): %s%n", map.get("name"), map.get("gender"), map.get("age").toString()); |
| 104 | + } |
| 105 | + |
| 106 | + // list |
| 107 | + System.out.println("get AQL query results in a list"); |
| 108 | + |
| 109 | + queryString = "FOR t IN " + COLLECTION_NAME |
| 110 | + + " FILTER t.age >= 20 && t.age < 30 && t.gender == @gender RETURN [t.name, t.gender, t.age]"; |
| 111 | + @SuppressWarnings("rawtypes") |
| 112 | + CursorResult<List> cursor2 = driver.executeAqlQuery(queryString, bindVars, aqlQueryOptions, List.class); |
| 113 | + @SuppressWarnings("rawtypes") |
| 114 | + Iterator<List> iterator2 = cursor2.iterator(); |
| 115 | + while (iterator2.hasNext()) { |
| 116 | + List<?> list = iterator2.next(); |
| 117 | + System.out.printf("%15s (%5s): %s%n", list.get(0), list.get(1), list.get(2).toString()); |
| 118 | + } |
| 119 | + |
| 120 | + } catch (ArangoException e) { |
| 121 | + e.printStackTrace(); |
| 122 | + } finally { |
| 123 | + configure.shutdown(); |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | +} |
0 commit comments