Skip to content

Commit 87b5ca5

Browse files
authored
Improve Aura Example (#1049)
Add examples for reading relationships and logging.
1 parent 78baf52 commit 87b5ca5

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

examples/src/main/java/org/neo4j/docs/driver/DriverIntroductionExample.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,29 @@ public void close() throws Exception {
4848
driver.close();
4949
}
5050

51-
public void createFriendship(final String person1Name, final String person2Name) {
51+
public void createFriendship(final String person1Name, final String person2Name, final String knowsFrom) {
5252
// To learn more about the Cypher syntax, see https://neo4j.com/docs/cypher-manual/current/
5353
// The Reference Card is also a good resource for keywords https://neo4j.com/docs/cypher-refcard/current/
5454
String createFriendshipQuery = "CREATE (p1:Person { name: $person1_name })\n" +
5555
"CREATE (p2:Person { name: $person2_name })\n" +
56-
"CREATE (p1)-[:KNOWS]->(p2)\n" +
57-
"RETURN p1, p2";
56+
"CREATE (p1)-[k:KNOWS { from: $knows_from }]->(p2)\n" +
57+
"RETURN p1, p2, k";
5858

5959
Map<String, Object> params = new HashMap<>();
6060
params.put("person1_name", person1Name);
6161
params.put("person2_name", person2Name);
62+
params.put("knows_from", knowsFrom );
6263

6364
try (Session session = driver.session()) {
6465
// Write transactions allow the driver to handle retries and transient errors
6566
Record record = session.writeTransaction(tx -> {
6667
Result result = tx.run(createFriendshipQuery, params);
6768
return result.single();
6869
});
69-
System.out.println(String.format("Created friendship between: %s, %s",
70+
System.out.println(String.format("Created friendship between: %s, %s from %s",
7071
record.get("p1").get("name").asString(),
71-
record.get("p2").get("name").asString()));
72+
record.get("p2").get("name").asString(),
73+
record.get("k").get("from").asString()));
7274
// You should capture any errors along with the query and data for traceability
7375
} catch (Neo4jException ex) {
7476
LOGGER.log(Level.SEVERE, createFriendshipQuery + " raised an exception", ex);
@@ -102,9 +104,12 @@ public static void main(String... args) throws Exception {
102104

103105
String user = "<Username for Neo4j Aura database>";
104106
String password = "<Password for Neo4j Aura database>";
105-
106-
try (DriverIntroductionExample app = new DriverIntroductionExample(boltUrl, user, password, Config.defaultConfig())) {
107-
app.createFriendship("Alice", "David");
107+
Config config = Config.builder()
108+
// Configuring slf4j logging
109+
.withLogging( Logging.slf4j() )
110+
.build();
111+
try (DriverIntroductionExample app = new DriverIntroductionExample(boltUrl, user, password, config)) {
112+
app.createFriendship("Alice", "David", "School");
108113
app.findPerson("Alice");
109114
}
110115
}

examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,13 @@ void testShouldRunDriverIntroduction() throws Exception
336336

337337
try ( AutoCloseable ignored = stdIO.capture() )
338338
{
339-
intro.createFriendship( "Alice", "David" );
339+
intro.createFriendship( "Alice", "David", "School" );
340340
intro.findPerson( "Alice" );
341341
}
342342

343343
// Then
344344
assertThat( stdIO.stdout().size(), equalTo( 2 ) );
345-
assertThat( stdIO.stdout().get( 0 ), containsString( "Created friendship between: Alice, David" ) );
345+
assertThat( stdIO.stdout().get( 0 ), containsString( "Created friendship between: Alice, David from School" ) );
346346
assertThat( stdIO.stdout().get( 1 ), containsString( "Found person: Alice" ) );
347347
}
348348
}

0 commit comments

Comments
 (0)