Skip to content

Commit f32abb3

Browse files
Zhen Liboggle
authored andcommitted
Updated doc example to use king-arthur story
1 parent a10969d commit f32abb3

File tree

3 files changed

+144
-68
lines changed

3 files changed

+144
-68
lines changed

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

Lines changed: 85 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,22 @@
1919
package org.neo4j.docs.driver;
2020

2121
import java.io.File;
22+
import java.util.ArrayList;
2223
import java.util.List;
2324

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;
2827
import org.neo4j.driver.v1.summary.Notification;
2928
import org.neo4j.driver.v1.util.Pair;
30-
import org.neo4j.driver.v1.Record;
3129
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;
3630

3731
public class Examples
3832
{
3933

4034
public static Driver constructDriver() throws Exception
4135
{
4236
// tag::construct-driver[]
43-
Driver driver = GraphDatabase.driver( "bolt://localhost" );
37+
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic("neo4j", "neo4j") );
4438
// end::construct-driver[]
4539

4640
return driver;
@@ -49,8 +43,10 @@ public static Driver constructDriver() throws Exception
4943
public static Driver configuration() throws Exception
5044
{
5145
// 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() );
5450
// end::configuration[]
5551

5652
return driver;
@@ -60,82 +56,111 @@ public static void statement( Session session ) throws Exception
6056
{
6157
// tag::statement[]
6258
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[]
6561
int theOnesCreated = result.consume().counters().nodesCreated();
6662
System.out.println( "There were " + theOnesCreated + " the ones created." );
67-
// end::statement[]
6863
}
6964

7065
public static void statementWithoutParameters( Session session ) throws Exception
7166
{
7267
// 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[]
7570
int theOnesCreated = result.consume().counters().nodesCreated();
7671
System.out.println( "There were " + theOnesCreated + " the ones created." );
77-
// end::statement-without-parameters[]
7872
}
7973

80-
public static void resultCursor( Session session ) throws Exception
74+
public static void resultTraversal( Session session ) throws Exception
8175
{
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 ) );
8580

81+
System.out.println("List of weapons called " + searchTerm + ":");
8682
while ( result.hasNext() )
8783
{
8884
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() )
90103
{
91-
System.out.println( fieldInRecord.key() + " = " + fieldInRecord.value() );
104+
sword.add( key + ": " + record.get(key) );
92105
}
106+
System.out.println(sword);
93107
}
94-
// end::result-cursor[]
108+
// end::access-record[]
95109
}
96110

97111
public static void retainResultsForNestedQuerying( Session session ) throws Exception
98112
{
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" ) );
102116

103117
for ( Record record : result.list() )
104118
{
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" ) );
107123
}
108-
// end::retain-result-query[]
124+
// end::nested-statements[]
109125
}
110126

111127
public static void retainResultsForLaterProcessing( Driver driver ) throws Exception
112128
{
113-
// tag::retain-result-process[]
114129
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" ) );
118133

119134
List<Record> records = result.list();
120-
121135
session.close();
122136

123137
for ( Record record : records )
124138
{
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!");
129154
}
130-
// end::retain-result-process[]
155+
// end::handle-cypher-error[]
131156
}
132157

133158
public static void transactionCommit( Session session ) throws Exception
134159
{
135160
// tag::transaction-commit[]
136161
try ( Transaction tx = session.beginTransaction() )
137162
{
138-
tx.run( "CREATE (p:Person { name: 'The One' })" );
163+
tx.run( "CREATE (:Person {name: 'Guinevere'})" );
139164
tx.success();
140165
}
141166
// end::transaction-commit[]
@@ -146,7 +171,7 @@ public static void transactionRollback( Session session ) throws Exception
146171
// tag::transaction-rollback[]
147172
try ( Transaction tx = session.beginTransaction() )
148173
{
149-
tx.run( "CREATE (p:Person { name: 'The One' })" );
174+
tx.run( "CREATE (:Person {name: 'Merlin'})" );
150175
tx.failure();
151176
}
152177
// end::transaction-rollback[]
@@ -156,7 +181,7 @@ public static void resultSummary( Session session ) throws Exception
156181
{
157182
// tag::result-summary-query-profile[]
158183
StatementResult result = session.run( "PROFILE MATCH (p:Person { name: {name} }) RETURN id(p)",
159-
Values.parameters( "name", "The One" ) );
184+
Values.parameters( "name", "Arthur" ) );
160185

161186
ResultSummary summary = result.consume();
162187

@@ -168,7 +193,7 @@ public static void resultSummary( Session session ) throws Exception
168193
public static void notifications( Session session ) throws Exception
169194
{
170195
// 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();
172197

173198
for ( Notification notification : summary.notifications() )
174199
{
@@ -180,7 +205,7 @@ public static void notifications( Session session ) throws Exception
180205
public static Driver requireEncryption() throws Exception
181206
{
182207
// tag::tls-require-encryption[]
183-
Driver driver = GraphDatabase.driver( "bolt://localhost",
208+
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic("neo4j", "neo4j"),
184209
Config.build().withEncryptionLevel( Config.EncryptionLevel.REQUIRED ).toConfig() );
185210
// end::tls-require-encryption[]
186211

@@ -190,8 +215,8 @@ public static Driver requireEncryption() throws Exception
190215
public static Driver trustOnFirstUse() throws Exception
191216
{
192217
// 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 )
195220
.withTrustStrategy( Config.TrustStrategy.trustOnFirstUse( new File( "/path/to/neo4j_known_hosts" ) ) )
196221
.toConfig() );
197222
// end::tls-trust-on-first-use[]
@@ -202,12 +227,22 @@ public static Driver trustOnFirstUse() throws Exception
202227
public static Driver trustSignedCertificates() throws Exception
203228
{
204229
// 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 )
207232
.withTrustStrategy( Config.TrustStrategy.trustSignedBy( new File( "/path/to/ca-certificate.pem") ) )
208233
.toConfig() );
209234
// end::tls-signed[]
210235

211236
return driver;
212237
}
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+
}
213248
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,24 @@
2020

2121
// NOTE: Be careful about auto-formatting here: All imports should be between the tags below.
2222
// tag::minimal-example-import[]
23-
import org.neo4j.driver.v1.Driver;
24-
import org.neo4j.driver.v1.GraphDatabase;
25-
import org.neo4j.driver.v1.Record;
26-
import org.neo4j.driver.v1.StatementResult;
27-
import org.neo4j.driver.v1.Session;
23+
import org.neo4j.driver.v1.*;
2824
// end::minimal-example-import[]
2925

3026
public class MinimalWorkingExample
3127
{
3228
public static void minimalWorkingExample() throws Exception
3329
{
3430
// tag::minimal-example[]
35-
Driver driver = GraphDatabase.driver( "bolt://localhost" );
31+
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );
3632
Session session = driver.session();
3733

38-
session.run( "CREATE (neo:Person {name:'Neo', age:23})" );
34+
session.run( "CREATE (a:Person {name:'Arthur', title:'King'})" );
3935

40-
StatementResult result = session.run( "MATCH (p:Person) WHERE p.name = 'Neo' RETURN p.age" );
36+
StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" );
4137
while ( result.hasNext() )
4238
{
4339
Record record = result.next();
44-
System.out.println( "Neo is " + record.get( "p.age" ).asInt() + " years old." );
40+
System.out.println( record.get( "title" ).asString() + " " + record.get("name").asString() );
4541
}
4642

4743
session.close();

0 commit comments

Comments
 (0)