From 6508c2b74ff4aa9195042d52d9dd5764c4c03b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Barc=C3=A9los?= Date: Fri, 29 Oct 2021 04:38:58 -0300 Subject: [PATCH] Improve Aura Example (#1049) Add examples for reading relationships and logging. --- .../driver/DriverIntroductionExample.java | 21 ++++++++++++------- .../org/neo4j/docs/driver/ExamplesIT.java | 4 ++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/examples/src/main/java/org/neo4j/docs/driver/DriverIntroductionExample.java b/examples/src/main/java/org/neo4j/docs/driver/DriverIntroductionExample.java index 25dacdd474..067422382e 100644 --- a/examples/src/main/java/org/neo4j/docs/driver/DriverIntroductionExample.java +++ b/examples/src/main/java/org/neo4j/docs/driver/DriverIntroductionExample.java @@ -48,17 +48,18 @@ public void close() throws Exception { driver.close(); } - public void createFriendship(final String person1Name, final String person2Name) { + public void createFriendship(final String person1Name, final String person2Name, final String knowsFrom) { // To learn more about the Cypher syntax, see https://neo4j.com/docs/cypher-manual/current/ // The Reference Card is also a good resource for keywords https://neo4j.com/docs/cypher-refcard/current/ String createFriendshipQuery = "CREATE (p1:Person { name: $person1_name })\n" + "CREATE (p2:Person { name: $person2_name })\n" + - "CREATE (p1)-[:KNOWS]->(p2)\n" + - "RETURN p1, p2"; + "CREATE (p1)-[k:KNOWS { from: $knows_from }]->(p2)\n" + + "RETURN p1, p2, k"; Map params = new HashMap<>(); params.put("person1_name", person1Name); params.put("person2_name", person2Name); + params.put("knows_from", knowsFrom ); try (Session session = driver.session()) { // Write transactions allow the driver to handle retries and transient errors @@ -66,9 +67,10 @@ public void createFriendship(final String person1Name, final String person2Name) Result result = tx.run(createFriendshipQuery, params); return result.single(); }); - System.out.println(String.format("Created friendship between: %s, %s", + System.out.println(String.format("Created friendship between: %s, %s from %s", record.get("p1").get("name").asString(), - record.get("p2").get("name").asString())); + record.get("p2").get("name").asString(), + record.get("k").get("from").asString())); // You should capture any errors along with the query and data for traceability } catch (Neo4jException ex) { LOGGER.log(Level.SEVERE, createFriendshipQuery + " raised an exception", ex); @@ -102,9 +104,12 @@ public static void main(String... args) throws Exception { String user = ""; String password = ""; - - try (DriverIntroductionExample app = new DriverIntroductionExample(boltUrl, user, password, Config.defaultConfig())) { - app.createFriendship("Alice", "David"); + Config config = Config.builder() + // Configuring slf4j logging + .withLogging( Logging.slf4j() ) + .build(); + try (DriverIntroductionExample app = new DriverIntroductionExample(boltUrl, user, password, config)) { + app.createFriendship("Alice", "David", "School"); app.findPerson("Alice"); } } diff --git a/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java b/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java index 09266b0525..063a9be9d7 100644 --- a/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java +++ b/examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java @@ -336,13 +336,13 @@ void testShouldRunDriverIntroduction() throws Exception try ( AutoCloseable ignored = stdIO.capture() ) { - intro.createFriendship( "Alice", "David" ); + intro.createFriendship( "Alice", "David", "School" ); intro.findPerson( "Alice" ); } // Then assertThat( stdIO.stdout().size(), equalTo( 2 ) ); - assertThat( stdIO.stdout().get( 0 ), containsString( "Created friendship between: Alice, David" ) ); + assertThat( stdIO.stdout().get( 0 ), containsString( "Created friendship between: Alice, David from School" ) ); assertThat( stdIO.stdout().get( 1 ), containsString( "Found person: Alice" ) ); } }