19
19
package org .neo4j .docs .driver ;
20
20
21
21
import java .io .File ;
22
+ import java .util .ArrayList ;
22
23
import java .util .List ;
23
24
25
+ import org .neo4j .driver .v1 .AuthTokens ;
24
26
import org .neo4j .driver .v1 .Config ;
25
27
import org .neo4j .driver .v1 .Driver ;
26
28
import 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 ;
30
29
import org .neo4j .driver .v1 .Record ;
31
- import org .neo4j .driver .v1 .summary .ResultSummary ;
32
30
import org .neo4j .driver .v1 .Session ;
31
+ import org .neo4j .driver .v1 .StatementResult ;
33
32
import org .neo4j .driver .v1 .Transaction ;
34
- import org .neo4j .driver .v1 .Value ;
35
33
import 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 ;
36
37
37
38
public class Examples
38
39
{
39
40
40
41
public static Driver constructDriver () throws Exception
41
42
{
42
43
// tag::construct-driver[]
43
- Driver driver = GraphDatabase .driver ( "bolt://localhost" );
44
+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ) );
44
45
// end::construct-driver[]
45
46
46
47
return driver ;
@@ -49,8 +50,10 @@ public static Driver constructDriver() throws Exception
49
50
public static Driver configuration () throws Exception
50
51
{
51
52
// 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 () );
54
57
// end::configuration[]
55
58
56
59
return driver ;
@@ -60,82 +63,111 @@ public static void statement( Session session ) throws Exception
60
63
{
61
64
// tag::statement[]
62
65
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[]
65
68
int theOnesCreated = result .consume ().counters ().nodesCreated ();
66
69
System .out .println ( "There were " + theOnesCreated + " the ones created." );
67
- // end::statement[]
68
70
}
69
71
70
72
public static void statementWithoutParameters ( Session session ) throws Exception
71
73
{
72
74
// 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[]
75
77
int theOnesCreated = result .consume ().counters ().nodesCreated ();
76
78
System .out .println ( "There were " + theOnesCreated + " the ones created." );
77
- // end::statement-without-parameters[]
78
79
}
79
80
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
81
98
{
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 ) );
85
103
104
+ System .out .println ("List of weapons owned by " + searchTerm + ":" );
86
105
while ( result .hasNext () )
87
106
{
88
107
Record record = result .next ();
89
- for ( Pair <String ,Value > fieldInRecord : record .fields () )
108
+ List <String > sword = new ArrayList <>();
109
+ for ( String key : record .keys () )
90
110
{
91
- System . out . println ( fieldInRecord . key () + " = " + fieldInRecord . value ( ) );
111
+ sword . add ( key + ": " + record . get ( key ) );
92
112
}
113
+ System .out .println (sword );
93
114
}
94
- // end::result-cursor []
115
+ // end::access-record []
95
116
}
96
117
97
118
public static void retainResultsForNestedQuerying ( Session session ) throws Exception
98
119
{
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 " ) );
102
123
103
124
for ( Record record : result .list () )
104
125
{
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" ) );
107
130
}
108
- // end::retain-result-query []
131
+ // end::nested-statements []
109
132
}
110
133
111
134
public static void retainResultsForLaterProcessing ( Driver driver ) throws Exception
112
135
{
113
- // tag::retain-result-process[]
114
136
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 " ) );
118
140
119
141
List <Record > records = result .list ();
120
-
121
142
session .close ();
122
143
123
144
for ( Record record : records )
124
145
{
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" );
129
147
}
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[]
131
163
}
132
164
133
165
public static void transactionCommit ( Session session ) throws Exception
134
166
{
135
167
// tag::transaction-commit[]
136
168
try ( Transaction tx = session .beginTransaction () )
137
169
{
138
- tx .run ( "CREATE (p :Person { name: 'The One' })" );
170
+ tx .run ( "CREATE (:Person {name: 'Guinevere' })" );
139
171
tx .success ();
140
172
}
141
173
// end::transaction-commit[]
@@ -146,7 +178,7 @@ public static void transactionRollback( Session session ) throws Exception
146
178
// tag::transaction-rollback[]
147
179
try ( Transaction tx = session .beginTransaction () )
148
180
{
149
- tx .run ( "CREATE (p :Person { name: 'The One' })" );
181
+ tx .run ( "CREATE (:Person {name: 'Merlin' })" );
150
182
tx .failure ();
151
183
}
152
184
// end::transaction-rollback[]
@@ -156,7 +188,7 @@ public static void resultSummary( Session session ) throws Exception
156
188
{
157
189
// tag::result-summary-query-profile[]
158
190
StatementResult result = session .run ( "PROFILE MATCH (p:Person { name: {name} }) RETURN id(p)" ,
159
- Values .parameters ( "name" , "The One " ) );
191
+ Values .parameters ( "name" , "Arthur " ) );
160
192
161
193
ResultSummary summary = result .consume ();
162
194
@@ -168,7 +200,7 @@ public static void resultSummary( Session session ) throws Exception
168
200
public static void notifications ( Session session ) throws Exception
169
201
{
170
202
// 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 ();
172
204
173
205
for ( Notification notification : summary .notifications () )
174
206
{
@@ -180,7 +212,7 @@ public static void notifications( Session session ) throws Exception
180
212
public static Driver requireEncryption () throws Exception
181
213
{
182
214
// tag::tls-require-encryption[]
183
- Driver driver = GraphDatabase .driver ( "bolt://localhost" ,
215
+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ),
184
216
Config .build ().withEncryptionLevel ( Config .EncryptionLevel .REQUIRED ).toConfig () );
185
217
// end::tls-require-encryption[]
186
218
@@ -190,8 +222,8 @@ public static Driver requireEncryption() throws Exception
190
222
public static Driver trustOnFirstUse () throws Exception
191
223
{
192
224
// 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 )
195
227
.withTrustStrategy ( Config .TrustStrategy .trustOnFirstUse ( new File ( "/path/to/neo4j_known_hosts" ) ) )
196
228
.toConfig () );
197
229
// end::tls-trust-on-first-use[]
@@ -202,12 +234,22 @@ public static Driver trustOnFirstUse() throws Exception
202
234
public static Driver trustSignedCertificates () throws Exception
203
235
{
204
236
// 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 )
207
239
.withTrustStrategy ( Config .TrustStrategy .trustSignedBy ( new File ( "/path/to/ca-certificate.pem" ) ) )
208
240
.toConfig () );
209
241
// end::tls-signed[]
210
242
211
243
return driver ;
212
244
}
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
+ }
213
255
}
0 commit comments