(String dbPath)
| 63 | } |
| 64 | |
| 65 | private static void runExample(String dbPath) { |
| 66 | // 1. Open database |
| 67 | System.out.println("1. Opening database..."); |
| 68 | try (GraphLite db = GraphLite.open(dbPath)) { |
| 69 | System.out.println(" [OK] GraphLite version: " + GraphLite.version() + "\n"); |
| 70 | |
| 71 | // 2. Create session |
| 72 | System.out.println("2. Creating session..."); |
| 73 | String session = db.createSession("admin"); |
| 74 | System.out.println(" [OK] Session created: " + session.substring(0, 20) + "...\n"); |
| 75 | |
| 76 | // 3. Create schema and graph |
| 77 | System.out.println("3. Setting up schema and graph..."); |
| 78 | db.execute(session, "CREATE SCHEMA IF NOT EXISTS /example"); |
| 79 | db.execute(session, "SESSION SET SCHEMA /example"); |
| 80 | db.execute(session, "CREATE GRAPH IF NOT EXISTS social"); |
| 81 | db.execute(session, "SESSION SET GRAPH social"); |
| 82 | System.out.println(" [OK] Schema and graph created\n"); |
| 83 | |
| 84 | // 4. Insert data |
| 85 | System.out.println("4. Inserting data..."); |
| 86 | db.execute(session, "INSERT (:Person {name: 'Alice', age: 30})"); |
| 87 | db.execute(session, "INSERT (:Person {name: 'Bob', age: 25})"); |
| 88 | db.execute(session, "INSERT (:Person {name: 'Charlie', age: 35})"); |
| 89 | System.out.println(" [OK] Inserted 3 persons\n"); |
| 90 | |
| 91 | // 5. Query data |
| 92 | System.out.println("5. Querying: All persons' age and name)"); |
| 93 | QueryResult result = db.query(session, |
| 94 | "MATCH (p:Person) RETURN p.name as name, p.age as age"); |
| 95 | System.out.println(" Found " + result.getRowCount() + " persons:"); |
| 96 | for (Map<String, Object> row : result.getRows()) { |
| 97 | System.out.println(" - " + row.get("name") + ": " + row.get("age") + " years old"); |
| 98 | } |
| 99 | System.out.println(); |
| 100 | |
| 101 | // 6. Filter with WHERE |
| 102 | System.out.println("6. Filtering: Persons older than 25 years in the ascending order of age"); |
| 103 | result = db.query(session, |
| 104 | "MATCH (p:Person) WHERE p.age > 25 " + |
| 105 | "RETURN p.name as name, p.age as age ORDER BY p.age ASC"); |
| 106 | System.out.println(" Found " + result.getRowCount() + " persons over 25:"); |
| 107 | for (Map<String, Object> row : result.getRows()) { |
| 108 | System.out.println(" - " + row.get("name") + ": " + row.get("age") + " years old"); |
| 109 | } |
| 110 | System.out.println(); |
| 111 | |
| 112 | // 7. Aggregation |
| 113 | System.out.println("7. Aggregation query..."); |
| 114 | result = db.query(session, |
| 115 | "MATCH (p:Person) RETURN count(p) as total, avg(p.age) as avg_age"); |
| 116 | if (!result.isEmpty()) { |
| 117 | Map<String, Object> row = result.first(); |
| 118 | System.out.println(" Total persons: " + row.get("total")); |
| 119 | Object avgAge = row.get("avg_age"); |
| 120 | System.out.println(" Average age: " + (avgAge instanceof Number ? ((Number) avgAge).doubleValue() : avgAge)); |
| 121 | } |
| 122 | System.out.println(); |
no test coverage detected