|
21 | 21 | // tag::driver-introduction-example-import[]
|
22 | 22 |
|
23 | 23 | import org.neo4j.driver.*;
|
| 24 | +import org.neo4j.driver.exceptions.Neo4jException; |
24 | 25 |
|
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; |
26 | 31 | // end::driver-introduction-example-import[]
|
27 | 32 |
|
28 | 33 | // 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()); |
31 | 36 | private final Driver driver;
|
32 | 37 |
|
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); |
36 | 46 | }
|
37 | 47 |
|
38 | 48 | @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. |
41 | 51 | driver.close();
|
42 | 52 | }
|
43 | 53 |
|
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; |
61 | 98 | }
|
62 | 99 | }
|
63 | 100 |
|
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"); |
69 | 108 | }
|
70 | 109 | }
|
71 | 110 | }
|
|
0 commit comments