1919package org .neo4j .docs .driver ;
2020
2121import java .io .File ;
22+ import java .util .ArrayList ;
2223import java .util .List ;
2324
25+ import org .neo4j .driver .v1 .AuthTokens ;
2426import org .neo4j .driver .v1 .Config ;
2527import org .neo4j .driver .v1 .Driver ;
2628import org .neo4j .driver .v1 .GraphDatabase ;
27- import org .neo4j .driver .v1 .StatementResult ;
28- import org .neo4j .driver .v1 .summary .Notification ;
29- import org .neo4j .driver .v1 .util .Pair ;
3029import org .neo4j .driver .v1 .Record ;
31- import org .neo4j .driver .v1 .summary .ResultSummary ;
3230import org .neo4j .driver .v1 .Session ;
31+ import org .neo4j .driver .v1 .StatementResult ;
3332import org .neo4j .driver .v1 .Transaction ;
34- import org .neo4j .driver .v1 .Value ;
3533import org .neo4j .driver .v1 .Values ;
34+ import org .neo4j .driver .v1 .exceptions .ClientException ;
35+ import org .neo4j .driver .v1 .summary .Notification ;
36+ import org .neo4j .driver .v1 .summary .ResultSummary ;
3637
3738public class Examples
3839{
3940
4041 public static Driver constructDriver () throws Exception
4142 {
4243 // tag::construct-driver[]
43- Driver driver = GraphDatabase .driver ( "bolt://localhost" );
44+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ) );
4445 // end::construct-driver[]
4546
4647 return driver ;
@@ -49,8 +50,10 @@ public static Driver constructDriver() throws Exception
4950 public static Driver configuration () throws Exception
5051 {
5152 // tag::configuration[]
52- Driver driver =
53- GraphDatabase .driver ( "bolt://localhost" , Config .build ().withMaxSessions ( 10 ).toConfig () );
53+ Driver driver = GraphDatabase .driver (
54+ "bolt://localhost" ,
55+ AuthTokens .basic ("neo4j" , "neo4j" ),
56+ Config .build ().withMaxSessions ( 10 ).toConfig () );
5457 // end::configuration[]
5558
5659 return driver ;
@@ -60,82 +63,111 @@ public static void statement( Session session ) throws Exception
6063 {
6164 // tag::statement[]
6265 StatementResult result =
63- session .run ( "CREATE (p :Person { name: {name} })" , Values .parameters ( "name" , "The One " ) );
64-
66+ session .run ( "CREATE (person :Person {name: {name}})" , Values .parameters ( "name" , "Arthur " ) );
67+ // end::statement[]
6568 int theOnesCreated = result .consume ().counters ().nodesCreated ();
6669 System .out .println ( "There were " + theOnesCreated + " the ones created." );
67- // end::statement[]
6870 }
6971
7072 public static void statementWithoutParameters ( Session session ) throws Exception
7173 {
7274 // tag::statement-without-parameters[]
73- StatementResult result = session .run ( "CREATE (p:Person { name: 'The One ' })" );
74-
75+ StatementResult result = session .run ( "CREATE (p:Person { name: 'Arthur ' })" );
76+ // end::statement-without-parameters[]
7577 int theOnesCreated = result .consume ().counters ().nodesCreated ();
7678 System .out .println ( "There were " + theOnesCreated + " the ones created." );
77- // end::statement-without-parameters[]
7879 }
7980
80- public static void resultCursor ( Session session ) throws Exception
81+ public static void resultTraversal ( Session session ) throws Exception
82+ {
83+ // tag::result-traversal[]
84+ String searchTerm = "Sword" ;
85+ StatementResult result = session .run ( "MATCH (weapon:Weapon) WHERE weapon.name CONTAINS {term} RETURN weapon.name" ,
86+ Values .parameters ( "term" , searchTerm ) );
87+
88+ System .out .println ("List of weapons called " + searchTerm + ":" );
89+ while ( result .hasNext () )
90+ {
91+ Record record = result .next ();
92+ System .out .println (record .get ("weapon.name" ).asString ());
93+ }
94+ // end::result-traversal[]
95+ }
96+
97+ public static void accessRecord ( Session session ) throws Exception
8198 {
82- // tag::result-cursor[]
83- StatementResult result = session .run ( "MATCH (p:Person { name: {name} }) RETURN p.age" ,
84- Values .parameters ( "name" , "The One" ) );
99+ // tag::access-record[]
100+ String searchTerm = "Arthur" ;
101+ StatementResult result = session .run ( "MATCH (weapon:Weapon) WHERE weapon.owner CONTAINS {term} RETURN weapon.name, weapon.material, weapon.size" ,
102+ Values .parameters ( "term" , searchTerm ) );
85103
104+ System .out .println ("List of weapons owned by " + searchTerm + ":" );
86105 while ( result .hasNext () )
87106 {
88107 Record record = result .next ();
89- for ( Pair <String ,Value > fieldInRecord : record .fields () )
108+ List <String > sword = new ArrayList <>();
109+ for ( String key : record .keys () )
90110 {
91- System . out . println ( fieldInRecord . key () + " = " + fieldInRecord . value ( ) );
111+ sword . add ( key + ": " + record . get ( key ) );
92112 }
113+ System .out .println (sword );
93114 }
94- // end::result-cursor []
115+ // end::access-record []
95116 }
96117
97118 public static void retainResultsForNestedQuerying ( Session session ) throws Exception
98119 {
99- // tag::retain-result-query []
100- StatementResult result = session .run ( "MATCH (p :Person { name: {name} }) RETURN id(p) " ,
101- Values .parameters ( "name " , "The One " ) );
120+ // tag::nested-statements []
121+ StatementResult result = session .run ( "MATCH (knight :Person:Knight) WHERE knight.castle = {castle} RETURN id(knight) AS knight_id " ,
122+ Values .parameters ( "castle " , "Camelot " ) );
102123
103124 for ( Record record : result .list () )
104125 {
105- session .run ( "MATCH (p) WHERE id(p) = {id} " + "CREATE (p)-[:HAS_TRAIT]->(:Trait {type:'Immortal'})" ,
106- Values .parameters ( "id" , record .get ( "id(p)" ) ) );
126+ session .run ( "MATCH (knight) WHERE id(knight) = {id} " +
127+ "MATCH (king:Person) WHERE king.name = {king} " +
128+ "CREATE (knight)-[:DEFENDS]->(king)" ,
129+ Values .parameters ( "id" , record .get ( "knight_id" ), "king" , "Arthur" ) );
107130 }
108- // end::retain-result-query []
131+ // end::nested-statements []
109132 }
110133
111134 public static void retainResultsForLaterProcessing ( Driver driver ) throws Exception
112135 {
113- // tag::retain-result-process[]
114136 Session session = driver .session ();
115-
116- StatementResult result = session .run ( "MATCH (p :Person { name: {name} }) RETURN p.age " ,
117- Values .parameters ( "name " , "The One " ) );
137+ // tag::retain-result[]
138+ StatementResult result = session .run ( "MATCH (knight :Person:Knight) WHERE knight.castle = {castle} RETURN knight.name AS name " ,
139+ Values .parameters ( "castle " , "Camelot " ) );
118140
119141 List <Record > records = result .list ();
120-
121142 session .close ();
122143
123144 for ( Record record : records )
124145 {
125- for ( Pair <String ,Value > fieldInRecord : record .fields () )
126- {
127- System .out .println ( fieldInRecord .key () + " = " + fieldInRecord .value () );
128- }
146+ System .out .println ( record .get ("name" ).asString () + " is a knight of Camelot" );
129147 }
130- // end::retain-result-process[]
148+ // end::retain-result[]
149+ }
150+
151+ public static void handleCypherError ( Session session ) throws Exception
152+ {
153+ // tag::handle-cypher-error[]
154+ try
155+ {
156+ session .run ( "This will cause a syntax error" ).consume ();
157+ }
158+ catch ( ClientException e )
159+ {
160+ throw new RuntimeException ("Something really bad has happened!" );
161+ }
162+ // end::handle-cypher-error[]
131163 }
132164
133165 public static void transactionCommit ( Session session ) throws Exception
134166 {
135167 // tag::transaction-commit[]
136168 try ( Transaction tx = session .beginTransaction () )
137169 {
138- tx .run ( "CREATE (p :Person { name: 'The One' })" );
170+ tx .run ( "CREATE (:Person {name: 'Guinevere' })" );
139171 tx .success ();
140172 }
141173 // end::transaction-commit[]
@@ -146,7 +178,7 @@ public static void transactionRollback( Session session ) throws Exception
146178 // tag::transaction-rollback[]
147179 try ( Transaction tx = session .beginTransaction () )
148180 {
149- tx .run ( "CREATE (p :Person { name: 'The One' })" );
181+ tx .run ( "CREATE (:Person {name: 'Merlin' })" );
150182 tx .failure ();
151183 }
152184 // end::transaction-rollback[]
@@ -156,7 +188,7 @@ public static void resultSummary( Session session ) throws Exception
156188 {
157189 // tag::result-summary-query-profile[]
158190 StatementResult result = session .run ( "PROFILE MATCH (p:Person { name: {name} }) RETURN id(p)" ,
159- Values .parameters ( "name" , "The One " ) );
191+ Values .parameters ( "name" , "Arthur " ) );
160192
161193 ResultSummary summary = result .consume ();
162194
@@ -168,7 +200,7 @@ public static void resultSummary( Session session ) throws Exception
168200 public static void notifications ( Session session ) throws Exception
169201 {
170202 // tag::result-summary-notifications[]
171- ResultSummary summary = session .run ( "EXPLAIN MATCH (a ), (b ) RETURN a,b " ).consume ();
203+ ResultSummary summary = session .run ( "EXPLAIN MATCH (king ), (queen ) RETURN king, queen " ).consume ();
172204
173205 for ( Notification notification : summary .notifications () )
174206 {
@@ -180,7 +212,7 @@ public static void notifications( Session session ) throws Exception
180212 public static Driver requireEncryption () throws Exception
181213 {
182214 // tag::tls-require-encryption[]
183- Driver driver = GraphDatabase .driver ( "bolt://localhost" ,
215+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ),
184216 Config .build ().withEncryptionLevel ( Config .EncryptionLevel .REQUIRED ).toConfig () );
185217 // end::tls-require-encryption[]
186218
@@ -190,8 +222,8 @@ public static Driver requireEncryption() throws Exception
190222 public static Driver trustOnFirstUse () throws Exception
191223 {
192224 // tag::tls-trust-on-first-use[]
193- Driver driver = GraphDatabase .driver ( "bolt://localhost" , Config .build ()
194- .withEncryptionLevel ( Config .EncryptionLevel .NONE )
225+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ), Config .build ()
226+ .withEncryptionLevel ( Config .EncryptionLevel .REQUIRED )
195227 .withTrustStrategy ( Config .TrustStrategy .trustOnFirstUse ( new File ( "/path/to/neo4j_known_hosts" ) ) )
196228 .toConfig () );
197229 // end::tls-trust-on-first-use[]
@@ -202,12 +234,22 @@ public static Driver trustOnFirstUse() throws Exception
202234 public static Driver trustSignedCertificates () throws Exception
203235 {
204236 // tag::tls-signed[]
205- Driver driver = GraphDatabase .driver ( "bolt://localhost" , Config .build ()
206- .withEncryptionLevel ( Config .EncryptionLevel .NONE )
237+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ), Config .build ()
238+ .withEncryptionLevel ( Config .EncryptionLevel .REQUIRED )
207239 .withTrustStrategy ( Config .TrustStrategy .trustSignedBy ( new File ( "/path/to/ca-certificate.pem" ) ) )
208240 .toConfig () );
209241 // end::tls-signed[]
210242
211243 return driver ;
212244 }
245+
246+ public static Driver connectWithAuthDisabled () throws Exception
247+ {
248+ // tag::connect-with-auth-disabled[]
249+ Driver driver = GraphDatabase .driver ( "bolt://localhost" ,
250+ Config .build ().withEncryptionLevel ( Config .EncryptionLevel .REQUIRED ).toConfig () );
251+ // end::connect-with-auth-disabled[]
252+
253+ return driver ;
254+ }
213255}
0 commit comments