Skip to content

Improve Aura Example #1066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,29 @@ 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<String, Object> 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
Record record = session.writeTransaction(tx -> {
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);
Expand Down Expand Up @@ -102,9 +104,12 @@ public static void main(String... args) throws Exception {

String user = "<Username for Neo4j Aura database>";
String password = "<Password for Neo4j Aura database>";

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");
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) );
}
}
Expand Down