Skip to content

Commit 2c1ba34

Browse files
committed
update example
1 parent b944eff commit 2c1ba34

File tree

2 files changed

+91
-30
lines changed

2 files changed

+91
-30
lines changed

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

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,90 @@
2121
// tag::driver-introduction-example-import[]
2222

2323
import org.neo4j.driver.*;
24+
import org.neo4j.driver.exceptions.Neo4jException;
2425

25-
import static org.neo4j.driver.Values.parameters;
26+
import java.util.Collections;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
import java.util.logging.Level;
30+
import java.util.logging.Logger;
2631
// end::driver-introduction-example-import[]
2732

2833
// tag::driver-introduction-example[]
29-
public class DriverIntroductionExample implements AutoCloseable
30-
{
34+
public class DriverIntroductionExample implements AutoCloseable {
35+
private static final Logger LOGGER = Logger.getLogger(DriverIntroductionExample.class.getName());
3136
private final Driver driver;
3237

33-
public DriverIntroductionExample(String uri, String user, String password )
34-
{
35-
driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ) );
38+
public DriverIntroductionExample(String uri, String user, String password) {
39+
// Aura queries use an encrypted connection
40+
Config config = Config.builder()
41+
.withEncryption()
42+
.build();
43+
44+
// The driver is a long living object and should be opened during the start of your application
45+
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password), config);
3646
}
3747

3848
@Override
39-
public void close() throws Exception
40-
{
49+
public void close() throws Exception {
50+
// The driver object should be closed before the application ends.
4151
driver.close();
4252
}
4353

44-
public void printGreeting( final String message )
45-
{
46-
try ( Session session = driver.session() )
47-
{
48-
String greeting = session.writeTransaction( new TransactionWork<String>()
49-
{
50-
@Override
51-
public String execute( Transaction tx )
52-
{
53-
Result result = tx.run( "CREATE (a:Greeting) " +
54-
"SET a.message = $message " +
55-
"RETURN a.message + ', from node ' + id(a)",
56-
parameters( "message", message ) );
57-
return result.single().get( 0 ).asString();
58-
}
59-
} );
60-
System.out.println( greeting );
54+
public void createFriendship(final String person1Name, final String person2Name) {
55+
// To learn more about the Cypher syntax, see https://neo4j.com/docs/cypher-manual/current/
56+
// The Reference Card is also a good resource for keywords https://neo4j.com/docs/cypher-refcard/current/
57+
String createFriendshipQuery = "CREATE (p1:Person { name: $person1_name })\n" +
58+
"CREATE (p2:Person { name: $person2_name })\n" +
59+
"CREATE (p1)-[:KNOWS]->(p2)\n" +
60+
"RETURN p1, p2";
61+
62+
Map<String, Object> params = new HashMap<>();
63+
params.put("person1_name", person1Name);
64+
params.put("person2_name", person2Name);
65+
66+
try (Session session = driver.session()) {
67+
Record record = session.writeTransaction(tx -> {
68+
Result result = tx.run(createFriendshipQuery, params);
69+
return result.single();
70+
});
71+
System.out.println(String.format("Created friendship between: %s, %s",
72+
record.get("p1").get("name").asString(),
73+
record.get("p2").get("name").asString()));
74+
// You should capture any errors along with the query and data for traceability
75+
} catch (Neo4jException ex) {
76+
LOGGER.log(Level.SEVERE, createFriendshipQuery + " raised an exception", ex);
77+
throw ex;
78+
}
79+
}
80+
81+
public void findPerson(final String personName) {
82+
String readPersonByNameQuery = "MATCH (p:Person)\n" +
83+
"WHERE p.name = $person_name\n" +
84+
"RETURN p.name AS name";
85+
86+
Map<String, Object> params = Collections.singletonMap("person_name", personName);
87+
88+
try (Session session = driver.session()) {
89+
Record record = session.readTransaction(tx -> {
90+
Result result = tx.run(readPersonByNameQuery, params);
91+
return result.single();
92+
});
93+
System.out.println(String.format("Found person:: %s", record.get("name").asString()));
94+
// You should capture any errors along with the query and data for traceability
95+
} catch (Neo4jException ex) {
96+
LOGGER.log(Level.SEVERE, readPersonByNameQuery + " raised an exception", ex);
97+
throw ex;
6198
}
6299
}
63100

64-
public static void main( String... args ) throws Exception
65-
{
66-
try ( DriverIntroductionExample greeter = new DriverIntroductionExample( "bolt://localhost:7687", "neo4j", "password" ) )
67-
{
68-
greeter.printGreeting( "hello, world" );
101+
public static void main(String... args) throws Exception {
102+
String boltUrl = "%%BOLT_URL_PLACEHOLDER%%";
103+
String user = "<Username for Neo4j Aura database>";
104+
String password = "<Password for Neo4j Aura database>";
105+
try (DriverIntroductionExample app = new DriverIntroductionExample(boltUrl, user, password)) {
106+
app.createFriendship("Alice", "David");
107+
app.findPerson("Alice");
69108
}
70109
}
71110
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,28 @@ void testShouldRunHelloWorld() throws Exception
304304
}
305305
}
306306

307+
@Test
308+
void testShouldRunDriverIntroduction() throws Exception
309+
{
310+
// Given
311+
try ( DriverIntroductionExample intro = new DriverIntroductionExample( uri, USER, PASSWORD ) )
312+
{
313+
// When
314+
StdIOCapture stdIO = new StdIOCapture();
315+
316+
try ( AutoCloseable ignored = stdIO.capture() )
317+
{
318+
intro.createFriendship( "Alice", "David" );
319+
intro.findPerson( "Alice" );
320+
}
321+
322+
// Then
323+
assertThat( stdIO.stdout().size(), equalTo( 2 ) );
324+
assertThat( stdIO.stdout().get( 0 ), containsString( "Create friendship between: Alice, David" ) );
325+
assertThat( stdIO.stdout().get( 1 ), containsString( "Found person: Alice" ) );
326+
}
327+
}
328+
307329
@Test
308330
void testShouldRunReadWriteTransactionExample() throws Exception
309331
{

0 commit comments

Comments
 (0)