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
24
- import org .neo4j .driver .v1 .Config ;
25
- import org .neo4j .driver .v1 .Driver ;
26
- import org .neo4j .driver .v1 .GraphDatabase ;
27
- import org .neo4j .driver .v1 .StatementResult ;
25
+ import org .neo4j .driver .v1 .*;
26
+ import org .neo4j .driver .v1 .exceptions .ClientException ;
28
27
import org .neo4j .driver .v1 .summary .Notification ;
29
28
import org .neo4j .driver .v1 .util .Pair ;
30
- import org .neo4j .driver .v1 .Record ;
31
29
import org .neo4j .driver .v1 .summary .ResultSummary ;
32
- import org .neo4j .driver .v1 .Session ;
33
- import org .neo4j .driver .v1 .Transaction ;
34
- import org .neo4j .driver .v1 .Value ;
35
- import org .neo4j .driver .v1 .Values ;
36
30
37
31
public class Examples
38
32
{
39
33
40
34
public static Driver constructDriver () throws Exception
41
35
{
42
36
// tag::construct-driver[]
43
- Driver driver = GraphDatabase .driver ( "bolt://localhost" );
37
+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ) );
44
38
// end::construct-driver[]
45
39
46
40
return driver ;
@@ -49,8 +43,10 @@ public static Driver constructDriver() throws Exception
49
43
public static Driver configuration () throws Exception
50
44
{
51
45
// tag::configuration[]
52
- Driver driver =
53
- GraphDatabase .driver ( "bolt://localhost" , Config .build ().withMaxSessions ( 10 ).toConfig () );
46
+ Driver driver = GraphDatabase .driver (
47
+ "bolt://localhost" ,
48
+ AuthTokens .basic ("neo4j" , "neo4j" ),
49
+ Config .build ().withMaxSessions ( 10 ).toConfig () );
54
50
// end::configuration[]
55
51
56
52
return driver ;
@@ -60,82 +56,111 @@ public static void statement( Session session ) throws Exception
60
56
{
61
57
// tag::statement[]
62
58
StatementResult result =
63
- session .run ( "CREATE (p :Person { name: {name} })" , Values .parameters ( "name" , "The One " ) );
64
-
59
+ session .run ( "CREATE (person :Person {name: {name}})" , Values .parameters ( "name" , "Arthur " ) );
60
+ // end::statement[]
65
61
int theOnesCreated = result .consume ().counters ().nodesCreated ();
66
62
System .out .println ( "There were " + theOnesCreated + " the ones created." );
67
- // end::statement[]
68
63
}
69
64
70
65
public static void statementWithoutParameters ( Session session ) throws Exception
71
66
{
72
67
// tag::statement-without-parameters[]
73
- StatementResult result = session .run ( "CREATE (p:Person { name: 'The One ' })" );
74
-
68
+ StatementResult result = session .run ( "CREATE (p:Person { name: 'Arthur ' })" );
69
+ // end::statement-without-parameters[]
75
70
int theOnesCreated = result .consume ().counters ().nodesCreated ();
76
71
System .out .println ( "There were " + theOnesCreated + " the ones created." );
77
- // end::statement-without-parameters[]
78
72
}
79
73
80
- public static void resultCursor ( Session session ) throws Exception
74
+ public static void resultTraversal ( Session session ) throws Exception
81
75
{
82
- // tag::result-cursor[]
83
- StatementResult result = session .run ( "MATCH (p:Person { name: {name} }) RETURN p.age" ,
84
- Values .parameters ( "name" , "The One" ) );
76
+ // tag::result-traversal[]
77
+ String searchTerm = "Sword" ;
78
+ StatementResult result = session .run ( "MATCH (weapon:Weapon) WHERE weapon.name CONTAINS {term} RETURN weapon.name" ,
79
+ Values .parameters ( "term" , searchTerm ) );
85
80
81
+ System .out .println ("List of weapons called " + searchTerm + ":" );
86
82
while ( result .hasNext () )
87
83
{
88
84
Record record = result .next ();
89
- for ( Pair <String ,Value > fieldInRecord : record .fields () )
85
+ System .out .println (record .get ("weapon.name" ).asString ());
86
+ }
87
+ // end::result-traversal[]
88
+ }
89
+
90
+ public static void accessRecord ( Session session ) throws Exception
91
+ {
92
+ // tag::access-record[]
93
+ String searchTerm = "Arthur" ;
94
+ StatementResult result = session .run ( "MATCH (weapon:Weapon) WHERE weapon.owner CONTAINS {term} RETURN weapon.name, weapon.material, weapon.size" ,
95
+ Values .parameters ( "term" , searchTerm ) );
96
+
97
+ System .out .println ("List of weapons owned by " + searchTerm + ":" );
98
+ while ( result .hasNext () )
99
+ {
100
+ Record record = result .next ();
101
+ List <String > sword = new ArrayList <>();
102
+ for ( String key : record .keys () )
90
103
{
91
- System . out . println ( fieldInRecord . key () + " = " + fieldInRecord . value ( ) );
104
+ sword . add ( key + ": " + record . get ( key ) );
92
105
}
106
+ System .out .println (sword );
93
107
}
94
- // end::result-cursor []
108
+ // end::access-record []
95
109
}
96
110
97
111
public static void retainResultsForNestedQuerying ( Session session ) throws Exception
98
112
{
99
- // tag::retain-result-query []
100
- StatementResult result = session .run ( "MATCH (p :Person { name: {name} }) RETURN id(p) " ,
101
- Values .parameters ( "name " , "The One " ) );
113
+ // tag::nested-statements []
114
+ StatementResult result = session .run ( "MATCH (knight :Person:Knight) WHERE knight.castle = {castle} RETURN id(knight) AS knight_id " ,
115
+ Values .parameters ( "castle " , "Camelot " ) );
102
116
103
117
for ( Record record : result .list () )
104
118
{
105
- session .run ( "MATCH (p) WHERE id(p) = {id} " + "CREATE (p)-[:HAS_TRAIT]->(:Trait {type:'Immortal'})" ,
106
- Values .parameters ( "id" , record .get ( "id(p)" ) ) );
119
+ session .run ( "MATCH (knight) WHERE id(knight) = {id} " +
120
+ "MATCH (king:Person) WHERE king.name = {king} " +
121
+ "CREATE (knight)-[:DEFENDS]->(king)" ,
122
+ Values .parameters ( "id" , record .get ( "knight_id" ), "king" , "Arthur" ) );
107
123
}
108
- // end::retain-result-query []
124
+ // end::nested-statements []
109
125
}
110
126
111
127
public static void retainResultsForLaterProcessing ( Driver driver ) throws Exception
112
128
{
113
- // tag::retain-result-process[]
114
129
Session session = driver .session ();
115
-
116
- StatementResult result = session .run ( "MATCH (p :Person { name: {name} }) RETURN p.age " ,
117
- Values .parameters ( "name " , "The One " ) );
130
+ // tag::retain-result[]
131
+ StatementResult result = session .run ( "MATCH (knight :Person:Knight) WHERE knight.castle = {castle} RETURN knight.name AS name " ,
132
+ Values .parameters ( "castle " , "Camelot " ) );
118
133
119
134
List <Record > records = result .list ();
120
-
121
135
session .close ();
122
136
123
137
for ( Record record : records )
124
138
{
125
- for ( Pair <String ,Value > fieldInRecord : record .fields () )
126
- {
127
- System .out .println ( fieldInRecord .key () + " = " + fieldInRecord .value () );
128
- }
139
+ System .out .println ( record .get ("name" ).asString () + "is a knight of Camelot" );
140
+ }
141
+ // end::retain-result[]
142
+ }
143
+
144
+ public static void handleCypherError ( Session session ) throws Exception
145
+ {
146
+ // tag::handle-cypher-error[]
147
+ try
148
+ {
149
+ session .run ( "This will cause a syntax error" ).consume ();
150
+ }
151
+ catch ( ClientException e )
152
+ {
153
+ throw new RuntimeException ("Something really bad has happened!" );
129
154
}
130
- // end::retain-result-process []
155
+ // end::handle-cypher-error []
131
156
}
132
157
133
158
public static void transactionCommit ( Session session ) throws Exception
134
159
{
135
160
// tag::transaction-commit[]
136
161
try ( Transaction tx = session .beginTransaction () )
137
162
{
138
- tx .run ( "CREATE (p :Person { name: 'The One' })" );
163
+ tx .run ( "CREATE (:Person {name: 'Guinevere' })" );
139
164
tx .success ();
140
165
}
141
166
// end::transaction-commit[]
@@ -146,7 +171,7 @@ public static void transactionRollback( Session session ) throws Exception
146
171
// tag::transaction-rollback[]
147
172
try ( Transaction tx = session .beginTransaction () )
148
173
{
149
- tx .run ( "CREATE (p :Person { name: 'The One' })" );
174
+ tx .run ( "CREATE (:Person {name: 'Merlin' })" );
150
175
tx .failure ();
151
176
}
152
177
// end::transaction-rollback[]
@@ -156,7 +181,7 @@ public static void resultSummary( Session session ) throws Exception
156
181
{
157
182
// tag::result-summary-query-profile[]
158
183
StatementResult result = session .run ( "PROFILE MATCH (p:Person { name: {name} }) RETURN id(p)" ,
159
- Values .parameters ( "name" , "The One " ) );
184
+ Values .parameters ( "name" , "Arthur " ) );
160
185
161
186
ResultSummary summary = result .consume ();
162
187
@@ -168,7 +193,7 @@ public static void resultSummary( Session session ) throws Exception
168
193
public static void notifications ( Session session ) throws Exception
169
194
{
170
195
// tag::result-summary-notifications[]
171
- ResultSummary summary = session .run ( "EXPLAIN MATCH (a ), (b ) RETURN a,b " ).consume ();
196
+ ResultSummary summary = session .run ( "EXPLAIN MATCH (king ), (queen ) RETURN king, queen " ).consume ();
172
197
173
198
for ( Notification notification : summary .notifications () )
174
199
{
@@ -180,7 +205,7 @@ public static void notifications( Session session ) throws Exception
180
205
public static Driver requireEncryption () throws Exception
181
206
{
182
207
// tag::tls-require-encryption[]
183
- Driver driver = GraphDatabase .driver ( "bolt://localhost" ,
208
+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ),
184
209
Config .build ().withEncryptionLevel ( Config .EncryptionLevel .REQUIRED ).toConfig () );
185
210
// end::tls-require-encryption[]
186
211
@@ -190,8 +215,8 @@ public static Driver requireEncryption() throws Exception
190
215
public static Driver trustOnFirstUse () throws Exception
191
216
{
192
217
// tag::tls-trust-on-first-use[]
193
- Driver driver = GraphDatabase .driver ( "bolt://localhost" , Config .build ()
194
- .withEncryptionLevel ( Config .EncryptionLevel .NONE )
218
+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ), Config .build ()
219
+ .withEncryptionLevel ( Config .EncryptionLevel .REQUIRED )
195
220
.withTrustStrategy ( Config .TrustStrategy .trustOnFirstUse ( new File ( "/path/to/neo4j_known_hosts" ) ) )
196
221
.toConfig () );
197
222
// end::tls-trust-on-first-use[]
@@ -202,12 +227,22 @@ public static Driver trustOnFirstUse() throws Exception
202
227
public static Driver trustSignedCertificates () throws Exception
203
228
{
204
229
// tag::tls-signed[]
205
- Driver driver = GraphDatabase .driver ( "bolt://localhost" , Config .build ()
206
- .withEncryptionLevel ( Config .EncryptionLevel .NONE )
230
+ Driver driver = GraphDatabase .driver ( "bolt://localhost" , AuthTokens . basic ( "neo4j" , "neo4j" ), Config .build ()
231
+ .withEncryptionLevel ( Config .EncryptionLevel .REQUIRED )
207
232
.withTrustStrategy ( Config .TrustStrategy .trustSignedBy ( new File ( "/path/to/ca-certificate.pem" ) ) )
208
233
.toConfig () );
209
234
// end::tls-signed[]
210
235
211
236
return driver ;
212
237
}
238
+
239
+ public static Driver connectWithAuthDisabled () throws Exception
240
+ {
241
+ // tag::connect-with-auth-disabled[]
242
+ Driver driver = GraphDatabase .driver ( "bolt://localhost" ,
243
+ Config .build ().withEncryptionLevel ( Config .EncryptionLevel .REQUIRED ).toConfig () );
244
+ // end::connect-with-auth-disabled[]
245
+
246
+ return driver ;
247
+ }
213
248
}
0 commit comments