GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(new File("path/to/neo4j")); try (Transaction tx = graphDb.beginTx()) { Node personNode = graphDb.createNode(); personNode.setProperty("name", "John Doe"); personNode.setProperty("age", 30); Node companyNode = graphDb.createNode(); companyNode.setProperty("name", "Acme Corporation"); companyNode.setProperty("location", "New York"); Relationship relationship = personNode.createRelationshipTo(companyNode, RelationshipType.withName("WORKS_FOR")); relationship.setProperty("years", 5); tx.success(); } try (Transaction tx = graphDb.beginTx()) { Result result = graphDb.execute("MATCH (p:Person)-[r:WORKS_FOR]->(c:Company) WHERE p.age > 25 RETURN p.name, r.years, c.name"); while (result.hasNext()) { Map<String, Object> row = result.next(); String personName = (String) row.get("p.name"); int workYears = (int) row.get("r.years"); String companyName = (String) row.get("c.name"); System.out.println(personName + " works for " + companyName + " for " + workYears + " years."); } tx.success(); }


上一篇:
下一篇:
切换中文